java学习:weblogic下JNDI及JDBC连接测试(weblogic环境)

JNDI的专业解释,大家自行去网络搜索吧,这里就不啰嗦了。

单纯从使用角度看,可以简称把它看成一个key-value的“哈希资源”容器。给定一个string类型的key,可以把任何类型的value,放入这个容器(通过bind/rebind方法);其它地方需要使用该资源时,根据key就能取出该资源(通过lookup方法)

JNDI使用示例:

package jmyang.weblogic;

/**
 * <p>Title:JNDI示例(WebLogic环境) </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company:cnblogs </p>
 * @菩提下的杨过
 * @version 1.0
 */

import javax.naming.*;
import java.util.Hashtable;

public class JNDITest {

    static Context ctx = null;

    public static void test() {
        String key = "jmyang";

        //先绑定
        bind(key, "杨俊明");

        //再取出来
        String value = (String) lookUp(key);

        System.out.print(key + "=" + value);
    }

    /*
     *绑定
     */
    public static void bind(String name, String object) {
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY,
               "weblogic.jndi.WLInitialContextFactory");
        ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
        try {
            ctx = new InitialContext(ht);
            ctx.rebind(name, object);
        } catch (NamingException e) {
            e.printStackTrace();
        } finally {
            try {
                ctx.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*
     *
     */
    public static Object lookUp(String name) {
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY,
               "weblogic.jndi.WLInitialContextFactory");
        ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
        try {
            ctx = new InitialContext(ht);
            Object object = ctx.lookup(name);
            return object;
        } catch (Exception e) {} finally {
            try {
                ctx.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

 上述代码执行完以后,也可能通过weblogic控制台,查看jndi树来验证: 

 

 JDBC数据源,实际上,也是使用JNDI服务来访问的,下面是JDBC示例代码:(必须先在weblogic中创建数据源)

package jmyang.weblogic;

/**
 * <p>Title:JDBC示例(WebLogic环境) </p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company:cnblogs </p>
 * @菩提下的杨过
 * @version 1.0
 */
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Hashtable;

public class JDBCTest {

    static final String webLogicServer = "t3://localhost:7001"; //weblogic服务器地址
    static final String webLogicINDIStr =
            "weblogic.jndi.WLInitialContextFactory"; 

    public static void test() {
        Connection myConn = null;
        DataSource ds = null;
        Context ctx = null;

        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY, webLogicINDIStr);
        ht.put(Context.PROVIDER_URL, webLogicServer);
        try {
            ctx = new InitialContext(ht);
            ds = (javax.sql.DataSource) ctx.lookup("infoskysso"); //取得名为infoskysso的数据源(注:infoskysso要在weblogic中设置数据源)
        } catch (NamingException e) {
            e.printStackTrace();
        }

        Statement myStatement = null;
        ResultSet myResultSet = null;

        try {
            myConn = ds.getConnection();//建立连接

            myStatement = myConn.createStatement();
            myResultSet = myStatement.executeQuery(
                    "Select DEPTNO From DEPT where rownum<=10");

            while (myResultSet.next()) {
                System.out.println("DEPTNO=" + myResultSet.getInt("DEPTNO"));
            }
            myResultSet.close();
        } catch (SQLException e) {
            System.out.println("Error code = " + e.getErrorCode());
            System.out.println("Error message = " + e.getMessage());
        } finally {
            
            //关闭查询
            if (myStatement != null) {
                try {
                    myStatement.close();
                } catch (SQLException e) {
                    System.out.println("Error code = " + e.getErrorCode());
                    System.out.println("Error message = " + e.getMessage());
                }
            }

            //关闭连接
            if (myConn != null) {
                try {
                    myConn.close();
                } catch (SQLException e) {
                    System.out.println("Error code = " + e.getErrorCode());
                    System.out.println("Error message = " + e.getMessage());
                }
            }
        }

    }
}

 附: weblogic中创建jdbc数据源的方法

posted @ 2012-11-15 11:13  菩提树下的杨过  阅读(6650)  评论(0编辑  收藏  举报