📄 dboption.java
字号:
package action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import setting.DBConnManager;
public class DBOption {
protected DBConnManager db = null;
protected String poolName = null;
protected Connection conn = null;
protected Statement stmt = null;
protected ResultSet rs = null;
public DBOption() {
this.db = DBConnManager.getInstance();
}
public void setPoolName(String name) {
this.poolName = name;
}
/**
* 当已经获得连接、执行状态后查询sql语句
* @param sql 查询的SQL语句
* @return ResultSet 结果集
* @throws SQLException 查询时的异常
*/
public ResultSet query(String sql) throws SQLException {
//获取连接
if (poolName == null || poolName.equals("")) {
Log.writeLog("action.DBOption.query:\n\t必须先指定连接池!");
return null;
}
this.conn = db.getConn(this.poolName);
this.stmt = conn.createStatement();
this.rs = this.stmt.executeQuery(sql);
db.freeConn(this.poolName,conn);
return rs;
}
public void update(String sql) throws SQLException {
if (poolName == null || poolName.equals("")) {
Log.writeLog("action.DBOption.update:\n\t必须先指定连接池!");
return;
}
this.conn = db.getConn(this.poolName);
this.stmt = conn.createStatement();
this.stmt.executeUpdate(sql);
this.stmt.close();
db.freeConn(this.poolName, conn);
}
public Connection getConn() {
this.conn = db.getConn(this.poolName);
return conn;
}
public Statement getStmt() throws SQLException {
if (this.conn == null) {
Log.writeLog("action.DBOption.getStmt:\n\t必须先指定连接池!");
return null;
}
this.stmt = conn.createStatement();
return stmt;
}
public void freeConn() {
if (this.conn != null) {
db.freeConn(this.poolName, conn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -