jdbc连接失败!请大家帮忙看一下100 分!!!
我的代码:
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