JTSExamples.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:8k
源码类别:
Java编程
开发平台:
Java
- package bible.jdbc;
- import javax.transaction.UserTransaction;
- import java.sql.*;
- import javax.naming.*;
- import java.util.*;
- import weblogic.jndi.*;
- /**
- * Class JTSExamples
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class JTSExamples {
- /**
- * Method getTransaction
- *
- *
- * @return
- *
- */
- public static UserTransaction getTransaction() {
- Context ctx = null;
- Hashtable env = new Hashtable();
- UserTransaction tx = null;
- try {
- env.put(Context.INITIAL_CONTEXT_FACTORY,
- "weblogic.jndi.WLInitialContextFactory");
- env.put(Context.PROVIDER_URL, "t3://localhost:7001");
- env.put(Context.SECURITY_PRINCIPAL, "system");
- env.put(Context.SECURITY_CREDENTIALS, "password");
- ctx = new InitialContext(env);
- tx = (UserTransaction) ctx.lookup("javax.transaction.UserTransaction");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- return tx;
- }
- }
- /**
- * Method insertEmp
- *
- *
- * @param empNo
- * @param empName
- * @param empJob
- *
- * @throws SQLException
- *
- */
- public static void insertEmp(int empNo, String empName, String empJob)
- throws SQLException {
- Connection myConnection = Examples.getJTSConnection("OraclePool");
- String sql =
- "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (?, ?, ?)";
- PreparedStatement myStatement = myConnection.prepareStatement(sql);
- myStatement.setInt(1, empNo);
- myStatement.setString(2, empName);
- myStatement.setString(3, empJob);
- if (myStatement.executeUpdate() == 1) {
- System.out.println("Employee inserted.");
- } else {
- System.out.println("Employee not inserted.");
- }
- myStatement.close();
- myConnection.close();
- }
- /**
- * Method insertEmp
- *
- *
- * @param theConnection
- * @param empNo
- * @param empName
- * @param empJob
- *
- * @throws SQLException
- *
- */
- public static void insertEmp(
- Connection theConnection, int empNo, String empName, String empJob)
- throws SQLException {
- String sql =
- "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (?, ?, ?)";
- PreparedStatement myStatement = theConnection.prepareStatement(sql);
- myStatement.setInt(1, empNo);
- myStatement.setString(2, empName);
- myStatement.setString(3, empJob);
- if (myStatement.executeUpdate() == 1) {
- System.out.println("Employee inserted.");
- } else {
- System.out.println("Employee not inserted.");
- }
- myStatement.close();
- }
- /**
- * Method updateEmp
- *
- *
- * @param theConnection
- * @param empNo
- * @param newName
- *
- */
- public static void updateEmp(Connection theConnection, int empNo,
- String newName) {
- String sql =
- "UPDATE EMP SET ENAME = ? WHERE EMPNO = ?";
- PreparedStatement myStatement = null;
- try {
- myStatement = theConnection.prepareStatement(sql);
- myStatement.setString(1, newName);
- myStatement.setInt(2, empNo);
- if (myStatement.executeUpdate() == 1) {
- System.out.println("Employee updated.");
- } else {
- System.out.println("Employee not updated.");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- myStatement.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Method demoNormal
- *
- *
- * @param demoMode
- *
- */
- public static void demoNormal(String demoMode) {
- Connection myConnection = null;
- UserTransaction tx = null;
- Driver myDriver = null;
- try {
- tx = getTransaction();
- tx.begin();
- System.out.println("Beginning transaction...");
- myConnection = Examples.getPooledConnection("OraclePool", "");
- insertEmp(myConnection, 8000, "GILL", "ENGINEER");
- insertEmp(myConnection, 8001, "SCHNEIDER", "ENGINEER");
- insertEmp(myConnection, 8002, "TUCKER", "ENGINEER");
- insertEmp(myConnection, 8003, "WELLS", "MANAGER");
- insertEmp(myConnection, 8004, "ZUFFOLETTO", "WRITER");
- insertEmp(myConnection, 8005, "SEKANDER", "SALES");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (demoMode.equals("commit")) {
- tx.commit();
- System.out.println("Committing transaction.");
- } else {
- tx.rollback();
- System.out.println("Rolling back transaction.");
- }
- myConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Method demoJTS
- *
- *
- * @param demoMode
- *
- */
- public static void demoJTS(String demoMode) {
- Connection myConnection = null;
- UserTransaction tx = null;
- Driver myDriver = null;
- try {
- tx = getTransaction();
- tx.begin();
- System.out.println("Beginning JTS transaction...");
- myConnection = Examples.getJTSConnection("OraclePool");
- insertEmp(9000, "GILL", "ENGINEER");
- insertEmp(9001, "SCHNEIDER", "ENGINEER");
- insertEmp(9002, "TUCKER", "ENGINEER");
- insertEmp(9003, "WELLS", "MANAGER");
- insertEmp(9004, "ZUFFOLETTO", "WRITER");
- insertEmp(9005, "SEKANDER", "SALES");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (demoMode.equals("commit")) {
- tx.commit();
- System.out.println("Committing transaction.");
- } else {
- tx.rollback();
- System.out.println("Rolling back transaction.");
- }
- myConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Method demoXA
- *
- *
- */
- public static void demoXA() {
- javax.sql.DataSource ds1 = null;
- javax.sql.DataSource ds2 = null;
- Connection conn1 = null;
- Connection conn2 = null;
- InitialContext ctx = null;
- UserTransaction tx = null;
- Driver myDriver = null;
- try {
- tx = getTransaction();
- tx.begin();
- ctx = new InitialContext();
- ds1 = (javax.sql.DataSource) ctx.lookup("DenverTxDS");
- ds2 = (javax.sql.DataSource) ctx.lookup("SausalitoTxDS");
- conn1 = ds1.getConnection();
- conn2 = ds2.getConnection();
- insertEmp(conn1, 8000, "ZUFFOLETTO", "WRITER");
- insertEmp(conn1, 8001, "WELLS", "MANAGER");
- insertEmp(conn1, 8002, "TUCKER", "ENGINEER");
- insertEmp(conn1, 8003, "GILL", "ENGINEER");
- insertEmp(conn1, 8004, "SEKANDER", "SALES");
- insertEmp(conn1, 8005, "SCHNEIDER", "ENGINEER");
- insertEmp(conn2, 8000, "ZUFFOLETTO", "WRITER");
- insertEmp(conn2, 8001, "WELLS", "MANAGER");
- insertEmp(conn2, 8002, "TUCKER", "ENGINEER");
- insertEmp(conn2, 8003, "GILL", "ENGINEER");
- insertEmp(conn2, 8004, "SEKANDER", "SALES");
- insertEmp(conn2, 8005, "SCHNEIDER", "ENGINEER");
- tx.commit();
- } catch (Exception e) {
- try {
- tx.rollback();
- e.printStackTrace();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } finally {
- try {
- conn1.close();
- conn2.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String args []) {
- // demoNormal("commit");
- // demoJTS("commit");
- demoXA();
- // Example1.getOracleData();
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/