A detailed explanation of JSP access to the database method

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

JSP (JavaServer pages) is a Dynamic Web page technology standard initiated by Sun company and participated by many companies. Using JSP technology, Web page developers can use HTML or XML identities to design and format final pages. Use the JSP ID (tag) or the scriptlet to generate dynamic content on the page. The logic for generating content is encapsulated in identity and JavaBeans components, and bundled in small scripts, all scripts run on the server side.

A database connection is the most important part of a dynamic web site, and the technology for connecting to the database in Java is JDBC (Java Database Connectivity). Many database systems have JDBC drivers, and Java programs connect to the database through the JDBC driver, executing queries, fetching data, and so on. Sun also developed JDBC-ODBC Bridge, which uses Java programs to access databases with ODBC drivers, and most database systems now have ODBC drivers, so Java programs can access Oracle, Sybase, MS SQL Databases such as server and MS Access. This article presents an example of using JavaBeans to access the Access customer information database through JDBC-ODBC Bridge in JSP.

1. First create an Access database Customers.mdb, where the table customers has the field ID (AutoIncrement, and set as the primary key), name (text, length 10), address (text type, length 30), info (memo type).

2. In the Control Panel, in the ODBC DataSource module, add system DSN, name customers, 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 to encapsulate the connection operation with the database, the contents are as follows:

Import java.sql.*;
public class Dbconn {
String dbdriver = "Sun.jdbc.odbc.JdbcOdbcDriver";
String connstr = "Jdbc:odbc:Customers";
Connection conn = null;
ResultSet rs = null;
Public Dbconn {
try {
Class.forName (Dbdriver);
Loading 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);
Establishing a connection with a DBMS
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (SQL);
}
catch (SQLException ex) {
System.err.println ("Aq.executequery:" + ex.getmessage ());
}
Return RS;
}
}

4.dbconn.java Editor, in the DOS state, and then the JDK javac command compiled Dbconn.java form the corresponding class file.

5. Establish the customers.jsp file, in the JSP call the above compiled JavaBeans, its contents are 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 Customers");
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〉

Within the 〈jsp:usebean〉 tag, several properties are defined, where the ID is the identity of the bean within the entire JSP page, the scope property defines the lifetime of the bean, and the class property describes the bean.

As it turns out, JSP is an ideal framework for developing Web applications, using JavaBeans components that run across platforms, and JSP provides an excellent solution for separating processing logic and display styles.

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.