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