Welcome to the Java Community Forum and interact with 2 million technical staff. Here we use the ODBC data source configuration method. First, you need to configure the Data source: create ODBC process: control panel -- Management Tools --> data source. Select "system DSN" --> "add", select "driverdoMicrosoftAccess", and click"
Welcome to the Java Community Forum and interact with 2 million technical staff> here we use the ODBC data source configuration method. First, you need to configure the Data source: create ODBC process: control panel -- Management Tools --> data source. Select "system DSN" --> "add", select "driver do Microsoft Access", and click"
Welcome to the Java Community Forum and interact with 2 million technical staff> enter
The ODBC data source is configured here.
First, you need to configure the Data source:
ODBC creation process:
Control Panel --> Administrative Tools --> data source.
Select "system DSN" --> "add"
Select "driver do Microsoft Access" and click "finish"
Name the data source, for example, accessTest.
Click "select" and select your database file.
In this way, a data source is configured.
The following is an example of accessing a database:
Package com. hf. accessTest;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Public class ConnectionManager {
Static {
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public static Connection getConnection (){
Connection con = null;
// String url = "jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = f: \ test. mdb ";
String url = new String ("jdbc: odbc: accessTest"); // test ?? Expected? Source
Try {
Con = DriverManager. getConnection (url );
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return con;
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// Get the connection
Connection con = ConnectionManager. getConnection ();
Try {
PreparedStatement st = con. prepareStatement ("select id, name from test1 ");
ResultSet rs = st.exe cuteQuery ();
While (rs. next ()){
String id ");
String name ");
System. out. println ("id:" + id + "name:" + name );
}
Rs. close ();
St. close ();
Con. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}