📄 sqldboperator.java
字号:
package com.ntsky.database;
import com.ntsky.common.Debug;
import com.ntsky.common.EnvironmentConfig;
import com.ntsky.pool.DBConnectionManager;
import java.sql.*;
import java.util.Properties;
public class SQLDBOperator extends DBOperator
{
private DBConnectionManager conManager;
private String poolName;
private PreparedStatement prepstmt;
private Connection conn;
private static SQLDBOperator instance;
private long lifeTime;
private long currentTimeFirst;
private Properties dbProps = null;
private SQLDBOperator(String strKey)
{
dbProps = EnvironmentConfig.getInstance().getProperties("/SiteConfig/db.properties");
lifeTime = Long.parseLong(dbProps.getProperty("Connection.lifeTime"));
prepstmt = null;
poolName = strKey;
conManager = DBConnectionManager.getInstance();
conn = conManager.getConnection(strKey);
currentTimeFirst = System.currentTimeMillis();
}
public void Close()
{
try
{
conManager.freeConnection(poolName,conn);
}catch(Exception e){
e.printStackTrace(System.out);
}
}
public void executeUpdate(String strSql)
{
try
{
strSql = new String(strSql.getBytes("GBK"), "ISO8859_1");
Statement stmt = conn.createStatement();
long currentTime = System.currentTimeMillis();
if(currentTime-currentTimeFirst>=lifeTime*1000){
String user = dbProps.getProperty("Connection.user");
String password = dbProps.getProperty("Connection.password");
String url = dbProps.getProperty("Connection.url");
conManager.deleteConnection("Connection",conn);
//conn = conManager.getConnection("Connection");
conn = DriverManager.getConnection(url, user, password);
currentTimeFirst = currentTime;
stmt = null;
stmt = conn.createStatement();
}
stmt.executeUpdate(strSql);
}catch(Exception e){
e.printStackTrace(System.out);
Debug.writeLog("In executeUpdate(String), Exception Occured ! Info :" + e.getLocalizedMessage());
}
}
public ResultSet executeQuery(String strSql)
{
ResultSet rs = null;
try
{
strSql = new String(strSql.getBytes("GBK"), "ISO8859_1");
Statement stmt = conn.createStatement();
long currentTime = System.currentTimeMillis();
if(currentTime-currentTimeFirst>=lifeTime*1000){
String user = dbProps.getProperty("Connection.user");
String password = dbProps.getProperty("Connection.password");
String url = dbProps.getProperty("Connection.url");
conManager.deleteConnection("Connection",conn);
//conn = conManager.getConnection("Connection");
conn = DriverManager.getConnection(url, user, password);
currentTimeFirst = currentTime;
stmt = null;
stmt = conn.createStatement();
}
rs = stmt.executeQuery(strSql);
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In executeQuery(String), Exception Occured ! Info :" + e.getLocalizedMessage());
}
return rs;
}
public void prepareStatement(String strSql)
{
try {
long currentTime = System.currentTimeMillis();
if(currentTime-currentTimeFirst>=lifeTime*1000){
String user = dbProps.getProperty("Connection.user");
String password = dbProps.getProperty("Connection.password");
String url = dbProps.getProperty("Connection.url");
conManager.deleteConnection("Connection",conn);
//conn = conManager.getConnection("Connection");
conn = DriverManager.getConnection(url, user, password);
currentTimeFirst = currentTime;
}
prepstmt = conn.prepareStatement(strSql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void setString(int index, String value)
{
try
{
value = new String(value.getBytes("GBK"), "ISO8859_1");
prepstmt.setString(index, value);
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In setString(int index,String value), Exception Occured ! Info :" + e.getLocalizedMessage());
}
}
public void setInt(int index, int value)
{
try
{
prepstmt.setInt(index, value);
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In setInt(int index,int value), Exception Occured ! Info :" + e.getLocalizedMessage());
}
}
public void clearParameters()
{
try
{
prepstmt.clearParameters();
prepstmt = null;
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In clearParameters(), Exception Occured ! Info :" + e.getLocalizedMessage());
}
}
public PreparedStatement getPreparedStatement()
{
return prepstmt;
}
public void executeUpdate()
{
try
{
if(prepstmt != null){
prepstmt.executeUpdate();
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In executeUpdate(String), Exception Occured ! Info :" + e.getLocalizedMessage());
}
}
public ResultSet executeQuery()
{
ResultSet rs = null;
try
{
if(prepstmt != null){
rs = prepstmt.executeQuery();
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
Debug.writeLog("In executeQuery(String), Exception Occured ! Info :" + e.getLocalizedMessage());
}
return rs;
}
public Connection getConnection()
{
return conn;
}
public void setConnection(Connection conn)
{
this.conn = conn;
}
public static SQLDBOperator getInstance(String strKey)
{
if(instance == null)instance = new SQLDBOperator(strKey);
return instance;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -