Detailed description of JSP database access methods

Source: Internet
Author: User

JSP is a dynamic web page technical standard proposed by Sun and established by many companies. With JSP technology, Web page developers can use HTML or XML tags to design and format the final page. Use JSP tags or Scriptlet to generate dynamic content on the page. The logic of the generated content is encapsulated in the identifiers and JavaBeans components and bundled in small scripts. All scripts run on the server.

Database connection is the most important part for dynamic websites. The connection technology in Java is JDBC (Java Database Connectivity ). Many database systems have JDBC drivers. Java programs connect to the database through the JDBC driver and perform operations such as querying and extracting data. Sun also developed JDBC-ODBC bridge, with this technology Java program can access the database with ODBC driver, currently most database systems with ODBC driver, therefore, Java programs can Access databases such as Oracle, Sybase, ms SQL Server, and MS Access. This article uses an example to introduce the use of JavaBeans in JSP to Access the customer information database through JDBC-ODBC Bridge.

1. first, create an Access database Customers. mdb, in which the table Customers has the field id (automatic incremental type, and set as the primary keyword), name (text type, length 10), address (text type, length 30), info (Remark type ).

2. Add System DSN to the ODBC Datasource module of the Control Panel, name MERs, and point to Customers. mdb.

3. Create a JavaBeans named DBconn. java and save it in the default document root directory of the Web server that supports JSP. DBconn. java is mainly used to encapsulate connection operations with databases. The content is as follows:

Import java. SQL .*;
Public class DBconn {
String DBDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String ConnStr = "jdbc: odbc: MERs ";
Connection conn = null;
ResultSet rs = null;
Public DBconn {
Try {
Class. forName (DBDriver );
// Load the database driver
}
Catch (java. lang. ClassNotFoundException e ){
System. err. println ("DBconn ():" + e. getMessage ());
}
}
Public ResultSet executeQuery (String SQL ){
Rs = null;
Try {
Conn = DriverManager. getConnection (ConnStr );
// Establish a connection with the DBMS
Statement stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery (SQL );
}
Catch (SQLException ex ){
System. err. println ("aq.exe cuteQuery:" + ex. getMessage ());
}
Return rs;
}
}

4. After DBconn. java is edited, use the javac command of JDK to compile DBconn. java in the DOS state to form the corresponding class file.

5. Create the Customers. jsp file and call the compiled ans in JSP. The content is as follows:

<Html> 〉
<Head> 〉
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> "〉
<Title> Customer Information Survey </title> 〉
</Head> 〉
<Body> 〉
<P> <B> Customer Information Survey </B> </p> 〉
<% @ Page language = "java" import = "java. SQL. *" %> 〉
<Jsp: useBean id = "DBconn1" scope = "page" class = "DBconn"/> "/〉
<%
ResultSet RS = DBconn1.executeQuery ("SELECT * FROM MERs ");
While (RS. next ()){
Out. print ("<LI>" + RS. getString ("name") + "</LI> 〉");
Out. print ("<LI>" + RS. getString ("address") + "</LI> 〉");
Out. print ("<LI>" + RS. getString ("info") + "</LI> 〉");
}
RS. close ();
%> 〉
</Body> 〉
</Html> 〉

Several attributes are defined in the tag "jsp: useBean, the id is the Bean identifier on the JSP page, the scope attribute defines the Bean's survival time, and the class attribute specifies the Bean class file.

It turns out that JSP is an ideal framework for developing Web applications. Using the cross-platform running JavaBeans component, JSP provides an excellent solution for separating processing logic and display styles.

 

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.