📄 statementutil.java
字号:
package hong.javanet.dao;
import java.sql.*;
import java.util.*;
import org.apache.log4j.Logger;
/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author 洪桃李
* @version 1.0
*/
public class StatementUtil {
private static Logger log = Logger.getLogger(StatementUtil.class);
/**
* 所有连接的缓存语句集合的集合
*/
private static Map sessions = new HashMap();
/**
* 取得连接的缓存语句集合
* @param conn Connection
* @return Map
*/
private static Map getStatementCaches(Connection conn) {
Map statementCaches = (Map) sessions.get(conn);
if (statementCaches == null) {
statementCaches = new HashMap();
sessions.put(conn, statementCaches);
}
return statementCaches;
}
/**
* 清除连接的缓存语句集合
* @param conn Connection
* @throws SQLException
*/
public static void clearStatementCaches(Connection conn) throws SQLException {
Map statementCaches = (Map) sessions.get(conn);
if (statementCaches != null) {
sessions.remove(conn);
Iterator iter = statementCaches.entrySet().iterator();
while (iter.hasNext()) {
try {
Map.Entry item = (Map.Entry) iter.next();
Statement stat = (Statement) item.getValue();
stat.close();
}
catch (SQLException ex) {
log.fatal(ex.getMessage(), ex);
}
}
}
}
/**
* 创建语句
* @throws SQLException
* @return Statement
*/
public static Statement createStatement() throws SQLException {
Connection conn = ConnectionUtil.currentConnection();
Map statementCaches = getStatementCaches(conn);
Statement stat = (Statement) statementCaches.get("");
if (stat == null) {
stat = conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statementCaches.put("", stat);
}
return stat;
}
/**
* 获得当前连接的某个预编译的语句
* @param sql String
* @throws SQLException
* @return PreparedStatement
*/
public static PreparedStatement prepareStatement(String sql) throws
SQLException {
Connection conn = ConnectionUtil.currentConnection();
Map statementCaches = getStatementCaches(conn);
PreparedStatement stat = (PreparedStatement) statementCaches.get(sql);
if (stat == null) {
stat = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statementCaches.put(sql, stat);
}
return stat;
}
/**
* 获得当前连接的某个可调用的语句
* @param sql String
* @throws SQLException
* @return CallableStatement
*/
public static CallableStatement prepareCall(String sql) throws SQLException {
Connection conn = ConnectionUtil.currentConnection();
Map statementCaches = getStatementCaches(conn);
CallableStatement stat = (CallableStatement) statementCaches.get(sql);
if (stat == null) {
stat = conn.prepareCall(sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statementCaches.put(sql, stat);
}
return stat;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -