📄 trancontext.java
字号:
package com.laoer.bbscs.db;
import java.sql.*;
import java.util.StringTokenizer;
import java.io.*;
import java.util.Properties;
import com.laoer.bbscs.sysinfo.*;
public class TranContext {
public Connection conn;
private Statement stmt = null;
boolean isAutoCommit;
DBConnectionManager connMgr = null;
String databasename = null;
private PreparedStatement prepstmt = null;
public TranContext(boolean c) throws SQLException {
initConnection();
if (c) {
stmt = this.conn.createStatement();
}
}
public TranContext() {
//initPro();
initConnection();
}
public TranContext(String databasename) {
this.databasename = databasename;
initConnection();
}
public TranContext(Connection conn) throws SQLException {
this.conn = conn;
//stmt = this.conn.createStatement();
}
public TranContext(Connection conn, boolean isScrollSensitive) throws
SQLException {
this.conn = conn;
if (isScrollSensitive == false) {
this.stmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else {
this.stmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
}
public TranContext(Connection conn, String sql) throws SQLException {
this.conn = conn;
this.prepareStatement(sql);
}
public TranContext(Connection conn, String sql, boolean isScrollSensitive) throws
SQLException {
this.conn = conn;
if (isScrollSensitive == false) {
this.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else {
this.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
}
public void initPro() {
Properties prop = new Properties();
try {
InputStream is = getClass().getResourceAsStream("db.properties");
prop.load(is);
this.databasename = prop.getProperty("dbname");
}
catch (IOException e) {
System.out.println("[SysInfo] Open db.properties File, Error!");
}
}
private void initConnection() {
if (this.databasename == null) {
//DBPro myDBPro = DBPro.getInstance();
this.databasename = Sys.SYSINFO.DEFAULTDB;
}
try {
if (conn == null) {
if (Sys.SYSINFO.USEAPPDBPOOL.equals("no")) {
this.connMgr = DBConnectionManager.getInstance();
conn = connMgr.getConnection(this.databasename);
}
else {
DBPool aDBPool = new DBPool( (String) (Sys.SYSINFO.DBPOOLS.get(this.
databasename)));
conn = aDBPool.getCon();
}
}
}
catch (Exception ex) {
System.out.println("Can not get new Connection" + ex.getMessage());
}
}
public Connection getCon() {
return this.conn;
}
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
public void prepareStatement(String sql, boolean isScrollSensitive) throws
SQLException {
if (isScrollSensitive == false) {
prepstmt = this.conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else {
prepstmt = this.conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
}
public void prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
}
public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
}
public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
}
public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
}
public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
}
public void setDouble(int index, double value) throws SQLException {
prepstmt.setDouble(index, value);
}
public void setBinaryStream(int index, InputStream in, int length) throws
SQLException {
prepstmt.setBinaryStream(index, in, length);
}
public void beginTrans() throws SQLException {
/*
try {
isAutoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.print("beginTrans Errors");
throw ex;
}*/
}
public void commit() throws SQLException {
/*
try {
conn.commit();
conn.setAutoCommit(isAutoCommit);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.print("Commit Errors!");
throw ex;
}*/
}
public void roolback() {
/*
try {
conn.rollback();
conn.setAutoCommit(isAutoCommit);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.print("Roolback Error!");
}*/
}
public boolean getAutoCommit() throws SQLException {
boolean result = false;
try {
result = conn.getAutoCommit();
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("getAutoCommit fail " + ex.getMessage());
throw ex;
}
return result;
}
public void clearParameters() throws SQLException {
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
public Statement getStatement() {
return stmt;
}
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt == null) {
stmt = this.conn.createStatement();
}
return stmt.executeQuery(sql);
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else {
return null;
}
}
public void executeUpdate(String sql) throws SQLException {
if (stmt == null) {
stmt = this.conn.createStatement();
}
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null) {
prepstmt.executeUpdate();
}
}
public int[] doBatch(String sql) throws SQLException {
int[] rowResult = null;
String a;
try {
stmt = conn.createStatement();
StringTokenizer st = new StringTokenizer(sql, ";");
while (st.hasMoreElements()) {
a = st.nextToken();
stmt.addBatch(a);
}
rowResult = stmt.executeBatch();
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.doBatch" + ex.getMessage());
throw ex;
}
return rowResult;
}
public void close() throws SQLException {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
/*
if (!Sys.SYSINFO.USEAPPDBPOOL.equals("no")) {
if (conn != null) {
conn.close();
conn = null;
}
}*/
}
public void freeCon() {
if (Sys.SYSINFO.USEAPPDBPOOL.equals("no")) {
if (conn != null) {
connMgr.freeConnection(this.databasename, conn);
}
}
else {
if (conn != null) {
try {
conn.close();
conn = null;
}
catch (SQLException e) {
}
}
}
}
/*
protected void finalize() throws Throwable {
if (conn != null && this.databasename != null) {
connMgr.freeConnection(this.databasename, conn);
}
//connMgr.release();
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -