隨筆-11  評論-5  文章-1  trackbacks-0

          我的評論

          自己先跟一帖
          import javax.transaction.*;
          import java.sql.*;
          import java.util.*;
          import java.io.*;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          public class JTATest
          {
          public static void main(String args[])
          {
          Context ctx=null;
          Hashtable ht=new Hashtable();
          DataSource ds=null;
          Statement stmt=null;
          ResultSet rs=null;
          Connection myConn = null;
          UserTransaction tx = null;
          String tablename="student";
          String str1="INSERT INTO student VALUES ('01001001','ixucheng', 33)";
          String str2="INSERT INTO student VALUES ('01001002','hangsan', 44)";
          try
          {
          ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
          ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
          ctx=new InitialContext(ht);
          String str="javax.transaction.UserTransaction";
          tx =(UserTransaction)ctx.lookup(str);
          tx.begin();
          ds = (javax.sql.DataSource)ctx.lookup("TestData");
          myConn= ds.getConnection();
          stmt = myConn.createStatement();
          stmt.executeUpdate (str1);
          stmt.executeUpdate (str2);
          tx.commit();
          System.out.println("Success!");
          }
          catch (Exception E)
          {
          try{tx.rollback();}catch(Exception e){}
          System.out.println("事務發生錯誤,事務被取消! ");
          }
          finally
          {
          if (rs != null)
          {
          try{ rs.close(); } catch (Exception ignore) {};
          }
          if (stmt != null)
          {
          try{ stmt.close(); } catch (Exception ignore) {};
          }
          if (myConn != null)
          {
          try { myConn.close(); } catch (Exception ignore) {};
          }

          }
          }
          }
          主站蜘蛛池模板: 剑川县| 岳阳县| 晴隆县| 重庆市| 襄樊市| 开封县| 茌平县| 营口市| 五峰| 汉源县| 江阴市| 高淳县| 工布江达县| 策勒县| 大冶市| 明光市| 彰化县| 邓州市| 马关县| 辽宁省| 宣汉县| 大兴区| 尉氏县| 田阳县| 定边县| 特克斯县| 山阴县| 金乡县| 沅陵县| 长岛县| 云安县| 巫溪县| 广元市| 万安县| 金寨县| 文水县| 手机| 遂平县| 德惠市| 武山县| 金寨县|