JDBC Connection Access database several ways to introduce _jsp programming

Source: Internet
Author: User
Tags odbc access database java web
The following is a summary of several commonly used connection methods.

For example, the following Access database student, table Basic, and 6 records, now display their data in a JSP in several ways. As shown in the figure:

For several ways to connect to an Access database, it is basically based on the JDBC-ODBC approach and, of course, a pure JDBC-driven approach. I'm not going to say this here for a while. For each of these ways, other code is the same except to get a connection. So here are some ways to get a connection, and then use the full code to display it.

Way one: Through the Jdbc-odbc Way Bridge connects directly:

1, for this way, first to establish an ODBC data source, my system is Win7 system, so select the Control Panel----management tools----data source (ODBC), open the data Source Manager, as shown in the figure:

2. On the System DSN tab, click the Add button, open the Create Data Source dialog box, select the driver for the Access database, Microsoft access Driver (*.mdb), as shown in the figure:

3, click the Completion button, the following dialog box appears, enter the name of the data source in the data source name "Jdbc-odbc", click the Select button, select the database to operate "Student.mdb", click OK button to complete the configuration of the data source. As shown in the figure:

4, the data source configuration is good, you can write to get the connection code, as follows:

Copy Code code 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 are key to connecting Access databases via JDBC-ODBC. This way it is useful to connect to the URL code that can be easily memorized by the connection. The following code is normally written.

mode two: Connect through the absolute path of the database

It is said that several ways are based on the JDBC-ODBC approach. So the parameters in the load-driven class.forname () are "Sun.jdbc.odbc.JdbcOdbcDriver". For this way I put the Student.mdb file in the root of e disk, when used, write directly to the absolute path of the database line. The code to get the connection looks like this:

Copy Code code as follows:

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D bq=" + "E://student.mdb";
Connection con = drivermanager.getconnection (URL);


There is no need to configure the data source for this way, although the code is more, but it is well understood. is also very common.

mode three: To obtain the absolute path of the database by request connection

In this way, I personally think it is suitable for use in Java Web Applications, will do a good job for others, others can use. I placed the database file under the root path of the Web application. The code that dynamically gets the connection is as follows:

Copy Code code 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)};D bq=" +path+ "Student.mdb";
Connection con = drivermanager.getconnection (URL);


The above is three ways to get the connection. The next step is to display the code. The code looks like this:

Copy Code code 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" >
<title>Access</title>
<body>
<table border= "1" width= "40%" >
<tr bgcolor= "Gray" >
<th> School Number </th>
<th> name </th>
<th> Age </th>
<th> Address </th>
<th> Languages </th>
<th> Math </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.executequery (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>


The results of running the JSP are as follows:

When you change the connection to the second way, the JSP code looks like this:

<%@ 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 results of running the JSP are as follows:

When you change the connection to a third way, the JSP code looks something like this:

<%@ 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 results of running the JSP are as follows:

For this way my project's directory structure looks like this:

From the above three ways you can know that no matter what kind of use is working.

This blog about the information I have uploaded, if you want to download this blog, you can download here, download the address Odbc_access_jb51.rar

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.