JDBC经典之作

// JDBC RMI Database connection -- database connection object

 

package dbpool;


import java.sql.*;


public class DBConnection {

private Connection con// Database connection object


public PreparedStatement pstmt// SQL statement object


public ResultSet rs// SQL query results


private boolean IsQuery// is a query statement


private String dbHost;


private String userName;


private String password;


// private static final String JDBC_DRIVER_NAME =

// "weblogic.jdbc.oci.Driver"; //Weblogic jdbc driver

// private static final String JDBC_ORACLE = "jdbc:weblogicracle:";


private static final String JDBC_DRIVER_NAME = "oracle.jdbc.driver.OracleDriver"// Weblogic

// jdbc

// driver


private static final String JDBC_ORACLE = "jdbcracle:thin";


public DBConnection() {

rs = null;

pstmt = null;

IsQuery = true;

con = null;

}


public DBConnection(String dbHost, String userName, String password)

throws SQLException, ClassNotFoundException {

this.dbHost = dbHost;

this.userName = userName;

this.password = password;


this.open();

}


public void open(String dbHost, String userName, String password)

throws SQLException, ClassNotFoundException {

this.dbHost = dbHost;

this.userName = userName;

this.password = password;


// Load the JDBdriver

Class.forName(JDBC_DRIVER_NAME);


// Connect to the database

con = DriverManager.getConnection(JDBC_ORACLE + dbHost, userName,

password);


// Set auto-commit to be false

con.setAutoCommit(false);


}

public void open() throws SQLException, ClassNotFoundException {

// Load the JDBdriver

Class.forName(JDBC_DRIVER_NAME);


// Connect to the database

con = DriverManager.getConnection(JDBC_ORACLE + dbHostuserName,

password);


// Set auto-commit to be false

con.setAutoCommit(false);

}


private void checkConnection() throws ClassNotFoundException {

try {

if ((con == null) || con.isClosed()) {

this.open();

}

catch (SQLException ex) {

System.out.println("Could not obtain status of the connection: "

+ ex.getMessage());

}


}

/** Prepare a SQL statement */

public void createStatement(String sqlString) throws SQLException,

ClassNotFoundException {

this.checkConnection();


try {

if (pstmt != null)

pstmt.close();


sqlString = sqlString.toUpperCase();

if (sqlString.startsWith("SELECT "))

IsQuery = true;

else

IsQuery = false;


pstmt = con.prepareStatement(sqlString);

catch (SQLException e) {

System.out

.println("DBConnection timed out, createStatement() reconnected");


this.open();

this.createStatement(sqlString);

}

}


/** Execute a SQL statement */

public ResultSet executeSQL() throws SQLException, ClassNotFoundException {

try {

this.checkConnection();


if (IsQuery)

rs = pstmt.executeQuery();

else

pstmt.executeUpdate();

return rs;

catch (SQLException e) {

int err = e.getErrorCode();


if (err == 0) {

System.out

.println("DBConnection timed out, executeSQL() reconnected");


this.open();


}

throw e;

}

}


public boolean hasMoreRow() throws SQLException {

boolean result = false;

if (IsQuery) {

if (rs.next())

result = true;

else

close(); // close the connection when the query is done

}

return result;

}


public void rollback() throws SQLException {

if (!IsQuery)

con.rollback();

}


public void commit() throws SQLException {

if (!IsQuery) {

con.commit();


close(); // close the connection when the update is done

}

}

public void close() throws SQLException {

if (rs != null)

rs.close();


if (pstmt != null)

pstmt.close();


if (con != null)

con.close();


rs = null;

pstmt = null;

con = null;

}

}

转载于:https://www.cnblogs.com/growing/archive/2012/07/04/2576777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值