JSP (JavaServer Pages) is advocated by Sun Microsystems, a dynamic web technology standard for many companies involved. With JSP technology, web page developers can use HTML or XML identity to design and format the final page. Use the JSP ID (TAG) or a Scriptle (Scriptlet) to generate dynamic content on the page. The logic of generating content is packaged in the identification and javabeans components, and bundled in a small script, all scripts run on the server.
JavaBeans is a Java-based software component, JavaBeans, and ActiveX controls, which can create a complete set of reusable object libraries through package business logic. JSP provides perfect support for integrating JavaBeans components in web applications, which not only shorten the development time (you can directly utilize test and trusted existing components), avoid duplication, but also bring more JSP applications. Multi-scalability. The JavaBeans component can be used to perform complex computing tasks or is responsible for interacting with the database, such as data extraction, etc., this article will be introduced in JSP to access customer information databases through JDBC-ODBC Bridge through JDBC-ODBC Bridge through an instance.
Database connection is the most important part of the dynamic website. The technology connecting the database in Java is JDBC (Java Database Connectivity). Many database systems have a JDBC driver, and the Java program is connected to the database through the JDBC driver, performing queries, extracting data, etc. Sun has also developed JDBC-ODBC Bridge. With this technology Java program, you can access databases with ODBC drivers. Most database systems have an ODBC driver, so Java programs can access such as Oracle, Sybase, MS SQL. Server and MS Access and other databases. The following describes how to implement a customer information query program with Access.
1. First create an Access database customers.mdb, where the table Customers has field ID (automatic increment, and set-to-primary keyword), name (text, length 10), address (text, length 30), INFO (Remarks).
2. Add the System DSN to the ODBC DataSource module of Control Panel, name Customers, and point to Customers.mdb.
3. Create a JavaBean named dbconn.java and save the default document root directory of the web server that supports JSP. Dbconn.java is mainly the connection operation of the package and the database, as follows:
Import java.sql. *;
Public clas dbconn {
StringDBDriver = "sun.jdbc.odbc.jdbcodbcdriver";
String connStr = "JDBC: ODBC: CUSTOMERS";
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 link with DBMS
Statement Stmt = conn.createstatement ();
RS = stmt.executequery (SQL);
}
Catch (Sqlexception EX) {
System.err.println ("Aq.executeQuery:" ex.getMessage ());
}
Return RS;
}
}
4, DBCONN.JAVA has been edited, in the DOS state, compile DBCONN.JAVA to form the corresponding class file with JDK's Javac command.
5. Create a Customers.jsp file, call the above-compiled JavaBeans in JSP, as follows:
"text / html; charSet = GB2312">
hEAD>
Customer Information Investigation B> P>
<% @ Page language = "java" import = "java.sql. *"%>
"Page" Class = "dbconn" /> <% ResultSet RS = DBCONN1.EXECUTEQUERY ("SELECT * from customers"); While (rs.next ()) { OUT.PRINT ("
Out.print ("
Out.print ("
}
Rs.close ();
%>
body>
html>
Several properties are defined within the
It turns out that JSP is an ideal architecture for developing web applications, using JavaBeans components that run across platforms, JSP provides excellent solutions for separation processing logic and display style.