Eme Emertana
Eme Emertana

Reputation: 571

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();
            }

Upvotes: 2

Views: 5660

Answers (2)

LaGrandMere
LaGrandMere

Reputation: 10359

You should have:

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

Upvotes: 4

Jesper
Jesper

Reputation: 206776

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).

Upvotes: 1

Related Questions