将sqlite数据库链接到java桌面应用程序的正确方法是什么?

时间:2017-05-03 03:44:52

标签: java sqlite

我正在使用java开发桌面应用程序,并使用SQLite数据库,但是当我重新启动eclipse时,我从所有表中丢失了我的数据,如何处理这个问题?我的db连接文件在这里,当我在mozilla上使用sqlite manager来显示数据库时,它没有显示我从java代码中添加的任何数据。但这两个使用相同的.db文件?谢谢你帮助我:)对不起我的英语

public class SQLiteConnection {

static Connection con = null;

public static Connection dbConnector(){

    try{
        Class.forName("org.sqlite.JDBC");
        String path = SQLiteConnection.class.getResource("Paritmetik.db").toString();
        con = DriverManager.getConnection("jdbc:sqlite:"+path);
        Statement stat = con.createStatement();
        stat.executeUpdate("CREATE TABLE IF NOT EXISTS `Student` (`id` INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "
                + "                                 `firstName` VARCHAR NOT NULL , "
                + "                                 `lastName` VARCHAR, "
                + "                                 `dob` DATETIME, "
                + "                                 `gender` VARCHAR, "
                + "                                 `image` VARCHAR UNIQUE );");
        stat.executeUpdate("CREATE TABLE IF NOT EXISTS `list` (`id` INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "
                + "                                             `studentID` INTEGER NOT NULL , "
                + "                                             `qLevel` VARCHAR NOT NULL , "
                + "                                             `qTotalNumbersInOneQuestion` INTEGER NOT NULL , "
                + "                                             `qQuestionNumbers` INTEGER NOT NULL , "
                + "                                             `qSpeed` DOUBLE NOT NULL , "
                + "                                             `rightAnsweredCount` INTEGER NOT NULL , "
                + "                                             `successByPercent` DOUBLE, "
                + "                                             `date` VARCHAR NOT NULL);");

        return con;
    }catch(Exception e){
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "Өгөгдлийн сантай холбогдож чадсангүй.");
    }

    return null;
}

}

0 个答案:

没有答案