JAVA method for accessing the ACCESS database

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

}

}

}

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.