📄 dbconn.java
字号:
/*
* Created on 2003/10/30
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.leeman.common.db;
import java.sql.*;
import java.util.GregorianCalendar;
/**
* @author dennis
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DbConn{
private AbstractDBConnector dbConnector;
public Connection conn;
public PreparedStatement pstmt;
public boolean traceOn;
public Statement stmt;
public DbConn() throws Exception
{
setTraceOnOff();
getConnection();
}
public DbConn(String sDBType) throws Exception
{
setTraceOnOff();
getConnection(sDBType);
}
public DbConn(String sDBType, String db_address, String db_user, String db_password) throws Exception
{
setTraceOnOff();
getConnection(sDBType, db_address, db_user, db_password);
}
private void setTraceOnOff()
{
traceOn = true;
}
public void trace(String log)
{
if(traceOn)
{
System.out.println(log);
}
}
public String getSDBType()
{
return dbConnector.getSDBType();
}
private void getConnection(String sDBType) throws Exception
{
//trace("getConnection()");
DBConnectorFactory dbf = new DBConnectorFactory();
dbConnector = dbf.getConnector(sDBType);
conn = dbConnector.conn;
dbf = null;
}
private void getConnection(String sDBType, String db_address, String db_user, String db_password) throws Exception
{
DBConnectorFactory dbf = new DBConnectorFactory();
dbConnector = dbf.getConnector(sDBType, db_address, db_user, db_password);
conn = dbConnector.conn;
dbf = null;
}
private void getConnection() throws Exception
{
//trace("getConnection()");
DBConnectorFactory dbf = new DBConnectorFactory();
dbConnector = dbf.getConnector();
conn = dbConnector.conn;
dbf = null;
}
public void commitTrans() throws SQLException
{
if (!dbConnector.sDBType.equals(DBConnectorFactory.WEBSPHERE_CONNECTOR))
{
trace("commitTrans()");
conn.commit();
}
else
{
trace("bypass commitTrans()");
}
}
public void rollbackTrans() throws SQLException
{
if (!dbConnector.sDBType.equals(DBConnectorFactory.WEBSPHERE_CONNECTOR))
{
trace("rollbackTrans()");
conn.rollback();
}
else
{
trace("bypass rollbackTrans()");
}
}
public void prepareStatement(String sql) throws SQLException
{
trace("prepareStatement():" + sql);
pstmt = conn.prepareStatement(sql);
}
public boolean executeNonQueryWithPstmt() throws SQLException
{
trace("executeNonQueryWithPstmt()");
boolean result = pstmt.execute();
return result;
}
public int executeUpdateWithPstmt() throws SQLException
{
trace("executeUpdateWithPstmt()");
int result = pstmt.executeUpdate();
return result;
}
public ResultSet executeQueryWithPstmt() throws SQLException
{
trace("executeQueryWithPstmt()");
ResultSet rs = pstmt.executeQuery();
return rs;
}
public void closePreparedStatement() throws SQLException
{
trace("closePreparedStatement()");
if (pstmt != null){
pstmt.close();
pstmt = null;
}
}
public void setAutoCommit(boolean isAutoCommit) throws SQLException
{
if (!dbConnector.sDBType.equals(DBConnectorFactory.WEBSPHERE_CONNECTOR))
{
trace("setAutoCommit(): " + (isAutoCommit?"true":"false") );
conn.setAutoCommit(isAutoCommit);
}
else
{
trace("bypass setAutoCommit(): " + (isAutoCommit?"true":"false") );
}
}
public ResultSet executeQuery(String sql) throws SQLException
{
trace("executeQuery(): " + sql );
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
public boolean executeNonQuery(String sql) throws SQLException
{
boolean result = false;
trace("executeNonQuery(): " + sql );
try{
stmt = conn.createStatement();
result = stmt.execute(sql);
result = true;
}
catch(SQLException sqlE)
{
result = false;
throw sqlE;
}
finally{
if (stmt!= null) stmt.close();
}
return result;
}
public int executeUpdate(String sql) throws SQLException
{
int result = 0;
trace("executeUpdate(): " + sql );
try{
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException sqlE)
{
throw sqlE;
}
finally{
if (stmt!= null) stmt.close();
}
return result;
}
public void closeQuery() throws SQLException
{
if (stmt!=null){
stmt.close();
stmt = null;
trace("closeQuery()");
}
}
public boolean isClosed() throws SQLException
{
if (conn!=null)
{
return conn.isClosed();
}
else
{
return true;
}
}
public void closeConnection()
{
try{
if (conn!=null)
{
if(!conn.isClosed())
{
conn.close();
//trace("closeConnection(): close normal");
}
}
dbConnector = null;
}catch(SQLException sqlE){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -