Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在sqlite JDBC中设置数据库密码?_Java_Sqlite_Jdbc - Fatal编程技术网

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

Java 如何在sqlite JDBC中设置数据库密码?,java,sqlite,jdbc,Java,Sqlite,Jdbc,在我的应用程序中,我使用JDBC连接到sqlite数据库,下面是创建数据库并在其中创建示例表的示例代码 public class Main { public static void main(String[] args) { try { Class.forName("org.sqlite.JDBC"); Connection connection = DriverManager.getConnection("jdbc:s

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

正如在评论中提到的,在.Net中,我可以在建立连接时执行以下操作

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

那么JDBC的等价物是什么呢?

在标准的SQLite发行版中,无法在SQLite数据库上设置密码。在Java中连接到SQLite数据库的方式是:

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

确保在运行程序时,SQLite JDBC驱动程序位于类路径上。

无法将密码设置为SQLite db。Sqlite3支持密码保护。我建议您选择H2database(),因为它非常快速、开源,而且也是用java编写的。它提供嵌入式数据库和服务器数据库。Sqlite不支持重命名和删除列。但是h2提供了所有必要的功能。

从 有一个名为
sqlite-jdbc-crypt
(“支持加密和身份验证的sqlite-jdbc驱动程序”)的Apache 2.0许可包,可在

单击中心的“Releases”选项卡下载一个pre-build.jar文件

下面是创建名为
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);
    }
}

可能的副本只有.net代码示例,我对此非常熟悉,并且知道.net新的SQLiteConnection的等价物是什么(“Data Source=db.sqlite;Password=123;”)在JDBCO中,如果我想连接到一个已经存在的具有密码的数据库,该怎么办?然后使用上面的语法:
DriverManager.getConnection(“jdbc:sqlite:/path/to/file.db”,“username”,“password”)
正如我上面提到的,它并没有给出期望的结果。如果您能澄清您所展示的代码的期望结果是什么,我将不胜感激。我假设您的问题是关于如何在JDBC中创建到SQLite数据库的
连接。你能给我一个连接到数据库的示例代码吗?