* ** JAVA Access to the Access database * because there is no driver like Access to other databases *, you can only Access the * example Table: createtableuser (idint, namechar (50 ), ageint), id is auto-incrementing * importjava. SQL. *; publicclassAccess_Conn {publicConnectiongetConn
/*** JAVA Access to the Access database * because there is no driver like accessing other databases *, you can only Access the * example table: create table user (id int, name char (50), age int), id is auto-incrementing */import java. SQL. *; public class Access_Conn {public Connection getConn
/**
* Accessing the Access database using JAVA
* Because there is no driver like accessing other databases
* Therefore, it can only be accessed through ODBC.
* Example table: create table user (id int, name char (50), age int), id: auto-incrementing
*/
Import java. SQL .*;
Public class Access_Conn {
Public Connection getConn (){
// Test indicates the name of the configured data source.
String url = "jdbc: odbc: test ";
Connection con = null;
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Con = DriverManager. getConnection (url ,"","");
} Catch (Exception e ){
E. printStackTrace ();
}
Return con;
}
Public static void main (String [] arg)
{
Access_Conn con = new Access_Conn ();
Connection conn = con. getConn ();
Try {
Statement st = conn. createStatement ();
St.exe cute ("insert into user (name, age) values ('test', 20 )");
ResultSet rs1_st.exe cuteQuery ("select * from user ");
While (rs. next ())
{
System. out. println (rs. getInt (1) + "" + rs. getString (2) + "" + rs. getInt (3 ));
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Finally {
Try {
If (conn! = Null)
Conn. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}