Java connection database class dbsource

xiaoxiao2021-03-05  22

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

// Import com.vfrankmusic.sysinfo.sysinfo;

/ ** *

Title: MusicWeb *

Description: MusicWeb Site *

Copyright: Copyright (c) 2004 *

Company: vfranksoft * @author Wu Xiaolong * @version 1.0 * /

public class DBSource {public Connection conn = null; public ResultSet rs = null; public Statement stmt = null; public PreparedStatement pstmt = null; public DataSource ds = null; private static DBSource instance; private String drivers = null; private String url = null ; private String user = null; private String password = null; private String jndi = null; public synchronized static DBSource getInstance () {if (instance == null) {instance = new DBSource ();} return instance;} private DBSource ( ) {Try {INITIALCONText (); // DS = (DataSource) CTX.lookup ("java: comp / env / jdbc / sqlserver); DS = (DataSource) CTX.lookup (" Java: Comp / Env / jdbc / mysql "); conn = ds.getConnection (); stmt = conn.createstatement ();} catch (exception ex) {system.out.println (" Excellent Exception, information is: " ex. maxmessage )); Ex.printstacktrace ();}} / ** * @function prepareState * @Param SQL * @Throws SQLEXCESTITION * / PUBLIC VOID PREPARESTATEMENT (STRING SQL) THROWS SQLEXCEPTION {Pstmt = conn.prepareStatement (sql);} / ** * * @param sql * @param resultSetType * @param resultSetConcurrency * @throws SQLException * / public void prepareStatement (String sql, int resultSetType, int resultSetConcurrency) throws SQLException {pstmt = conn.prepareStatement (sql, resultSetType, resultSetConcurrency);} // query / ** * * @param sql * @return * @throws SQLException * / public ResultSet executeQuery (String sql) throws SQLException {if (stmt = null!) {Return Stmt.executeQuery (SQL);} else return null;} / ** * * @return * @

throws SQLException * / public ResultSet executeQuery () throws SQLException {if (pstmt = null!) {return pstmt.executeQuery ();} else return null;} // Update / ** * * / public void executeUpdate (String sql) throws SQLException {if (stmt! = Null) {stmt.executeUpdate (sql);}}

/ ** * @Throws Sqlexception * / public int executeUpdate () throws SQLEXCEPTION {INT VALUE = 0; if (pstmt! = Null) {value = pstmt.executeUpdate ();} Return Value;} // ** * * @throws SQLException * / public void executeBatch () throws SQLException {if (pstmt = null!) pstmt.executeBatch ();} / ** * * @param value * @throws SQLException * / public void addBatch (String value) throws SQLException {pstmt.addBatch ();} // public void setString (int index, String value) throws SQLException {pstmt.setString (index, value);} public void setInt (int index, int value) throws SQLException {pstmt.setInt ( index, value);} public void setBoolean (int index, boolean value) throws SQLException {pstmt.setBoolean (index, value);} public void setDate (int index, Date value) throws SQLException {pstmt.setDate (index, value) Public void setlong (int index, long value) throws sqlexception {pstmt.setlong (index, value);} public void setfloat (int index, float value) throws SQLE xception {pstmt.setFloat (index, value);} public void setBeytes (int index, byte [] value) throws SQLException {pstmt.setBytes (index, value);} public void setBinaryStream (int index, InputStream value, int len) throws SQLException {pstmt.setBinaryStream (index, value, len);} public void setTimestamp (int index, timestamp timestamp) throws SQLException {pstmt.setTimestamp (index, timestamp);} // transaction public void setAutoCommit (boolean value) throws SQLException {If (this.conn! = Null) this.conn.setautoCommit (value); public void commit () throws sqlexception {this.conn.commit ();} public void roolback () throws sqlexception {this.conn.rollback (); Public void close () {try {= (rs! =

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

New Post(0)