JSP connects to an Access database (without the method of establishing an ODBC data source) _jsp programming

Source: Internet
Author: User
Tags stmt access database
1. In the site (My site is: E:\javatest) under the establishment of the folder "Accessdb" and under the folder to establish a database "Test.mdb" and the Table "Stu", Stu Field Id,stuname test.mdb the path is: e:\ Under Javatest\accessdb
The following figure:


2. Establish the Test connection database file "accesstest.jsp" under the site (E:\javatest)
The code is as follows:

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>access Connectivity Test </title>

<body>
<%
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
Out.print (e);
}
try{
Absolute path
String url = "Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D Bq=e:/javatest/accessdb/test.mdb";
Where E:/javatest/accessdb/test.mdb is the absolute path of the database

Relative path
String Strdirpath=getservletcontext (). Getrealpath ("/"); Get the absolute path to the site: E:\javatest\
Out.print (strdirpath+ "<br>");

Strdirpath=strdirpath.replace (' \ \ ', '/'); Replace "\" with "/" e:/javatest/
Out.print (strdirpath+ "<br>");

String url = "Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D bq=" +strdirpath+ "Accessdb/test.mdb";
Out.print (url+ "<br>");

String url = "Jdbc:odbc:accesstest";
To establish an ODBC data source connection

Connection conn = drivermanager.getconnection (URL);
Statement stmt = Conn.createstatement ();
ResultSet rs = stmt.executequery ("Select * from Stu"); Get the records in the Stu table
Out.println ("table-list" + "<br>");
while (Rs.next ()) {
Out.print (Rs.getint (1) + "");
Out.print (rs.getstring (2) + "<br>");
}
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception ex) {
Out.print (ex);
}
%>
</body>

The results of the operation are as follows:




Related Article

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.