[英]Java Access DB Connection
I try to make project with connection to db (MS Access 2010) I use this tutorial on CodeProject . 我尝试使项目与数据库连接(MS Access 2010), 并在CodeProject上使用本教程。
import java.sql.*;
public class DbAccess
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;";
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
// create a table
String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
String createTable = "CREATE TABLE " + tableName +
" (id Integer, name Text(32))";
s.execute(createTable);
// enter value into table
for(int i=0; i<25; i++)
{
String addRow = "INSERT INTO " + tableName + " VALUES ( " +
String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " +
String.valueOf(Math.random()) + "')";
s.execute(addRow);
}
// Fetch table
String selTable = "SELECT * FROM " + tableName;
s.execute(selTable);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
// drop the table
String dropTable = "DROP TABLE " + tableName;
s.execute(dropTable);
// close and cleanup
s.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
But i get strange Exception : java.sql.SQLException: [Microsoft][????????? 但是我得到奇怪的异常:java.sql.SQLException:[Microsoft] [????????? ?????????
????????? ODBC] ????????
ODBC] ???????? ??????
?????? ??
?? ??????
?????? ?
? ??
?? ??????
?????? ???????, ????????????
???????,???????????? ??
?? ?????????
?????????
java.sql.SQLException: [Microsoft][?????????
java.sql.SQLException:[Microsoft] [????????? ?????????
????????? ODBC] ????????
ODBC] ???????? ??????
?????? ??
?? ??????
?????? ?
? ??
?? ??????
?????? ???????, ????????????
???????,???????????? ??
?? ?????????
????????? at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956) at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113) at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072) at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323) at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174) at java.sql.DriverManager.getConnection(DriverManager.java:579) at java.sql.DriverManager.getConnection(DriverManager.java:221) at dbaccess.DbAccess.main(DbAccess.java:28)
在sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)在sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)在sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java )在sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)在sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)在java.sql.DriverManager.getConnection(DriverManager.java:579)在dbaccess.DbAccess.main(DbAccess.java:28)上的java.sql.DriverManager.getConnection(DriverManager.java:221)上
I google it and find other questions on Stack like this : Stack Post 我用Google搜索它,然后在Stack上找到其他类似的问题: Stack Post
So i add all ODBC drivers that can help me connect *.mdb file. 因此,我添加了可以帮助我连接* .mdb文件的所有ODBC驱动程序。 But nothing good hepend.(
但是没有什么好办法的。(
What is it and how connect to Access DB? 这是什么以及如何连接到Access DB?
There is nothing fundamentally wrong with your code because I pasted it into Eclipse and it ran fine. 您的代码从根本上没有错,因为我将其粘贴到了Eclipse中并且运行良好。 The only change I made was to specify the path to the database file, ie, instead of using
我所做的唯一更改是指定数据库文件的路径,即,而不是使用
DBQ=myDB.mdb
I used 我用了
DBQ=C:\\__tmp\\myDB.mdb
I was also running it under a 32-bit JVM (on a 32-bit computer). 我也在32位JVM(在32位计算机上)下运行它。 So, my suggestions would be
所以,我的建议是
Try specifying the complete path to the .mdb
file like I did. 像我一样尝试指定
.mdb
文件的完整路径。
If you still get an error, check your Java environment to see if your application is running in a 64-bit JVM. 如果仍然出现错误,请检查Java环境以查看您的应用程序是否在64位JVM中运行。 If it is, then
Driver={Microsoft Access Driver (*.mdb)}
will not work: there is no 64-bit version of the older Jet ODBC driver. 如果是这样,那么
Driver={Microsoft Access Driver (*.mdb)}
将不起作用:没有较旧的Jet ODBC驱动程序的64位版本。 In that case you have two options: 在这种情况下,您有两种选择:
i. 一世。 Configure your application to run in a 32-bit JVM, or
配置您的应用程序以使其在32位JVM中运行,或者
ii. ii。 Download and install the 64-bit version of the Access Database Engine from here , and then use
Driver={Microsoft Access Driver (*.mdb, *.accdb)}
. 从此处下载并安装Access数据库引擎的64位版本,然后使用
Driver={Microsoft Access Driver (*.mdb, *.accdb)}
。
Now that the JDBC-ODBC Bridge has been removed from Java 8 a better approach would be to use the UCanAccess JDBC driver. 现在,从Java 8中删除了JDBC-ODBC桥,更好的方法是使用UCanAccess JDBC驱动程序。 For more information see
有关更多信息,请参见
Manipulating an Access database from Java without ODBC 在不使用ODBC的情况下从Java操作Access数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.