-1

it is reqired to create a DB if not exist and then save the table. The function getConnection() shown in code. 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 installer. Installed n 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 programatically 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
                }
    
mexco
  • 5
  • 2
  • See the SQLite documentation: [How to specify database files](https://github.com/xerial/sqlite-jdbc#how-to-specify-database-files). You may also be interested in this discussion on [where to place read write user files in a deployed JavaFX application](https://stackoverflow.com/questions/72294934/how-to-save-a-file-uploaded-by-filechooser-to-a-directory-in-your-project). – jewelsea Jul 15 '22 at 16:40
  • 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 (!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); – mexco Jul 16 '22 at 01:40

1 Answers1

0

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
         }
             
mexco
  • 5
  • 2
  • It would be nice if you answer contained description of the change needed and it's reasoning (which is in the comment above, but it should be in the answer). – Martin Prikryl Jul 16 '22 at 06:23
  • after installation, when first time, the application will be started, it will create a folder using File theDir = new File( "c://spm_database"); Thereafter, all read and writes are done to this folder. This worked for me. – mexco Jul 18 '23 at 06:17