数据库

时间:2017-02-06 15:12:22

标签: java sqlite jdbc

这是我的连接管理器类

的代码
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;
  }
 }

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

这些是错误

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)

我尝试过在线表单中提供的大多数选项。仍然没有帮助。

2 个答案:

答案 0 :(得分:0)

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

 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();
        }

答案 1 :(得分:0)

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

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

您正在创建连接,但请告诉我您关闭创建的连接的位置。如果您在已经拥有连接时尝试创建连接,则很可能会出现连接异常问题。

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

我建议重写你的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;
}