【问题标题】:sqlite error in database数据库中的sqlite错误
【发布时间】:2017-02-06 15:12:22
【问题描述】:

这是我的连接管理器类代码

public class ConnectionFactory {
 Connection con = null;
 Statement stmt = null;
 ResultSet rs = null;
 boolean flag=false;

 //Constructor starts
 public ConnectionFactory(){
    try{
        Class.forName("org.sqlite.JDBC");
        con=DriverManager.getConnection("jdbc:sqlite:db.sqlite");
        stmt=con.createStatement();
    }catch(Exception e){
        e.printStackTrace();
    }

 }//end of constructor ConnectionFactory

 //method Connection starts
 public Connection getConnection(){
    try{
        Class.forName("org.sqlite.JDBC");
        con=DriverManager.getConnection("jdbc:sqlite:db.sqlite");
         System.out.println("Connection to SQLite has been established.");
    }catch(Exception e){
        e.printStackTrace();
    }

    return con;

 }//end of method Connection

 //method checkLogin starts
 public boolean checkLogin(String username,String password, String user){
    if(user=="ADMINISTRATOR"){
        String query="SELECT * FROM users WHERE username='"+username+"' AND password='"+password+"' AND category='ADMINISTRATOR'";
        try{
            rs=stmt.executeQuery(query);
            while(rs.next()){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }else{
        String query="SELECT * FROM users WHERE username='"+username+"' AND password='"+password+"'AND category='NORMAL USER'";
        try{
            rs=stmt.executeQuery(query);
            while(rs.next()){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return flag;
  }
 }

与数据库的连接成功。并且登录成功。所以很明显可以从 db 访问数据,但仍然会出现一些错误。

这些是错误

java.sql.SQLException: database connection closed
at org.sqlite.core.CoreConnection.checkOpen(CoreConnection.java:336)
at org.sqlite.jdbc4.JDBC4Connection.createStatement(JDBC4Connection.java:38)
at org.sqlite.jdbc3.JDBC3Connection.createStatement(JDBC3Connection.java:193)
at com.inventory.dao.ProductDAO.<init>(ProductDAO.java:37)
at com.inventory.ui.Products.loadDatas(Products.java:421)
at com.inventory.ui.Products.<init>(Products.java:38)
at com.inventory.ui.Dashboard.<init>(Dashboard.java:44)
at com.inventory.ui.LoginDialog.loginBttnMouseClicked(LoginDialog.java:195)
at com.inventory.ui.LoginDialog.access$100(LoginDialog.java:18)
at com.inventory.ui.LoginDialog$2.mouseClicked(LoginDialog.java:68)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.inventory.dao.ProductDAO.getQueryResult(ProductDAO.java:482)
    at com.inventory.ui.Products.loadDatas(Products.java:422)
    at com.inventory.ui.Products.<init>(Products.java:38)
    at com.inventory.ui.Dashboard.<init>(Dashboard.java:44)
    at com.inventory.ui.LoginDialog.loginBttnMouseClicked(LoginDialog.java:195)
    at com.inventory.ui.LoginDialog.access$100(LoginDialog.java:18)
    at com.inventory.ui.LoginDialog$2.mouseClicked(LoginDialog.java:68)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我已经尝试了在线表格中提供的大多数选项。仍然没有帮助。

【问题讨论】:

  • 异常看起来不言自明。您正在尝试使用关闭的连接。而且,您正在尝试使用空指针。这些都不是最佳实践。

标签: java sqlite jdbc


【解决方案1】:

您正在两个不理想的地方创建连接。

重用语句和结果集对象不是一个好习惯。

您正在创建连接,但请告诉我您在哪里关闭已创建的连接。如果您在已有连接的情况下尝试创建连接,则很可能会出现连接异常问题。

因此,请确保您一次只有一个数据库连接,并且不要重复使用语句和结果集对象。

我建议重写你的 getConnection()、checkLogin() 方法。

//method Connection starts
public Connection getConnection(){
    Connection con = null;

    try{
        Class.forName("org.sqlite.JDBC");

        con = DriverManager.getConnection("jdbc:sqlite:db.sqlite");

        System.out.println("Connection to SQLite has been established.");
    }catch(Exception e){

        e.printStackTrace();
    }

    return con;
}//end of method Connection

//method checkLogin starts
public boolean checkLogin(String username,String password, String user){

    boolean flag=false;

    try( Connection con = getConnection(); Statement stmt = con.createStatement(); ){
        if(user == "ADMINISTRATOR"){

            String query = "SELECT * FROM users WHERE username='"+username+"' AND password='"+password+"' AND category='ADMINISTRATOR'";
        }else{

            String query = "SELECT * FROM users WHERE username='"+username+"' AND password='"+password+"'AND category='NORMAL USER'";
        }

        ResultSet rs = stmt.executeQuery(query);

        while(rs.next()){
            flag = true;
        }
    }catch(Exception e){

        e.printStackTrace();
    }

    return flag;
}

【讨论】:

  • @AnshulSharma 删除在 ConnectionFactory 的构造函数中创建连接
  • nope.. 它没用.. 我想我的代码还有其他问题。
【解决方案2】:

您永远不会在查询中调用连接。在你的所有陈述中尝试这个

 public boolean checkLogin(String username,String password, String user){      
    this.getConnection(); // See your System.out.prinln in the console
        Statement stmt = null;
        ResultSet rs = null; 
                if(user=="ADMINISTRATOR"){
            Statement stmt = con.createStatement();   
            String query="SELECT * FROM users WHERE username='"+username+"' AND password='"+password+"' AND category='ADMINISTRATOR'";
            ResultSet rs = stmt.executeQuery(s);
                    try{
                        rs=stmt.executeQuery(query);
                        while(rs.next()){
                            flag=true;
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }
        finally {
          stmt.close();
          rs.close();
        }

【讨论】:

  • 它实际上消除了一个错误,但仍然存在一个错误
  • ` com.inventory.ui.LoginDialog 的 com.inventory.database.ConnectionFactory.checkLogin(ConnectionFactory.java:71) 的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常。 loginBttnMouseClicked(LoginDialog.java:197) at com.inventory.ui.LoginDialog.access$100(LoginDialog.java:21) at com.inventory.ui.LoginDialog$2.mouseClicked(LoginDialog.java:71) at java.awt.Component .processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) `
  • 糟糕的是,我拨打了两次连接。我编辑了我的答案。
  • 这没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
相关资源
最近更新 更多