简体   繁体   English

如何在sqlite JDBC中设置数据库密码?

[英]How to set database password in sqlite JDBC?

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.在我的应用程序中,我使用 JDBC 连接到 sqlite 数据库,这里是创建数据库并在其中创建示例表的示例代码。

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?现在的问题是,我可以轻松打开我的 db 文件,而无需从外部输入任何用户或密码信息,那么我给 DriverManager 使用的参数在哪里以及如何为数据库指定密码?

As mentioned in a comment, in .Net I can do following when making a connection正如评论中提到的,在 .Net 中,我可以在建立连接时执行以下操作

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

So what is the JDBC equivalent for that?那么什么是 JDBC 等价物呢?

Setting password to sqlite db is not possible.无法将密码设置为 sqlite db。 Sqlite3 supports password protection though.不过 Sqlite3 支持密码保护。 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.我建议您选择H2数据库( http://www.h2database.com/html/main.html ),因为它非常快速且开源,并且也是用 java 编写的。 It provides both embedded database as well as server database.它提供嵌入式数据库和服务器数据库。 Sqlite does not support renaming and deleting columns. Sqlite 不支持重命名和删除列。 But h2 provides all the necessary features.但是 h2 提供了所有必要的功能。

There is no way of setting a password on a SQLite database in the standard SQLite distribution.在标准 SQLite 发行版中,无法在 SQLite 数据库上设置密码。 The way you make a connection to a SQLite database in Java is:在 Java 中连接到 SQLite 数据库的方式是:

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.确保在运行程序时,SQLite JDBC 驱动程序位于类路径中。

As copied from https://stackoverflow.com/a/54797910/1365319复制自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 https://github.com/Willena/sqlite-jdbc-crypt有一个名为sqlite-jdbc-crypt (“支持加密和身份验证的 SQLite JDBC 驱动程序”)的 Apache 2.0 许可包

Click on the 'Releases' tab in the center to download a pre-build .jar file.单击中心的“发布”选项卡以下载预构建的 .jar 文件。

Here's example Java code to create an encrypted SQLite database named db.sql which is encrypted with the password apassword :这是创建名为db.sql的加密 SQLite 数据库的示例 Java 代码,该数据库使用密码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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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