📄 dbwrapper.java
字号:
package com.infobank.monitoringSystem.util;
import java.io.InputStream;
import java.io.StringReader;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
/**
* 数据库连接
*
* @author Noh, Jeong-oh
* @update Resine Connection Pool
*/
public class DBWrapper {
private static String dbName;
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private CallableStatement cstmt;
private ResultSet result;
private ResultSet pResult;
public DBWrapper() {
}
/**
* 取得Connection
* @param
* @return boolean
*/
public boolean getConnection() {
try {
conn = ConFactory.getInstance().getConnection();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 关闭连接
* @param
* @return
* @see java.sql.Connection
*/
public void close() {
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (cstmt != null)
try {
cstmt.close();
} catch (Exception e) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println("DBWrapper Exception : '"+e.toString()+"'");
}
stmt = null;
pstmt = null;
cstmt = null;
conn = null;
return;
}
public void close(Exception e) {
System.out.println(e.getMessage());
return;
}
public void closeStatement() {
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (Exception e) {
}
if (cstmt != null)
try {
cstmt.close();
} catch (Exception e) {
}
}
/**
* 运行SQL并且返回Result
* @param select_query
* @return ResultSet
* @see java.sql.Connection
* @see java.sql.Statement
*/
public ResultSet executeQuery(String select_query) {
try {
if (stmt == null)
stmt = conn.createStatement();
result = stmt.executeQuery(select_query);
return (ResultSet) result;
} catch (SQLException e) {
close(e);
}
return (ResultSet) null;
}
public ResultSetMetaData getMetaData() {
try {
return (ResultSetMetaData) result.getMetaData();
} catch (SQLException e) {
close(e);
}
return null;
}
public int executeUpdate(String update_query) {
int rt;
try {
if (result != null)
result.close();
if (stmt == null)
stmt = conn.createStatement();
//System.out.println(" Query in DBWrapper.executeUpdate : " + update_query);
rt = stmt.executeUpdate(update_query);
} catch (SQLException e) {
close(e);
rt = -1;
}
return (int) rt;
}
public void setAutoCommit(boolean autoCommit) {
try {
conn.setAutoCommit(autoCommit);
return;
} catch (SQLException e) {
close(e);
}
}
public void commit() {
try {
conn.commit();
return;
} catch (SQLException e) {
close(e);
}
}
public void rollback() {
try {
conn.rollback();
} catch (SQLException e) {
close(e);
}
}
public void addBatch(String query) {
try {
if (stmt == null)
stmt = conn.createStatement();
stmt.addBatch(query);
} catch (SQLException e) {
close(e);
}
}
/**
* 汗荐俺狼 query甫 老褒荐青窍绊 弊 Update搬苞甫 int[]屈侥栏肺 府畔茄促.
* @param
* @return int[]
* @see java.sql.Statement
*/
public int[] executeBatch() {
int resultBatch[] = null;
try {
resultBatch = stmt.executeBatch();
} catch (SQLException e) {
close(e);
}
return resultBatch;
}
/**
* 设置PreparedStatement
* @param pQuery
* @return
* @see java.sql.Connection
* @see java.sql.PreparedStatement
*/
public void prepareStatement(String pQuery) {
try {
if (pstmt != null)
pstmt.close();
pstmt = conn.prepareStatement(pQuery);
} catch (SQLException e) {
close(e);
}
return;
}
/**
* @param pQuery
* @return
* @see java.sql.Connection
* @see java.sql.PreparedStatement
*/
public void prepareCall(String cQuery) {
try {
if (cstmt != null)
cstmt.close();
cstmt = conn.prepareCall(cQuery);
} catch (SQLException e) {
close(e);
}
return;
}
/**
* @param
* @return
* @see java.sql.Connection
* @see java.sql.PreparedStatement
*/
public ResultSet executeQuery() {
ResultSet result = null;
try {
result = pstmt.executeQuery();
} catch (SQLException e) {
close(e);
return null;
}
return result;
}
public int executeUpdate() {
int rt;
try {
if (pResult != null)
pResult.close();
rt = pstmt.executeUpdate();
} catch (SQLException e) {
close(e);
rt = -1;
}
return (int) rt;
}
public int executeCallalbleUpdate() {
int rt;
try {
rt = cstmt.executeUpdate();
} catch (SQLException e) {
close(e);
rt = -1;
}
return (int) rt;
}
/**
* 设置参数
* Converts
* @param parameterIndex .
* @return
* @see java.sql.PreparedStatement
*/
public void setString(int parameterIndex, String x) {
try {
pstmt.setString(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setInt(int parameterIndex, int x) {
try {
pstmt.setInt(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setLong(int parameterIndex, long x) {
try {
pstmt.setLong(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setDouble(int parameterIndex, double x) {
try {
pstmt.setDouble(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setFloat(int parameterIndex, float x) {
try {
pstmt.setFloat(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setDate(int parameterIndex, Date x) {
try {
pstmt.setDate(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setTime(int parameterIndex, Time x) {
try {
pstmt.setTime(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setObject(int parameterIndex, Object x) {
try {
pstmt.setObject(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setObject(int parameterIndex, Object x, int sqlType) {
try {
pstmt.setObject(parameterIndex, x, sqlType);
} catch (SQLException e) {
close(e);
}
return;
}
public void setByte(int parameterIndex, byte x) {
try {
pstmt.setByte(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void setBytes(int parameterIndex, byte[] x) {
try {
pstmt.setBytes(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
/**
* @see java.sql.PreparedStatement
*/
public void setClob(int parameterIndex, Clob x) {
try {
pstmt.setClob(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
/**
* @return
* @see java.sql.PreparedStatement
*/
public void setBlob(int parameterIndex, Blob x) {
try {
pstmt.setBlob(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
/**
* @param int parameterIndex, InputStream x, int length
* @return
* @see java.sql.PreparedStatement
*/
public void setBinaryStream(
int parameterIndex,
InputStream x,
int length) {
try {
pstmt.setBinaryStream(parameterIndex, x, length);
} catch (SQLException e) {
close(e);
}
return;
}
/**
DB Name
@param dbName DB
*/
public void setDBName(String setDbName) {
dbName = setDbName;
}
/**
@return dbName String
*/
public String getDBName() {
return dbName;
}
/**
* null 设定
* @param parameterIndex
* @param x
*/
public void setNull(int parameterIndex, int x) {
try {
pstmt.setNull(parameterIndex, x);
} catch (SQLException e) {
close(e);
}
return;
}
public void clearParameter() {
try {
pstmt.clearParameters();
} catch (SQLException e) {
close(e);
}
return;
}
public void addBatchForPrepared(){
try {
pstmt.addBatch();
} catch (SQLException e) {
close(e);
}
return;
}
public int[] executeBatchForPrepared(){
try {
return pstmt.executeBatch();
} catch (SQLException e) {
close(e);
}
return null;
}
public void setCharacterStream(int parameterIndex, String x) {
try {
int length = x.getBytes().length;
if (length > 1500) {
StringReader reader = new StringReader(x);
pstmt.setCharacterStream(parameterIndex, reader, length);
} else {
pstmt.setString(parameterIndex, x);
}
} catch (SQLException e) {
close(e);
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -