简体   繁体   中英

SQLite Java JDBC Insert

Following is my Java code. In linux, it is working fine but in Windows I'm unable to insert data into the database on local disk. In NetBeans get it all right but .jar file not. JDBC driver see be good.

Connecting to database:

public static Connection connectToDb() {
    try {
        Connection connection = null;
        DriverManager.registerDriver(new org.sqlite.JDBC());
        //LINUX PATH
        if (OSDetector.isLinux()) {
            connection = DriverManager.getConnection("jdbc:sqlite:/home/" + userNameLinux + "/PDFMalwareDataAnalyser/DatabaseSQLite/database.db", NAME, PASSWORD);
            //WINDOWS PATH 
        } else {
            connection = DriverManager.getConnection("jdbc:sqlite:C:\\PDFMalwareDataAnalyser\\DatabaseSQLite\\database.db", NAME, PASSWORD);
        }
        connection.setAutoCommit(true);
        if (connection != null) {
            System.out.println("Otvorená.");
        }
        return connection;
    } catch (SQLException e) {
        System.err.println(e.getClass().getName() + e.getMessage());
        //  System.exit(0);
    }
    return null;
}

Insertion:

public void insertDataToDatabase(int idReport) throws SQLException {
    connection = new SQLiteJDBC().connectToDb();
    PreparedStatement insertCommunication = connection.prepareStatement("insert into table_communication values(?,?);");
    insertCommunication.setString(2, communicationsFinal.toString());
    try {
        insertCommunication.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    }
    insertCommunication.close();
    connection.close();
    System.out.println("1. --- Insert do tabuľky TABLE_COMMUNICATION OK ---");
}

尝试这个:

connection = DriverManager.getConnection("jdbc:sqlite:C:/PDFMalwareDataAnalyser/DatabaseSQLite/database.db")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM