I try to make project with connection to db (MS Access 2010) I use this tutorial on 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][????????? ????????? ODBC] ???????? ?????? ?? ?????? ? ?? ?????? ???????, ???????????? ?? ?????????
java.sql.SQLException: [Microsoft][????????? ????????? 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)
I google it and find other questions on Stack like this : Stack Post
So i add all ODBC drivers that can help me connect *.mdb file. But nothing good hepend.(
What is it and how connect to Access DB?
To connect Java with MS Access, you need a JDBC driver. Although Microsoft do not produce a JDBC driver for MS Access, Easysoft provide two Microsoft Access JDBC drivers. Use these JDBC drivers to provide the connectivity layer between your Java code and MS Access database.
STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.
There is nothing fundamentally wrong with your code because I pasted it into Eclipse and it ran fine. The only change I made was to specify the path to the database file, i.e., 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). So, my suggestions would be
Try specifying the complete path to the .mdb
file like I did.
If you still get an error, check your Java environment to see if your application is running in a 64-bit 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. In that case you have two options:
i. Configure your application to run in a 32-bit JVM, or
ii. Download and install the 64-bit version of the Access Database Engine from here, and then use Driver={Microsoft Access Driver (*.mdb, *.accdb)}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With