5

In my application I am using JDBC connection to the sqlite database, and here is a sample code that creates the database and creates a sample table in it.

public class Main {

    public static void main(String[] args) {
        try {
            Class.forName("org.sqlite.JDBC");

            Connection connection = DriverManager.getConnection("jdbc:sqlite:db.sqlite","admin","123");

            Statement statement = connection.createStatement();

            String query = "CREATE TABLE Users(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "Login TEXT, " +
                    "Password TEXT);";
            statement.execute(query);

            String query1 = "INSERT INTO Users(Login, Password) VALUES ('user1','password1')";
            String query2 = "INSERT INTO Users(Login, Password) VALUES ('user2','password2')";
            String query3 = "INSERT INTO Users(Login, Password) VALUES ('user3','password3')";

            statement.addBatch(query1);
            statement.addBatch(query2);
            statement.addBatch(query3);

            statement.executeBatch();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

Now the question is, I can easily open my db file without typing any user or password info from outside, so where are the parameters I give to the DriverManager used and how to specify a password for the database?

As mentioned in a comment, in .Net I can do following when making a connection

using(SQLiteConnection con = new SQLiteConnection("Data Source=db.sqlite; Password=123;")
{
     //code goes here
}

So what is the JDBC equivalent for that?

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • possible duplicate of [SQLite with encryption/password protection](http://stackoverflow.com/questions/5669905/sqlite-with-encryption-password-protection) – Gord Thompson Nov 30 '14 at 22:53
  • There are only .net code samples, I'm quite familiar with that, and what to know what is the equivalent for the .nets new SQLiteConnection("Data Source=db.sqlite; Password=123;") in JDBC – vcmkrtchyan Nov 30 '14 at 23:08

3 Answers3

2

Setting password to sqlite db is not possible. Sqlite3 supports password protection though. I recommend you to choose H2 database (http://www.h2database.com/html/main.html), since it is very fast and opensource and also written in java. It provides both embedded database as well as server database. Sqlite does not support renaming and deleting columns. But h2 provides all the necessary features.

Kumar
  • 110
  • 14
1

There is no way of setting a password on a SQLite database in the standard SQLite distribution. The way you make a connection to a SQLite database in Java is:

Class.forName("org.sqlite.JDBC"); // force loading of SQLite JDBC driver
Connection conn = DriverManager.getConnection("jdbc:sqlite:/path/to/file.db");

Make sure when you run the program, that the SQLite JDBC driver is on the classpath.

moosingin3space
  • 326
  • 2
  • 5
  • so what if i want to connect to an already existing database that has a password? – vcmkrtchyan Nov 30 '14 at 23:47
  • Then you use the syntax you had above: `DriverManager.getConnection("jdbc:sqlite:/path/to/file.db", "username", "password")` – moosingin3space Nov 30 '14 at 23:48
  • well as i mentioned above it does not give the desired result – vcmkrtchyan Dec 01 '14 at 00:20
  • I would appreciate it if you would clarify exactly what the desired result of the code you've presented is, then. I assumed your question was regarding how to create a `Connection` to a SQLite database in JDBC. – moosingin3space Dec 01 '14 at 00:57
  • 1
    Okay, let's say I have an sqlite database which has a password on it, let's say "12345678". Can you give me a sample code that connects to that database? – vcmkrtchyan Dec 01 '14 at 05:43
0

As copied from https://stackoverflow.com/a/54797910/1365319

There is an Apache 2.0 licensed package named sqlite-jdbc-crypt ("SQLite JDBC Driver with encryption and authentication support") available at https://github.com/Willena/sqlite-jdbc-crypt

Click on the 'Releases' tab in the center to download a pre-build .jar file.

Here's example Java code to create an encrypted SQLite database named db.sql which is encrypted with the password apassword:

package com.name.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

    public static void main(final String[] args) {

        try (final Connection connection = DriverManager.getConnection("jdbc:sqlite:db.sqlite", "",
                "apassword")) {
            try (final Statement stmt = connection.createStatement()) {
                stmt.execute("CREATE TABLE test (data TEXT(10));");
                stmt.execute("INSERT INTO test VALUES('hello');");
            }
            connection.close();
        } catch (final SQLException e) {
            e.printStackTrace();
        }

        System.out.println("finished");
        System.exit(0);
    }
}
Karthik Bose
  • 33,556
  • 3
  • 33
  • 43