我需要将 odbc 数据库连接到我的 java 代码。 我知道要连接 mdb 数据库,我需要使用此代码,但它不起作用:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "C:/porogram/pro.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
非常感谢。
最佳答案
目前我正在使用 jdbc-odbc 桥接器,我的代码对我来说 100% 有效:
this.jdbcUser = PropUtil.getValue(configFile, "jdbc.user");
this.jdbcPass = PropUtil.getValue(configFile, "jdbc.pass");
this.jdbcUrl = PropUtil.getValue(configFile, "jdbc.url");
this.jdbcDriver = PropUtil.getValue(configFile, "jdbc.driver");
//Be sure to load required JDBC driver
Class.forName(jdbcDriver);
dbconn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass);
属性文件:
jdbc.user=db_user
jdbc.pass=db_pass
jdbc.url=jdbc\:odbc\:Driver={Microsoft Access Driver (*.mdb)};DBQ=C\:/Data/data1.mdb
jdbc.driver=sun.jdbc.odbc.JdbcOdbcDriver
请分享有关您的问题的更多信息,例如堆栈跟踪或其他内容。
关于java - 如何将 odbc 数据库连接到我的 java 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10946090/