-3

I created a SQLite DB in JavaFx using Eclipse. The function to created and access the data base is shown below. I created the Runnable jar using Export option. i tried two methods: Method1: Created an installer using Inno. Then installed the application on another laptop for testing. The was successfully installed and launched. The problem was that data base was not created. While, it worked very well in laptop inside Eclipse. Method2: I included the database files form Eclipse based folder along with application JAR in Inno while creating installer. The exe set up with Data Base files included, was installed on another laptop. The installation and application launch was successful. The issue was that the table entries which were present already in the data base were shown. But, insertion of new entries and deletion of existing entries was not done. The project structure is shown in image.

    public static Connection getConnection() {      
    Connection conn = null;
    Statement stmt = null;
    try {
            String dbName  = "patientdata";             
            File file = new File (dbName);
             //Class.forName("com.mysql.cj.jdbc.Driver");            
             //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,"root","");     
            if(file.exists())System.out.println("patient file exists:  "+ file.getAbsolutePath());
            else System.out.println("patient file not exist: Creaeted new one ");
            
            try{
                Class.forName("org.sqlite.JDBC");
                //conn = DriverManager.getConnection("jdbc:sqlite:"+dbName,"root","");
                conn = DriverManager.getConnection("jdbc:sqlite:"+dbName);
                System.out.println("data base connection established:  "+ conn.toString());
            
                    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();
                      
                      Dialog<Void> pop = new Dialog<Void>();
                      pop.setContentText("Data base accessed");
                      pop.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);

                      Node closeButton = pop.getDialogPane().lookupButton(ButtonType.CLOSE);
                      closeButton.setVisible(false);

                      pop.showAndWait();
                                                
            }catch(SQLException tb){
                System.err.println(tb.getClass().getName() + ": " + tb.getMessage());
                  Dialog<Void> pop = new Dialog<Void>();
                  pop.setContentText("Data base  not accessed");
                  pop.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);

                  Node closeButton = pop.getDialogPane().lookupButton(ButtonType.CLOSE);
                  closeButton.setVisible(false);

                  pop.showAndWait();
            }
    }catch(Exception e)
    {
        //System.out.println("errors to Create Data Base : "+e.getMessage());
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return conn;
}


  private void insertrecord()
{
    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() +")";
    System.out.println(gender);     
    executeQuery(query);
        System.out.println("Saved");
        Main.selected_patient_id = Integer.parseInt(newpatient_id.getText());
       }
    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(); 
     }
}

     @FXML
     private void btnFetchHistory(ActionEvent event) {                                  
        try {                       
             Existpatcontrol existpat_controller = new Existpatcontrol();               
             
             FXMLLoader loader1 = new FXMLLoader();
             loader1.setController(existpat_controller.getTvHistory());
             //URL urlext = loader1.getClass().getClassLoader().getResource("Historypat.fxml");
            //Parent root1 = FXMLLoader.load(urlext);
             loader1.setLocation(getClass().getResource("Historypat.fxml"));
             Parent root1 = loader1.load();
             
            
             Stage stage1 = new Stage();
             stage1.setTitle("History");
             stage1.setScene(new Scene(root1));
             stage1.show();                 
                
        }catch (Exception hs) {
            
            System.out.println("errors"+hs.getMessage());
            try {
                PrintStream ps = new PrintStream(Main.test_file);
                hs.printStackTrace(ps);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("errors"+e.getMessage());
            }               
        }            
     } 

enter image description here

enter image description here enter image description here

mexco
  • 5
  • 2
  • 1
    Use the formatting feature of your IDE to [format code](https://stackoverflow.com/questions/15655126/how-to-auto-format-code-in-eclipse) that you post. – jewelsea Jul 05 '22 at 17:49
  • 2
    Log the complete stack trace in the places where you are just printing the exception method. Copy any stack traces and place them in the question, formatted as code. – jewelsea Jul 05 '22 at 19:06
  • Hi jewelsea, I am not much expert. Pls, just write with a dummy example, how to do what you suggested in second comment. – mexco Jul 06 '22 at 14:59
  • [e.printStackTrace](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Throwable.html#printStackTrace()). You already do that in places in the code, just do it consistently. Then copy the entire output of the stack trace if one is printed and place it in the text formatted as code, just as you have already added your java code to the question. Study [how to use stack traces](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – jewelsea Jul 06 '22 at 19:21
  • The problem is that: 1. After Installation on another laptop, the stack trace messages are not shown, so could not find the error reason and location. 2. The system.out , system.err etc work inside eclipse only, so how to enable to show errors / exceptions outside Eclipse ? – mexco Jul 07 '22 at 06:29
  • Getting the console and error stream output of your application depends on your OS and how you run the application. Try running from a command line and see if the output is in the command prompt. If not, perhaps ask on [superuser](https://superuser.com/), I haven't used that site, but it may be better suited for the question about collecting program output. – jewelsea Jul 07 '22 at 10:48
  • I saved the exceptions in file. Then, I came to know that "required location" or "Location no set" exception was raised in btnFetchHistory method, added to the post just now. In this I used two methods (commented one and uncommented one). In both same exception of path occurred. – mexco Jul 08 '22 at 10:03
  • the snippet in fetchHistory looks fishy: typically, set the controller _before_ loading to get its fields injected. Also, looks like you are using some external field (Main.existPatController.getTvHistory), are you? If so, that's calling for desaster, particularly if it's a globally accessible (aka: static). – kleopatra Jul 08 '22 at 10:31
  • btw: the resource lookup in the screenshot != that in the code here – kleopatra Jul 08 '22 at 10:59
  • As pointed by Kleo..in second comment, how should the path url be created in existing project set up ? If, need to modify the project set up, then what ? I am yet not much expert. – mexco Jul 08 '22 at 12:13
  • 1
    The answer kleopatra linked contains information and a troubleshooting section on resource lookup, follow that guide. The answer also links to the Eden tutorial on resource location. There is nothing I could write here that would be an improvement on that. – jewelsea Jul 08 '22 at 17:36

1 Answers1

0

The issue got resolved. There was misconception in my mind that jdbc:sqlite: creates the database file in the same folder where application is installed, but installation folder is write protected. I passed a fixed path as
jdbc:sqlite:c://appdata/database.db to save application run time data.

mexco
  • 5
  • 2