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 <JSP: usebean> tag, 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.