A JavaBean is easy to implement various operations for the database

zhaozj2021-02-16  100

Call instance

<% @ Page ContentType =

"text / html" import =

"OPERATE_DB. *, Java.sql. *"

%>

<%

String PRM1, PRM2, PRM3

ResultSet RST =

NULL;

PRM1 = ...

PRM2 = ...

PRM3 = ...

QueryBean.SetConnection

"Sun.jdbc.odbc.jdbcodbcdriver",

"JDBC: ODBC: Data Source Name",

"sa",

"" "

Try {

String [] param = {PRM1, PRM2, PRM3};

/ * Set SELECT statement * /

QueryBean.SetQueryStatement

"SELECT * from shop table where field1 =? And field2 =? And field3 =?");

QueryBean.SetParam (param);

RST = querybean.getresult ();

}

Catch (

Exception e) {

SYSTEM.

OUT.

Println (E);

While (RST.

NEXT ()) {

%> <% = rt.getstring (1)

%> <%}

%>

2.javabean querybean

QueryBean.java:

Pacakage operate_db;

/ **

* Title: SELECT INSERT UPDATE AND DELETE

* Description: SELECT INSERT UPDATE AND DELETE

* Copyright: CopyRight (C) 2001

* @Author lci21

* @version 1.0

* /

Import java.sql. *;

Public Class QueryBean

{

Public string query_statement; / * Define SQL statement * /

Public String param []; / * Query criteria, or new record * /

Public ResultSet Result = NULL;

Public connect conn;

Public void setParam (String [] Param)

{

This.Param = param;

}

Public void setQueryStatement (String query_statement)

{

THIS.QUERY_STATEMENT = query_statement;

}

Public void setConnection (String Drivername, String JDBCURL, STRING Username, String Passwd) THROWS EXCEPTION

{

CONNECTION CONN1;

Class.Forname (drivername);

CONN1 = DriverManager.getConnection (JDBCURL, Username, Passwd);

Conn1.setautocommit (false);

THIS.CONN = conn1;

}

/ * Get query results * /

Public ResultSet getResult () {

Try

{

PreparedState Select_stm = conn.preparestatement (query_statement, java.sql.resultSet.Type_scroll_insensitive, java.sql.resultset.concur_read_only);

IF (param! = null)

For (int i = 0; i

SELECT_STM.SETSTRING (i 1, param [i]);

Result = SELECT_STM.EXECUTEQUERY ();

} Catch (Exception E) {system.out.println (e);

Return Result;

}

/ * Add record operation on the database * /

Public void insertRecord () throws sqlexception, java.io.unsupportedEncodingexception

{

Try

{

PreparedStatement INSERT_STM = Conn.prepareStatement (query_statement);

IF (param! = null)

For (int i = 0; i

INSERT_STM.STSTRING (i 1, param [i]);

INSERT_STM.EXECUTEUPDATE ();

INSERT_STM.CLOSE ();

CONN.COMMIT ();

}

Catch (Exception E)

{

System.out.println (e);

CONN. ROLLBACK ();

}

}

/ * Change the data record * /

Public void updateRecord () THROWS SQLEXCEPTION, JAVA.IO.UNSUPPORTEDENCODINGEXCEPTION

{

Try

{

PreparedStatement Update_stm = conn.preparestatement (query_state);

IF (param! = null)

For (int i = 0; i

Update_stm.setstring (i 1, param [i]);

Update_stm.executeUpdate ();

Update_stm.close ();

CONN.COMMIT ();

}

Catch (Exception E)

{

System.out.println (e);

CONN. ROLLBACK ();

}

}

/ * Delete data record * /

Public void deleteRecord () THROWS SQLEXCEPTION, JAVA.IUNSUPPORTEDENCODINGEXCEPTION

{

Try

{

PreparedState delete_stm = conn.preparestatement (query_state);

IF (param! = null)

For (int i = 0; i

Delete_stm.setstring (i 1, param [i]);

Delete_stm.executeUpdate ();

Delete_stm.close ();

CONN.COMMIT ();

}

Catch (Exception E)

{

System.out.println (e);

CONN. ROLLBACK ();

}

}

}

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

New Post(0)