如何向尝试连接到 MS Access 数据库的 JDBC:ODBC 连接字符串添加密码

发布于 2024-08-28 05:58:44 字数 286 浏览 4 评论 0原文

这是当前在无密码保护的 MS Access 数据库上工作的连接字符串。

此代码片段来自我们的属性文件:

db.url = jdbc:odbc:Driver\={Microsoft Access Driver (*.mdb)};Dbq\=C:\Inventory.mdb;DriverID\=22;READONLY\=true

如何为受数据库密码(非 ULS)保护的 MS Access DB 添加密码到此连接字符串?

谢谢!

This is the connection string that is currently working on a non-password protected MS Access database.

this code snippet is from our properity file:

db.url = jdbc:odbc:Driver\={Microsoft Access Driver (*.mdb)};Dbq\=C:\Inventory.mdb;DriverID\=22;READONLY\=true

How do I add a password to this connection string for a MS Access DB protected by a database password (Non-ULS)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-09-04 05:58:44

从这里引用:Java 支持

db.url = jdbc:odbc:Driver\={Microsoft Access Driver (*.mdb)}Dbq\=C:\Inventory.mdb;DriverID\=22;READONLY\=true; UID\=me;PWD\=secret

Referenced from here: Java Support

db.url = jdbc:odbc:Driver\={Microsoft Access Driver (*.mdb)}Dbq\=C:\Inventory.mdb;DriverID\=22;READONLY\=true; UID\=me;PWD\=secret
无畏 2024-09-04 05:58:44

要处理受密码保护的 MS Access 数据库 2003/2007,请使用下面的代码片段

package jdbcExample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCExampleOfMSAccess2007 
{
    public static void main(String[] args) 
    {
        System.out.println("Start of Program");
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        String url=null,userID=null,password=null;
        String dbFileName=null;
        String sql=null;

        dbFileName = "C:\\temp\\MYTestDatabase.accdb";
        userID = "Admin";
        password = "Ganesh@123";
        <b>url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                "DBQ="+dbFileName+";"+
                "Uid="+userID+";"+
                "Pwd="+password+";"; </b>
        sql = "SELECT * FROM tblUserProfile";
        System.out.println("url = "+url); 

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(url,userID,password);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs!=null)
            {
                while(rs.next())
                {
                    System.out.print("User ID = "+rs.getString("User ID"));
                    System.out.print(" User Name = "+rs.getString("User Name"));
                    System.out.print(" Password = "+rs.getString("Password"));
                    System.out.println(" Access Type = "+rs.getString("Access Type"));
                }
                rs.close();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
        try {
                stmt.close();
                con.close();
            } catch (SQLException e) {
                //e.printStackTrace();
            }
        }
        System.out.println("End of Program");
    }
}

我的连接字符串 url 是,

    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
            "DBQ="+dbFileName+";"+
            "DriverID=22;READONLY=true;"+
            "Uid="+userID+";"+
            "Pwd="+password+";"; 

并且在连接到 MS Access 数据库时遇到一个问题,堆栈跟踪如下。

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x162c Thread 0x1e98 DBC 0x38f5924                                                              Jet'.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at jdbc.JDBCForMSAccess2007.main(JDBCExampleOfMSAccess2007 .java:37)

为了解决这个问题,我简单地删除了 url 字符串中的 "DriverID=22;READONLY=true;" ,问题就解决了:)
我已经在受密码保护的 MS Access 2003 和 2007 数据库上测试了给定的代码片段,并且运行良好。

希望对做新的实验有帮助。

To work on password protected MS Access Database 2003/2007 use the below code snippet

package jdbcExample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCExampleOfMSAccess2007 
{
    public static void main(String[] args) 
    {
        System.out.println("Start of Program");
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        String url=null,userID=null,password=null;
        String dbFileName=null;
        String sql=null;

        dbFileName = "C:\\temp\\MYTestDatabase.accdb";
        userID = "Admin";
        password = "Ganesh@123";
        <b>url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                "DBQ="+dbFileName+";"+
                "Uid="+userID+";"+
                "Pwd="+password+";"; </b>
        sql = "SELECT * FROM tblUserProfile";
        System.out.println("url = "+url); 

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(url,userID,password);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs!=null)
            {
                while(rs.next())
                {
                    System.out.print("User ID = "+rs.getString("User ID"));
                    System.out.print(" User Name = "+rs.getString("User Name"));
                    System.out.print(" Password = "+rs.getString("Password"));
                    System.out.println(" Access Type = "+rs.getString("Access Type"));
                }
                rs.close();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
        try {
                stmt.close();
                con.close();
            } catch (SQLException e) {
                //e.printStackTrace();
            }
        }
        System.out.println("End of Program");
    }
}

My connection string url was

    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
            "DBQ="+dbFileName+";"+
            "DriverID=22;READONLY=true;"+
            "Uid="+userID+";"+
            "Pwd="+password+";"; 

and I was facing one problem while connecting to MS Access Database the stack trace as below.

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x162c Thread 0x1e98 DBC 0x38f5924                                                              Jet'.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at jdbc.JDBCForMSAccess2007.main(JDBCExampleOfMSAccess2007 .java:37)

To overcome this problem simply I removed the "DriverID=22;READONLY=true;" in url string and the problem get solved :)
The given code snippet I have tested on password protected MS Access 2003 and 2007 Database and work well.

Hope that it will helpful to do new experiment.

夢归不見 2024-09-04 05:58:44

我知道您要求使用 ODBC,但是不可能使用 OLEDB,如 ConnectionStrings 上提供的连接字符串。 com

  Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;

我不知道 Jet ODBC 驱动程序是否提供了对数据库密码的任何支持,直到 Jet 4 才引入数据库密码(并且在 Access/Jet/ACE 的任何版本中完全没有价值)。

I know you're asking for ODBC, but is it impossible to use OLEDB, as in the connect string provided on ConnectionStrings.com:

  Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;

I don't know that the Jet ODBC driver provides any support for database passwords, which were not introduced until Jet 4 (and are completely worthless in any version of Access/Jet/ACE).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文