A JavaBean is easy to implement various operations for the database

xiaoxiao2021-03-06  107

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.jdbcodbcodbcdriver", "JDBC: ODBC: Data Source Name", "SA", "");

Try {

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

/ * Set SELECT statement * /

QueryBean.SetQueryStatement ("SELECT * from Table Where Field1 =? and field2 =? and field3 =?");

QueryBean.SetParam (param);

RST = querybean.getresult ();

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

While (Rst.next ()) {

%> <% = rst.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

{

PreparedStatement 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 ();

}

}

}

I hope everyone respects the author's labor. When using the author information and version information, thank you!

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

New Post(0)