Two Methods for connecting java to the Access Database

Source: Internet
Author: User
1: Do not use the WINDOW Data Source Creation Method to connect directly to the Access database in the JAVA code. Note: jdbc: odbc: driver {MicrosoftAccessDriver (*. mdb)}; this field is null and cannot be missing! Otherwise, an exception occurs. try {Stringurljdbc: odbc: driver {MicrosoftAccessDriver (*. mdb)}; DBQF: ge123

1: Do not use the WINDOW Data Source Creation Method to connect directly to the Access database in the JAVA code. Note: jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; this field is null and cannot be missing! Otherwise, an exception occurs. try {String url = jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = F: // ge // 123.

1: Do not use WINDOW to create a data sourceMethod

Directly in the JAVA code and AccessDatabaseConnection

Note: jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; there are no spaces here! Otherwise, an exception occurs.


try{String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=F://ge//123.MDB";Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection(url);System.out.println(conn);Statement sta = conn.createStatement();ResultSet resu = sta.executeQuery("select * from Roche_position");if(resu.next()){System.out.println(resu.getString("cname"));}System.out.println("exit");}catch(Exception e){e.printStackTrace();}



2: Use ODBC to create a WINDOW Data SourceMethod:

Start

> Control Panel

> Management tools

> Data source (ODBC)

> User DSN or system DSN

> Click to add

> Find "Microsoft Access Driver (*. mdb)", select, and click Finish> Custom Data Source Name to create a new or existing AccessDatabase(. Mdb) other options are optional.

> OK

The data source has been created successfully.

The following is the JAVA codeConnectionNow:

Try {Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver"); Connection conn = DriverManager. getConnection ("jdbc: odbc: Data Source Name ","Database"," User Password (if any) ";" System. out. println (conn); Statement sta = conn. createStatement (); ResultSet resu = sta.exe cuteQuery ("select * from Roche_position"); if (resu. next () {System. out. println (resu. getString ("cname");} System. out. println ("exit");} catch (Exception e) {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.