Introduction to several methods for connecting JDBC to the Access Database

Source: Internet
Author: User

Next, we will summarize several common connection methods.

For example, the following Access database student, table basic, and six records are displayed in Jsp in several ways. :

For several ways to connect to the Access database, basically is based on the JDBC-ODBC, of course, there are pure JDBC driver. I will not talk about it for the moment. Except for the difference in the connection, other codes are the same. So here we will first write several methods to get the connection, and then use the complete code to display it.

Method 1: direct connection through the JDBC-ODBC Bridge:

1. For this method, you must first create an ODBC Data Source. My system is Windows 7, so select "Control Panel-management tools-Data Source (ODBC)" in sequence to open the data source Manager ,:

2. on the "system DSN" tab, click the "add" button to open the "Create data source" dialog box, and select the "Microsoft Access Driver (*. mdb)" Driver of the Access database )":

3. Click the finish button, the following dialog box appears, enter the data source name "JDBC-ODBC" in the data source name, click the select button, select the Database "student. mdb, click OK to complete the data source configuration. :

4. After the data source is configured, you can write the connection Code as follows:

Copy codeThe Code is as follows: Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: JDBC-ODBC ";
Connection con = DriverManager. getConnection (url );

The above three lines of code is the key code to connect to the Access database through the JDBC-ODBC. This method can easily remember the url code of the connection, which is very useful. The subsequent code is normally written.

Method 2: connect through the absolute path of the database

The methods mentioned above are based on the JDBC-ODBC method. Therefore, the parameters in the Class. forName () of the driver are "sun. jdbc. odbc. JdbcOdbcDriver ". In this way, I put the student. mdb file under the root directory of the E disk. When using the file, just write the absolute path of the database. The code for getting the connection is as follows:

Copy codeThe Code is as follows: Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ =" + "e: // student. mdb ";
Connection con = DriverManager. getConnection (url );

You do not need to configure the data source for this method. Although there are many codes, it is easy to understand. It is also very common.

Method 3: Obtain the absolute path of the database through the request

For this method, I personally think it is suitable for Java Web applications and can be used by others. I put the database file under the root path of the Web application. The code for dynamically obtaining a connection is as follows:

Copy codeThe Code is as follows: String path = application. getRealPath ("/index. jsp ");
Path = path. substring (0, path. lastIndexOf ("\") + "\\";
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ =" + path + "student. mdb ";
Connection con = DriverManager. getConnection (url );

The preceding three methods are used to obtain the connection. The following is the displayed code. The Code is as follows:

Copy codeThe Code is as follows: <% @ page language = "java" import = "java. util. *, java. SQL. * "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> Access </title>
</Head>
<Body>
<Table border = "1" width = "40%">
<Tr bgcolor = "gray">
<Th> Student ID </th>
<Th> name </th>
<Th> age </th>
<Th> address </th>
<Th> language </th>
<Th> mathematics </th>
<Th> English </th>
</Tr>
<%
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: JDBC-ODBC ";
Connection con = DriverManager. getConnection (url );
Statement st = con. createStatement ();
String SQL = "select * from basic ";
ResultSet rs = st.exe cuteQuery (SQL );
While (rs. next ())
{
%>
<Tr>
<Td> <% = rs. getString (1) %> </td>
<Td> <% = rs. getString (2) %> </td>
<Td> <% = rs. getInt (3) %> </td>
<Td> <% = rs. getString (4) %> </td>
<Td> <% = rs. getInt (5) %> </td>
<Td> <% = rs. getInt (6) %> </td>
<Td> <% = rs. getInt (7) %> </td>
</Tr>
<%
}
Rs. close ();
St. close ();
Con. close ();
%>
</Table>
</Body>
</Html>

The result of running JSP is as follows:

The JSP code is as follows:

<% @ Page language = "java" import = "java. util. *, java. SQL. * "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

The result of running JSP is as follows:

The JSP code is as follows:

<% @ Page language = "java" import = "java. util. *, java. SQL. * "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

The result of running JSP is as follows:

In this way, the directory structure of my project is as follows:

You can see from the three methods above that either of them is easy to use.

Bytes

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.