在其他笔记本电脑/PC 上安装基于 JAVAFX 的应用程序后创建 SQLite 数据库的问题 [重复]

问题描述 投票:0回答:1

如果不存在需要创建一个DB然后保存表。代码中显示的函数 getConnection()。实施的方案如下: SAVE按钮事件调用btnsave()->insertrecord()->executequery()->getConnection()

我通过以下方式测试应用程序:

  1. 在 PC1 上安装的 eclipse 内部 --> 运行良好。创建数据库并成功删除表条目。

  2. Create the runnable Jar and copy to other laptop PC2 --.与情况 1 一样运作良好。

  3. Inno setup 安装在 PC2 上。因此,使用 INNO 来制作安装程序。安装 n PC2。似乎没有创建数据。如果创建了,不知道会在系统的什么地方创建?如何以编程方式读取数据库路径?

       @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
                }
    
eclipse javafx jar exe inno-setup
1个回答
0
投票

我有一个误解,认为 jdbc:sqlite: 会自动在安装应用程序的同一文件夹中创建数据库文件。但是,该文件夹在安装任何应用程序的地方都被写保护。以下对我有用:

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
         }
             
© www.soinside.com 2019 - 2024. All rights reserved.