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

pbwdgjma  于 2023-05-17  发布在  Eclipse
关注(0)|答案(1)|浏览(83)

此问题已在此处有答案

Application does not work when installed with Inno Setup(1个答案)
10个月前就关门了。
这篇文章是编辑并提交审查6天前.
如果不存在,则需要创建一个DB,然后保存该表。代码中显示的函数getConnection()。所实施的程序如下:保存按钮事件调用btnsave()->insertrecord()->executequery()->getConnection()
我通过以下方式测试了应用程序:
1.在PC 1上安装的eclipse内部-->运行良好。已成功创建数据库并删除表项。
1.创建可运行的Jar并复制到其他笔记本电脑PC 2--。在情况1中运行良好。

  1. PC 2上安装了Inno setup。所以,使用INNO来制作安装程序。安装在PC 2上。似乎没有创建数据。如果创建了,不知道会在系统的哪个位置创建?如何通过编程读取数据库路径?
@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
            }
k4emjkb1

k4emjkb11#

在我的脑海中有一个误解,即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
         }

相关问题