Implementing Jsp+javabean Access database

Source: Internet
Author: User
Tags getmessage odbc stmt access database

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" >
<

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.