Chinaunix首页 | 论坛 | 博客
  • 博客访问: 897826
  • 博文数量: 215
  • 博客积分: 10062
  • 博客等级: 上将
  • 技术积分: 2235
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 13:21
文章分类

全部博文(215)

文章存档

2012年(1)

2011年(24)

2009年(16)

2008年(91)

2007年(83)

我的朋友

分类:

2008-01-14 23:49:35

package jtasample;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Properties;
import javax.transaction.*;

public class JTATest
{
    public JTATest()
    {
    }
    public static void main(String[] args)
    {
        DataSource ds = null;
        Context ctx = null;
        Connection myConn = null;
        UserTransaction tx = null;
        try
        {
            ctx = getInitialContext();
            tx = (UserTransaction)
            ctx.lookup("javax.transaction.UserTransaction");
            tx.begin();
            ds = (javax.sql.DataSource)
            ctx.lookup ("mysqlDS");
        }
        catch (Exception E)
        {
            System.out.println("Init Error: " + E);
        }
        Statement myStatement = null;
        ResultSet myResultSet = null;
        try
        {
            myConn = ds.getConnection();
            myStatement = myConn.createStatement();
            myStatement.executeUpdate ("INSERT INTO emp (empname,empid,job) VALUES ('John', 10 ,'sales')");
            tx.commit();
            System.out.println("Success!");
        }
        catch (Exception e)
        {
            try
            {
                tx.rollback();
            }
            catch(Exception e1)
            {
            }
            System.out.println("Error message = " + e.getMessage());
        }
        finally
        {
            try
            {
                if (myStatement != null)
                {
                    myStatement.close();
                }
                if (myConn != null)
                {
                    myConn.close();
                }
            }
            catch (SQLException e)
            {
                System.out.println("Error code = " + e.getErrorCode());
                System.out.println("Error message = " + e.getMessage());
            }
        }
    }
    private static Context getInitialContext() throws Exception
    {
        String url = "t3://localhost:7001";
        String user = "weblogic";
        String password = "zhangjane";
        Properties properties = null;
        try
        {
            properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            properties.put(Context.PROVIDER_URL, url);
            if (user != null)
            {
                properties.put(Context.SECURITY_PRINCIPAL, user);
                properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
            }
            return new InitialContext(properties);
        }
        catch(Exception e)
        {
            throw e;
        }
    }
}
阅读(10397) | 评论(0) | 转发(0) |
0

上一篇:JNDI的绑定与查找

下一篇:HibernateApi的下载

给主人留下些什么吧!~~