Function with BEAN packages in JSP

xiaoxiao2021-03-06  47

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: ...

<%! string sql, name;%> <% sql = "select * from table"; resultset = sqlbean.executeQuery (sql); while (rs.nex ()) {name = rs.getstring ("name")}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}%> ... name: <% = name%> ... <% rs.close ();%> ...

转载请注明原文地址:https://www.9cbs.com/read-110345.html

New Post(0)