jdbc连接失败!请大家帮忙看一下100 分!!!

SmallMaker 2003-07-17 07:30:39
我的代码:
package college.db;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.sql.DataSource;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.naming.NamingException;
public class example
{ static private DataSource ds_ = null;
/**
* Stores Jndi provider url property
*/
private static String jndiUrl_="t3://localhost:7001";

/**
* Stores Jndi factory property
*/
private static String jndiFactory_="weblogic.jndi.WLInitialContextFactory";

private static String dsName_="OracleDataSource";

static public void execute(String inst)
{
if(inst == null){
System.out.println("String ::null " );
return;// log4g error.
}
executeQuery(inst);
}

static private DataSource getDataSource() throws NamingException
{
try{
InitialContext jndiCtx;
Hashtable jndiProp = new Hashtable();
jndiProp.put(Context.INITIAL_CONTEXT_FACTORY,jndiFactory_);
jndiProp.put(Context.PROVIDER_URL,jndiUrl_);
jndiCtx = new InitialContext(jndiProp);

return (DataSource)jndiCtx.lookup(dsName_);
}catch(NamingException nae){
System.out.println("DBSERVER::getDataSource() Jndi lookup for DataSource <" + dsName_ +"> failed!") ;
}
return null;
}

static private Connection getConnection() throws Exception
{
System.out.println("DBSERVER::getConnection()->retrieve JDBC Connection" + dsName_);
try{
if (ds_ == null){
ds_ = getDataSource();
System.out.println("getDataSource ::" + ds_);
return ds_.getConnection();
}else{
Connection con = ds_.getConnection();
System.out.println("con ::" + con);
// return ds_.getConnection();
return con;
}
}catch(SQLException sqle){
System.out.println("SQLException Caugh" + sqle.getMessage());
// throw new
}
return null;
}


}

weblogic中jdbc的配置

name: OracleConnectionPool
url:jdbc: oracle:thin:@127.0.0.1:1521:LJL
driver classname: oracle.jdbc.driver.OracleDriver
properties: user=college
password=college

输出信息如下:

DBSERVER::getConnection()->retrieve JDBC ConnectionOracleDataSource
DBSERVER::getDataSource() Jndi lookup for DataSource <OracleDataSource> failed!
getDataSource ::null
java.lang.NullPointerException
at college.db.example.getConnection(example.java:82)
at college.db.example.executeQuery(example.java:100)
at college.servlet.sampleServlet.doPost(sampleServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run
(ServletStubImpl.java:1058)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:401)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:306)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio
n.run(WebAppServletContext.java:5445)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServic
eManager.java:780)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
rvletContext.java:3105)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
pl.java:2588)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)
!!!!!!!con close
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
conning333 2003-07-17
  • 打赏
  • 举报
回复
try
{
String url = "t3://127.0.0.1:7001";
String user = "system";
String password = "11111111";
Properties properties = null;

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);
}

Context ic = new InitialContext(properties);
ds = (DataSource)ic.lookup("。。。。。。");
}
catch(NamingException ne)
{

ne.printStackTrace();
}
sduwjr 2003-07-17
  • 打赏
  • 举报
回复
检查你的连接配置
bruni 2003-07-17
  • 打赏
  • 举报
回复
getDataSource() return null.
Did you set up datasource for weblogic?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧