简体   繁体   中英

connect to Sqlite database using java

I need to connect to a Sqlite database, I am using following code but I reckon it connects to a database in memory. how can I connect to a database on my disk.

        String sDriver = "org.sqlite.JDBC";
        String Database = "NyDatabase.sqlite";
        String sJdbc = "jdbc:sqlite";
        String sDbUrl = sJdbc + ":" + Database;
        Class.forName(sDriver);

        conn = DriverManager.getConnection(sDbUrl);
        Statement st = conn.createStatement();

        // result = st.executeQuery(Select).toString();
        rs = st.executeQuery(Select);
        while (rs.next()) {
            for (int i = 1; i <= 4; i++)
                result[i] = rs.getString(i);
        }
        conn.close();

                    } catch (SQLException e) {

                    e.printStackTrace();
            }
            catch(Exception e){
        e.printStackTrace();
            }

你应该有:

 String sDbUrl = "jdbc:sqlite:C:/path/to/myDB.db"; 

You have to use the correct JDBC URL to specify the database file.

See How to Specify Database Files in the documentation of the JDBC driver for SQLite (assuming that that's the JDBC driver you're using).

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