1. Create a database and ODBC Data Source
1. Create a jcc. mdb database and user table
2. Add Test Data
3. Configure the ODBC Data Source
2. Under <% wwwroot %>/, create the Access database connection file Select. jsp.
Select. jsp source code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<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.exe cuteQuery ("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>
</Html>
4. Run http: // localhost/Select. jsp. The result is as follows:
User-list
1 Corebit
2 Ivan
The database connection is successful! Congratulations! Congratulations!
Otherwise, check the data source settings, and the error possibility is high!
Postscript:
It is often asked how to allow JSP to Access the Access database without using ODBC data sources. To solve this problem, close up the following connection code for your reference! Jcc. mdb and Select. jsp are in the same directory as <% wwwroot %>.
The source code of the modified Select. jsp file is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<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)}; DBQ =" + strDirPath + "jcc. mdb ";
Connection conn = DriverManager. getConnection (url );
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("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>
</Html>
The running result should be the same as that when ODBC is used!
* Note: The file name Select. jsp is case sensitive!
I hope this article will be helpful for connecting your JSP to the Access database!
========================================================== =
Only jdbc-odbc bridges can be used for connection.
Want to set odbc Data Source
Connect
String dbdriver = "oracle. jdbc. driver. OracleDriver ";
String dbname = "jdbc: oracle: thin: @ 192.168.0.101: 1521: orcl"; // modify according to your situation
String user = "system"; // user Name
String password = "manager"; // password
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String SQL = "select * from table name"; // modify 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 ();
Rs1_stmt.exe cuteQuery (SQL );
========================================================== =
SDBDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
SConnStr = "jdbc: odbc name ";
Conn = null;
Rs = null;
Try
{
Class. forName (sDBDriver );
}
Conn = DriverManager. getConnection (sConnStr );
Statement statement = conn. createStatement ();
Rs = statement.exe cuteQuery (s );
Create an access connection in the odbc data source, and change the odbc name in the above Code to your odbc data source connection name.