JAVA Access to the Access Database

Source: Internet
Author: User
* ** 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 ();
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.