如何在sqlite JDBC中设置数据库密码? [英] How to set database password in sqlite JDBC?

查看:118
本文介绍了如何在sqlite JDBC中设置数据库密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用JDBC连接到sqlite数据库,这是一个示例代码,用于创建数据库并在其中创建一个示例表。

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

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

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?

如评论中所述,在.Net中我可以在建立连接时执行以下操作

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
}

那等价的是什么?

推荐答案

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

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");

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

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

这篇关于如何在sqlite JDBC中设置数据库密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆