ITeye博客

JNDI使用测试

kinkding · 2010-07-15 18:22 · JAVA · 编程语言 · Markdown

package filesplit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JNDITest {
    public static void main(String[] args) {
        String jndiName = "CTDataSource";
        String url = "t3://10.10.5.29:9200";
        JNDITest jtest = new JNDITest();
        try {
            jtest.testjndi(jndiName, url);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void testjndi(String jndiName, String url) throws NamingException,
            SQLException {
        Hashtable arg = new Hashtable();
        arg.put("java.naming.provider.url", url);
        arg.put("java.naming.factory.initial",
                "weblogic.jndi.WLInitialContextFactory");

        InitialContext objCtx = new InitialContext(arg);
        DataSource ds = (DataSource) objCtx.lookup(jndiName);

        Connection conn = ds.getConnection();
        PreparedStatement pstmt = conn
                .prepareStatement("select USER_ID,NAME from pub_user where DEPARTMENT_ID=0");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString("USER_ID") + ","
                    + rs.getString("NAME"));
        }
        rs.close();
        pstmt.close();
        conn.close();

    }

}

 

得到的结果如下:

00000495,hpbac
00000503,test12
00000969,hang

 

运转良好。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。