J2EE中使用Weblogic数据库连接池的问题:weblogic.jndi.WLInitialContextFactory
我新建了一个工程jdbc_direct
然后添加了一个类:jdbcpool.java
源代码如下:
package jdbcexample;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;
import javax.rmi.*;
public class Example1 {
public static void main(String[] args) {
DataSource ds = null;
Context ctx = null;
Connection myConn = null;
try {
/* 获得WebLogic ServerJNDI初始上下文信息
*/
ctx = getInitialContext();
/* 建立数据源对象
*/
ds = (javax.sql.DataSource)
ctx.lookup("cyoracleDataSource");
}
catch (Exception E) {
System.out.println("Init Error: " + E);
}
Statement myStatement=null;
ResultSet myResultSet=null;
try {
//建立连接
myConn = ds.getConnection();
// 建立语句对象
myStatement = myConn.createStatement();
//建立结果集对象
myResultSet = myStatement.executeQuery("SELECT * from SCOTT.EMP" );
//遍历结果集对象,访问每一条记录,输出full_name字段
while(myResultSet.next())
{
System.out.println("the employee full name is " + myResultSet.getString("ENAME"));
}
//关闭结果集
myResultSet.close();
}
catch (SQLException e) {
System.out.println("Error code = " + e.getErrorCode());
System.out.println("Error message = " + e.getMessage());
}
finally {
try {
// close the Statement object using the close() method
if (myStatement != null) {
myStatement.close();
}
// close the Connection object using the close() method
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 = "";//密码
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;
}
}
}
在开启了weblogic后,然后编译,运行该文件的时间,总是提示:
Init Error: javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
Hit uncaught exception java.lang.NullPointerException
请问错误出在哪里?
先谢后给分