First, create the database.
We create an Access database Student.mdb and use the Table Designer to create a table called Table1, and add test data as follows:
Second, configure the data source.
Select Start-> Administrative Tools-> Data Source (ODBC) to eject the following window:
Select the Add button on the right, and select the item selected in the following figure:
Click "Finish" to eject the following dialog box:
At Data source (N), fill in the original name of the data you want to configure student, you can add "description (D)", and then click "Select (S)", pop-up the following dialog box:
Select the location of the database Student.mdb that we created earlier, as follows:
When you find the location of the Student.mdb, you must pay attention to select the "data originally (A)" Under the selected Student.mdb, so that the original figure in the *.mdb will become Student.mdb, then click the "OK" button, and then determine the completion of the data source configuration.
then, set up the directory and write code.
The directory hierarchy is as follows (see where I use the red box):
1. Write the JavaBean program that accesses the Access database Conn.java, the code is as follows: Package student;
Import java.sql. * ;
public class Conn ... {
String sdbdriver= "Sun.jdbc.odbc.JdbcOdbcDriver";
String sconnstr= "Jdbc:odbc:student";
Connection Connect=null;
ResultSet Rs=null;
Public Conn () ... {
Try ... {
Class.forName (Sdbdriver); Registering drivers
}catch (java.lang.ClassNotFoundException e) ... {
System.err.println (E.getmessage ());
}
}
Public ResultSet executequery (String sql) ... {//Execute Query Database operations method
Try ... {
Connect=drivermanager.getconnection (SCONNSTR); Connect to an Access database
Statement stmt=connect.createstatement (); Create a statement object to submit a query statement to a database
Rs=stmt.executequery (SQL); Save query results in the result set RS
}catch (SQLException ex) ... {
System.err.println (Ex.getmessage ());
}
Return RS;
}
}
After compiling, get the Conn.class file in the following directory (take my path for example):
D:/program Files/apache Software Foundation/tomcat 5.5/webapps/test/web-inf/classes/student
2. Write the JSP source file that implements access to JavaBean, the code is as follows: <% ... @ Page language= "Java"%>
<% ... @ Page import= "java.sql.*"%>
<% ... @ Page contenttype= "text/html;charset=gb2312"%>
< Jsp:usebean ID = "Conndbbean" scope = "page" class = "Student.conn"/>
< HTML >
< head >
< title >--test DB connection--</title >
< body bgcolor = "#ffffff" >
< div align = "center" >
< table width = "60%" border = "1" >
< tr bgcolor = "#ccccff" >
< td width = "50%" >
< div align = "center" >
< font color = "#ff0033" >< B > name </b ></font >
</div >
</td >
< td width = "25%" >
< div align = "center" >
< font color = "#ff0033" >< B > Sex </b ></font >
</div >
</td >
< td width = "25%" >
< div align = "center" >
<