赞
踩
|
public void foo() {
|
|
1 Connection conn = getConnection();
|
|
Statement stmt = null;
|
|
try {
|
|
2 conn = getConnection();
|
|
stmt = conn.createStatement();
|
|
} catch (Exception e) {
|
|
} finally {
|
|
close(stmt, conn);
|
|
}
|
|
}
<示例代码一>
|
|
private void close(Statement stmt, Connection conn) {
|
|
try {
|
|
1 stmt.close();
|
|
2 conn.close();
|
|
} catch (Exception e) {
|
|
}
|
|
<示例代码二>
|
|
private void close(Statement stmt, Connection conn) {
|
|
try {
|
|
1 if(stmt!=null) stmt.close();
|
|
} catch (Exception e) {}
|
|
try {
|
|
2 if(conn!=null) conn.close();
|
|
} catch (Exception e) {}
|
|
}
|
|
<示例代码三>
|
T_SEQUENCE
| |
NAME
|
CURRENT_VAL
|
CUSTOMER
|
10
|
T_CUSTOMER
| |
ID
|
CUSTOMER_NAME
|
9
|
Kevin
|
10
|
Mary
|
public class Customer {
public void sequencePlus() { Connection conn = null; Statement stmt = null; try { conn = getConnection(); stmt = conn.createStatement(); String sql = "update T_SEQUENCE set CURRENT_VAL = CURRENT_VAL + 1 " + " where NAME = 'CUSTOMER'"; stmt.execute(sql); } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(conn); } } public int getSequenceCurrentVal() { Connection conn = null; Statement stmt = null; ResultSet rset = null; int id = 0; try { conn = getConnection(); stmt = conn.createStatement(); String sql = "select CURRENT_VAL from T_SEQUENCE where NAME = 'CUSTOMER'"; rset = stmt.executeQuery(sql); if (rset.next()) { id = rset.getInt(1); } } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn, stmt, rset); } return id; } public void addCustomer(String name) { Connection conn = null; PreparedStatement stmt = null; ResultSet rset = null; try { sequencePlus(); int id = getSequenceCurrentVal(); conn = getConnection(); stmt = conn.prepareStatement( "insert into T_CUSTOMER(ID, CUSTOMER_NAME) values(?, ?)"); stmt.setInt(1, id); stmt.setString(2, name == null ? "" : name); stmt.execute(); } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(conn); } } } |
<示例代码四>
|
public void showConnection() {
Connection conn = null ;
try {
Class.forName( "oracle.jdbc.driver.OracleDriver" );
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:KEVINORA" , "system" , "manager" );
System.out.println( "Connection Class is:" + conn.getClass().getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
|
<示例代码五>
|
public void showConnection() {
Connection conn = null ;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup( "jdbc/oracle" );
conn = ds.getConnection();
System.out.println( "Connection Class is:" + conn.getClass().getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
|
<示例代码六>
|
public class SimpleBean implements SessionBean {
public void foo() {
Connection conn = null ;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup( "jdbc/oracle" );
conn = ds.getConnection();
System.out.println( "not release connection" );
} catch (Exception e) {
e.printStackTrace();
} finally {
// Not close the connection
// DbUtils.closeQuietly(conn);
}
}
}
|
<示例代码七>
|
|
<图一 执行方法之前的Oracle Session>
|
SQL> select count(*) from v$session;
COUNT(*)
----------
18
|
<图二:执行方法之后的Oracle Session>
|
con1 = getConnection();
|
<示例代码八>
|
|
InitialContext ctx =
new
InitialContext();
String txName =
"java:comp/UserTransaction"
;
UserTransaction tx = (UserTransaction)ctx.lookup(txName);
tx.begin();
new
Customer().addCustomer(
"eric"
);
tx.commit();
|
UserTransaction
启动一个事务,然后在该事务上下文中,增加一笔Customer的记录,我们发觉,在不需要更改Customer类的情况下,上述方法能够正常完成。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。