运行项目

时间:2018-03-13 19:31:14

标签: java sqlite

我在NetBeans 8.2 IDE上创建了一个java项目,该程序在IDE上运行完美,但是当我尝试运行项目的.jar文件时,sqlite数据库没有连接,请指导我该怎么做? P.S:我是堆叠溢出的新手,所以我不知道提出问题的确切方法!

以下是数据库的连接代码:

   public Connection connect1() {
    try {

        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    String url = "jdbc:sqlite:SaeedGoldCastingDB.sqlite";
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return conn;
}

通过给出绝对地址,它会抛出一个空指针eception:

run:
  

线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException     在   DatabaseConnection.SQLiteJDBCDriverConnection.Login(SQLiteJDBCDriverConnection.java:83)     在   goldsmith.billing.and.database.Login.btnLoginActionPerformed(Login.java:205)     在goldsmith.billing.and.database.Login.access $ 000(Login.java:15)at   goldsmith.billing.and.database.Login $ 1.actionPerformed(Login.java:90)     在   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)     在   javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2348)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在java.awt.Component.processMouseEvent(Component.java:6533)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)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)     在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 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:80)     在   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)     在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:80)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

以下是代码:

public class SQLiteJDBCDriverConnection {
     /**
     * Connect to a sample database
     */
    public String rate=null;
    public String date=null;


 public Connection connect1() {
    try {
        // SQLite connection string
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    String url = "jdbc:sqlite:C:\\Users\\Hashmi\\Documents\\NetBeansProjects\\Goldsmith-Billing-and-database\\src\\SourceFolder\\SaeedGoldCastingDB.sqlite";
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return conn;
}
  public boolean Login(Login l){

   String idLogin = null ;
   String password = null;
   char[] pass = l.jPasswordField1.getPassword();
  String passString = new String(pass);
   String sql = "SELECT idLogin , password from Login where idLogin like '%"+l.jTextField_idLogin.getText()+"%';";
    try (Connection conn = this.connect1();
         Statement stmt  = conn.createStatement();
         ResultSet rs    = stmt.executeQuery(sql)){
        // loop through the result set
        while (rs.next()) {
          idLogin=rs.getString("idLogin");
          password=rs.getString("password");
         }
          if(idLogin.matches(l.jTextField_idLogin.getText() ) && password.matches(passString))
            return true;
           else
               return false;

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return false;
 }
  public void UpdateLogin(String idLogin,String password){

     String sql = "UPDATE Login SET password=? WHERE idLogin like '%"+idLogin+"%';";
  try (Connection conn = this.connect1();
         PreparedStatement pstmt = conn.prepareStatement(sql))
    {

              pstmt.setString(1,password);
              pstmt.executeUpdate();

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

      }     
   public void insertLogin(String idLogin,String password){

     String sql = "insert into Login(idLogin,password)values(?,?);";
  try (Connection conn = this.connect1();
         PreparedStatement pstmt = conn.prepareStatement(sql))
    {

              pstmt.setString(1,idLogin);
              pstmt.setString(2,password);
              pstmt.executeUpdate();



    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

      }     
  public boolean userCheck(Settings s){

   String idLogin = null ;
   String password = null;
   char[] pass = s.jPasswordField_OldPassword.getPassword();
   String passString = new String(pass);
   String sql = "SELECT idLogin , password from Login where idLogin like '%"+s.jTextField_useName1.getText()+"%';";
    try (Connection conn = this.connect1();
         Statement stmt  = conn.createStatement();
         ResultSet rs    = stmt.executeQuery(sql)){
        // loop through the result set



            while (rs.next())
                 {
                  idLogin=rs.getString("idLogin");
                  password=rs.getString("password");
                 }

                 if(idLogin.matches(s.jTextField_useName1.getText() ) && password.matches(passString))
            return true;
           else
               return false;




    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return false;
 }

1 个答案:

答案 0 :(得分:0)

您使用SQLite数据库文件的相对路径:

jdbc:sqlite:SaeedGoldCastingDB.sqlite

根据文件SaeedGoldCastingDB.sqlite的实际位置,Java可能无法在程序执行时找到它。

改为使用绝对路径。