Java: Database Operation Package

xiaoxiao2021-03-06  14

Package creator.common.db;

Import Java.io.InputStream; import java.sql. *; import javax.sql. *; import javax.naming. *;

/ ** * *

Title: dbbean.java *

description: *

Copyright: CopyRight (c) 2004 *

Company: * @author TanBo * @version 1.0 * / public class DbBean {public java.sql.Connection conn = null; // connection object public ResultSet rs = null; // resultset object public Statement stmt = null; // statement object public PreparedStatement prepstmt = null; // preparedstatement object private String drivers = null; // connection parameter: drivers private String url = null; // connection parameter: url private String user = null; // connection parameter: user private String password = NULL; // Connection Parameter: Password Private String JNDI_NAME = NULL; // Connection Pool Parameter: JNDI Name

/ ** * init () * / public dbbean () {Try {// parameter init drivers = creator.config.configbundle.getstring ("drivers"); URL = Creat.config.configbundle.getstring ("URL"); User = creatle.config.configbundle.getstring ("user"); password = creat.Config.configbundle.getstring ("password"); jndi_name = Creat.config.configbundle.getstring ("JNDI_NAME"); "JNDI_NAME");

// DB Connection pool init // initialcontext env = new initialContext (); //javax.sql.datasource pool = (javax.sql.datasource) env.lookup (jndi_name); // conn = pool.getConnection ();

// DB Connection Init Class.Forname (drivers); conn = drivermanager.getConnection (URL, User, Password);

// DB Statement init stmt = conn.createstatement ();} catch (exception e) {system.out.println ("dbbean: init error!" e.tostring ());}} / ** * @function preparestatement * @Param SQL STRING * @THROWS SQLEXCEPTION * / Public Void PrepareStatement (String SQL) THROWS SQLEXCEPTION {prepStmt = conn.preparestatement (SQL);

/ ** * @function prepareStatement * @param sql String * @param resultSetType int * @param resultSetConcurrency int * @throws SQLException * / public void prepareStatement (String sql, int resultSetType, int resultSetConcurrency) throws SQLException {prepstmt = conn.prepareStatement ( SQL, RESULTSETTYPE, RESULTSETCONCURRENCY);

/ ** * @function executeQuery * @param sql String * @throws SQLException * @return ResultSet * / public ResultSet executeQuery (String sql) throws SQLException {if (stmt = null!) {Return stmt.executeQuery (sql);} else Return NULL;

/ ** * @function executeQuery * @throws SQLException * @return ResultSet * / public ResultSet executeQuery () throws SQLException {if (! Prepstmt = null) {return prepstmt.executeQuery ();} else return null;}

/ ** * @function executeUpdate * @param sql String * @throws SQLException * / public void executeUpdate (String sql) throws SQLException {if (stmt = null!) Stmt.executeUpdate (sql);}

/ ** * @function executeupdate * @throws sqlexception * / public void executeUpdate () THROWS SQLEXCEPTION {if (prepstmt! = Null) prePstmt.executeUpdate ()

/ ** * @function executeUpdate * @throws SQLException * / public void executeBatch () throws SQLException {if (prepstmt = null!) Prepstmt.executeBatch ();} / ** * @function addBatch * @param value String * @throws Sqlexception * / public void addbatch (string value) throws sqlexception {prepstmt.addbatch (value);}

/ ** * @function setString * @param index int * @param value String * @throws SQLException * / public void setString (int index, String value) throws SQLException {prepstmt.setString (index, value);}

/ ** * @function setint * @Param index int * @Param value int * @Throws SQLEXCEPTION * / PUBLIC VOID Setint (int index, int value) throws sqlexception {prepst.setint (index, value);}

/ ** * @function setBoolean * @param index int * @param value boolean * @throws SQLException * / public void setBoolean (int index, boolean value) throws SQLException {prepstmt.setBoolean (index, value);}

/ ** * @function setDate * @param index int * @param value Date * @throws SQLException * / public void setDate (int index, Date value) throws SQLException {prepstmt.setDate (index, value);}

/ ** * @function setLong * @param index int * @param value long * @throws SQLException * / public void setLong (int index, long value) throws SQLException {prepstmt.setLong (index, value);}

/ ** * @function setFloat * @param index int * @param value float * @throws SQLException * / public void setFloat (int index, float value) throws SQLException {prepstmt.setFloat (index, value);}

/ ** * @function setBytes * @param index int * @param value byte [] * @throws SQLException * / public void setBytes (int index, byte [] value) throws SQLException {prepstmt.setBytes (index, value);} / ** * @function setBinaryStream * @param index int * @param value InputStream * @param len int * @throws SQLException * / public void setBinaryStream (int index, InputStream value, int len) throws SQLException {prepstmt.setBinaryStream (index, Value, len);

/ ** * @function setTimestamp * @param index int * @param timestamp Timestamp * @throws SQLException * / public void setTimestamp (int index, Timestamp timestamp) throws SQLException {prepstmt.setTimestamp (index, timestamp);}

/ ** * @function setAutoCommit * @param value boolean * @throws SQLException * / public void setAutoCommit (boolean value) throws SQLException {if (this.conn = null!) This.conn.setAutoCommit (value);}

/ ** * @function commit * @throws sqlexception * / public void commit () throws sqlexception {this.conn.commit ();}

/ ** * @function rollback * @throws sqlexception * / public void rollback () throws sqlexception {this.conn.rollback ();

/ ** * @function close * @throws exception * / public void close () {r {i (= null) {r.close (); rs = null;}} catch (exception e) {system.out .println ("DBBean Close Rs Error!");} finally {try {if (stmt! = null) {stmt.close (); stmt = null;}} catch (exception e) {system.out.println (" DBBean Close Stmt Error! ");} Finally {Try {if (prepStmt! = null) {prepstmt.close (); prepstmt = null;}} catch (Exception E) {system.out.println (" DBBean Close Prepstmt Error ! ");} Finally {try {if (conn! = Null) {conn = null;}} catch (exception e) {system.out.println (" DBBean Close Conn Error! "); }}}}}}}}

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

New Post(0)