sqlitejavafxjarexeinno-setup

SQLite DB creation issue after installation in JAVAFX based appication created in Eclipse


I working an application using JavaFX to create a SQLite DB. If DB does not exist, it must be created. The function getConnection() shown in code.

There is form in GUI. After typing the values of all fields, the user will click SAVE button. The entries should be saved in table. The program implemented is as:

Save button event calls btnsave() -> insertrecord() -> executequery() -> getConnection()

I tested application in following ways:

  1. Inside the eclipse installed on PC1 --> worked well. Database is created and table entries are deleted successfully.

  2. Create the runnable JAR and copied to other laptop PC2 --. works well as in case 1.

  3. Inno Setup is installed on PC2. So, used Inno to make an installer. Installed on PC2. It seems data is not created. If it is created, I don't know where will it be created in the system? How to programmatically read the database path?

     @FXML
     private void btnsave(ActionEvent event) throws Exception{       
       insertrecord();
       System.out.println("New Patient Inserted");       
     }
    
     private void insertrecord() throws Exception
     {
          try{
          String query ="INSERT INTO `newpatient`(patientId,patientName,patientAge,patientGender,patientAddress,patientMobile)"+
             "VALUES("+ newpatient_id.getText() +",'" +  newpatient_name.getText() + "','"+  newpatient_age.getText() + "',"
                     + "'"+  selectedGender  + "','"+  newpatient_address.getText() + "',"+  newpatient_mobile.getText() +")";             
           executeQuery(query);
           System.out.println("Saved");
          }
          catch(Exception e) {
           //System.out.println("Execption in Save");
           e.printStackTrace();            
           }
          }
    
            private void executeQuery(String query) {
    
      Connection  conn= getConnection();
      Statement st;
      try {
             st = conn.createStatement();
             st.executeUpdate(query);         
         }catch(Exception e){
           e.printStackTrace(); 
        }
       }
    
    
        public static Connection getConnection() {       
         Connection conn = null;
    
          try{           
    
                 Class.forName("org.sqlite.JDBC");
                 conn = DriverManager.getConnection("jdbc:sqlite:patientdata","root","");
                 System.out.println("data base connection established:  "+ conn.toString());
    
                 Statement stmt = null;
                 stmt = conn.createStatement();
                 String pat = "CREATE TABLE if not exists newpatient " +
                                "(patientId INTEGER NOT NULL," +
                                " patientName    CHAR(50)    NOT NULL, " + 
                                " patientAge     INTEGER     NOT NULL, " + 
                                "patientGender   CHAR(10) NOT NULL,"+
                                "patientAddress  CHAR(100) NOT NULL,"+
                                "patientMobile   BIGINT(10) NOT NULL)";
                 System.out.println("newpatient Table Created:  ");
                 stmt.executeUpdate(pat);
                 stmt.close();
    
                 stmt = conn.createStatement();
                 String hist = "CREATE TABLE if not exists history " +
                                    "(id INTEGER NOT NULL," +
                                    " date    DATE    NOT NULL, " + 
                                    " start   TIME     NOT NULL, " +                                               
                                    "stop   TIME NOT NULL)";
                 System.out.println("history Table Created:  ");    
                 stmt.executeUpdate(hist);
                 stmt.close();
                 return conn
                }
    

Solution

  • There was misconception in my mind that jdbc:sqlite: automatically creates the database file in the same folder where application is installed. But, the folder is write protected where any application is installed. The following worked for me:

    public static Connection getConnection() {      
        Connection conn = null;
    
            try{        
                
                    File theDir = new File( "c://spm_database");
                    if (!theDir.exists()){
                        theDir.mkdirs();
                    }
                    System.out.println(theDir.toString());
                    String dbpath = theDir.toString()+"/patientdata.db";
                    System.out.println("Current absolute dbpath is: " + dbpath);
                    
                    Class.forName("org.sqlite.JDBC");
                    conn = DriverManager.getConnection("jdbc:sqlite:"+dbpath);
                    System.out.println("data base connection established:  "+ conn.toString());
                   // extra code .....
               }
               catch(Exception e){
                }
               retrun conn
             }