当前位置: 技术问答>java相关
数据库连接池调用的错误
来源: 互联网 发布时间:2015-08-13
本文导语: 我在weblogic中建立了一个DataSource,JNDI Name是SS3000DB,但是在Java中调用出错: import java.sql.*; import java.util.*; import java.util.Date; import javax.naming.*; import javax.sql.*; public class testWebLogic{ public static void main(String[] args)...
我在weblogic中建立了一个DataSource,JNDI Name是SS3000DB,但是在Java中调用出错:
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.naming.*;
import javax.sql.*;
public class testWebLogic{
public static void main(String[] args)
throws NamingException{
String s = "";
try{
s += "1";
Context ctx = new InitialContext();
s += "2";
ConnectionPoolDataSource ds = (ConnectionPoolDataSource)ctx.lookup("java:comp/env/ejb/SS3000DB");
s += "3";
PooledConnection pcon=ds.getPooledConnection();
s += "4";
Connection con = pcon.getConnection();
Statement stmt = con.createStatement();
s += "5";
String sque = "select USER_NAME from EPGC_USERS";
s += "6";
ResultSet rset = stmt.executeQuery(sque);
s += "7";
while(rset.next()){
System.out.println(rset.getString(1));
}
s += "8";
stmt.close();
s += "9";
}
catch(Exception e){
System.out.println(s);
}
}
}
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.naming.*;
import javax.sql.*;
public class testWebLogic{
public static void main(String[] args)
throws NamingException{
String s = "";
try{
s += "1";
Context ctx = new InitialContext();
s += "2";
ConnectionPoolDataSource ds = (ConnectionPoolDataSource)ctx.lookup("java:comp/env/ejb/SS3000DB");
s += "3";
PooledConnection pcon=ds.getPooledConnection();
s += "4";
Connection con = pcon.getConnection();
Statement stmt = con.createStatement();
s += "5";
String sque = "select USER_NAME from EPGC_USERS";
s += "6";
ResultSet rset = stmt.executeQuery(sque);
s += "7";
while(rset.next()){
System.out.println(rset.getString(1));
}
s += "8";
stmt.close();
s += "9";
}
catch(Exception e){
System.out.println(s);
}
}
}
|
问题应该出在:
Context ctx = new InitialContext();
这句话。应该加上参数。
比如:
...
Context ctx = getInitialContext("t3://localhost:7001", "user1", "user1Password");
...
static Context getInitialContext(String url, String user, String password) {
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
h.put(Context.SECURITY_PRINCIPAL, user);
return new InitialContext(h);
}
Context ctx = new InitialContext();
这句话。应该加上参数。
比如:
...
Context ctx = getInitialContext("t3://localhost:7001", "user1", "user1Password");
...
static Context getInitialContext(String url, String user, String password) {
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
h.put(Context.SECURITY_PRINCIPAL, user);
return new InitialContext(h);
}