0
votes

I have created an java project on NetBeans 8.2 IDE, the program runs perfectly on the IDE , but while i try to run through .jar file of the project, the sqlite database does not connect, please guide me what to do? P.S: I am new to stack overflow so i dont know the exact way of asking a question!

Here is the connection code to the database:

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

By giving absolute address it throws an null pointer eception:

run:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at DatabaseConnection.SQLiteJDBCDriverConnection.Login(SQLiteJDBCDriverConnection.java:83) at goldsmith.billing.and.database.Login.btnLoginActionPerformed(Login.java:205) at goldsmith.billing.and.database.Login.access$000(Login.java:15) at goldsmith.billing.and.database.Login$1.actionPerformed(Login.java:90) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at 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) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) 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:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) 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:80) 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)

Here is the Code:

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
Is this for a business? Or just a side project for fun/education? - mypetlion
this is for a small firm just to test an automated environment - Muhammed Talha Hashmi

1 Answers

0
votes

You use a relative path to the SQLite database file:

jdbc:sqlite:SaeedGoldCastingDB.sqlite

Depending on where the file SaeedGoldCastingDB.sqlite actually is, Java may not be able to find it when your program is executed.

Use an absolute path instead.