JSP connection Access database _jsp programming

Source: Internet
Author: User
Tags mdb database odbc stmt access database
I. Establishment of databases and ODBC data sources

1. Establish Jcc.mdb database and user table
2. Add test data
3. Configuring an ODBC data source

Two. Under <%wwwroot%>/, create a new Access database connection file select.jsp

Select.jsp source code is as follows:


<% @page contenttype= "text/html;charset=gb2312"%>
<% @page import= "java.sql.*"%>
<body>
<%
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
Out.print (e);
}
try{
String url = "JDBC:ODBC:JCC";
Connection conn = drivermanager.getconnection (URL, "JCC", "jsp.com.cn");
Statement stmt = Conn.createstatement ();
ResultSet rs = stmt.executequery ("Select * from user");
Out.println ("user-list" + "<br>");
while (Rs.next ()) {
Out.print (rs.getstring (1) + "");
Out.print (rs.getstring (2) + "<br>");
}
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception ex) {
Out.print (ex);
}
%>
</body>



Four. Run http://localhost/Select.jsp, display the results as follows:

User-list
1 corebit
2 Ivan

Indicates that the database connection was successful! Congratulations! Congratulations!

Otherwise, check the data source related settings, the likelihood of error is higher!

Postscript:

People often ask, how to do not do ODBC data source to let JSP access to the Access database, in order to unlock this mystery, close-up of the following connection code for reference! Among them, Jcc.mdb and select.jsp are located under the <%wwwroot%> (root directory).

After rewriting the select.jsp source code is as follows:


<% @page contenttype= "text/html;charset=gb2312"%>
<% @page import= "java.sql.*"%>
<body>
<%
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
Out.print (e);
}
try{
String Strdirpath=application.getrealpath (Request.getrequesturi ());
Strdirpath=strdirpath.substring (0,strdirpath.lastindexof (' \ \) ') + "\ \";
String url = "Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D bq=" +strdirpath+ "Jcc.mdb";
Connection conn = drivermanager.getconnection (URL);
Statement stmt = Conn.createstatement ();
ResultSet rs = stmt.executequery ("Select * from user");
Out.println ("user-list" + "<br>");
while (Rs.next ()) {
Out.print (rs.getstring (1) + "");
Out.print (rs.getstring (2) + "<br>");
}
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception ex) {
Out.print (ex);
}
%>
</body>



The running result should be the same as when using ODBC!

* Note: filename select.jsp case Sensitive!

Hopefully this article will help you with your JSP connection to the Access database!
==========================================
Only use the Jdbc-odbc bridge to connect
You want to set up an ODBC data source
And then connect
String dbdriver = "Oracle.jdbc.driver.OracleDriver";
String dbname = "JDBC:ORACLE:THIN:@192.168.0.101:1521:ORCL";//Modify according to your circumstances
String user = "System";//user name
String Password = "manager";//Password
Connection conn = null;
Statement stmt = null;
ResultSet RS =null;
String sql= "select * from table name";//modified according to the actual situation
Try
{
Class.forName (Dbdriver);
}
catch (Java.lang.ClassNotFoundException e) {
System.err.println ("Class Access_dbconnect not fount!" +e.getmessage ());
}
Conn=drivermanager.getconnection (Dbname,user,password);
Statement stmt=conn.createstatement ();
Rs=stmt.executequery (SQL);
=========================================
Sdbdriver = "Sun.jdbc.odbc.JdbcOdbcDriver";
sConnStr = "Jdbc:odbc:odbc name";
conn = null;
rs = null;
Try
{
Class.forName (Sdbdriver);
}
conn = Drivermanager.getconnection (SCONNSTR);
Statement Statement = Conn.createstatement ();
rs = Statement.executequery (s);
You build an Access connection in an ODBC data source, and then change the ODBC name in the above code to your ODBC data source connection name.
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.