1, build a bean
Package guestbook; import java.sql. *; / *** Title: Use the JavaBean connection database * Description: Use beans to encapsulate commonly used database function, declare the bean in JSP. * Copyright: Copyright: Copyright (C) 2001 * Company: Extreme Network Group * Editor: Speed Network Group * Version 1.0 * / Public Class SQL_Data {String SDBDriver = "Sun.jdbc.odbc.jdbCodbcDriver"; string sconnstr = "JDBC: ODBC: guestbook "; Connection conn = null; ResultSet rs = null; public sql_data () {try {class.forName (sDBDriver);} catch (java.lang.ClassNotFoundException e) {System.err.println (" sql_data (): " E.getMessage ());}}
public void executeInsert (String sql) {try {conn = DriverManager.getConnection (sConnStr); Statement stmt = conn.createStatement (); stmt.executeUpdate (sql);} catch (SQLException ex) {System.err.println ( "sql_data .executeUpdate: " ex.getMessage ());}}
public ResultSet executeQuery (String sql) {rs = null; try {conn = DriverManager.getConnection (sConnStr); Statement stmt = conn.createStatement (); rs = stmt.executeQuery (sql);} catch (SQLException ex) {System. Err.Println ("SQL_Data.executeQuery:" ex.getMessage ());}}
public void executeDelete (String sql) {try {conn = DriverManager.getConnection (sConnStr); Statement stmt = conn.createStatement (); stmt.executeUpdate (sql);} catch (SQLException ex) {System.err.println ( "sql_data .executedelete: " ex.getMessage ());}}}
2, declaration in JSP Bean: ...