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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With