JDBC代码有错吗? 1 你有Weblogic 吗?2 你的Weblogic 运行了吗?3 你的Weblogic 配置了一个名字叫MysqlDataSourse的数据源了吗?4 数据源需要一个连接池,你配置了吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据源的JNDI米有找到,可能的原因如一楼所说 1楼已经说得特别详细了。出错地方基本在这段代码中: ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); ctx=new InitialContext(ht); DataSource ds=(DataSource)ctx.lookup("MysqlDataSourse"); 基本上 配置是对的。就是在DataSource ds=(DataSource)ctx.lookup("MysqlDataSourse"); 这行代码出lookup“MysqlDataSourse”的时候出错的。因为ctx中找不多名为"MysqlDataSourse"的DataSource。据我猜测可能是你写错了一个字母。把"MysqlDataSourse"中的最后一个s改为c。改完后应该是"MysqlDataSource"。你可以用这个试试看。不行再问吧。:) weblogic 都配置好了,不过就是在外边启动的,因为在里边启动有错误(我也在里边发了帖子还没解决呢!)!数据源都正确!名字也没错!我今天晚上又从头到尾从弄了一遍,结果还是这个错误!不知道为什么? DataSource ds=(DataSource)ctx.lookup("MysqlDataSourse");lookup中的字符串不是你是数据源名,而是其对应的JNDI名 看看你的启动weblogic方式,你要是在eclipse启动必须在首选项里配置weblogic下的一个参数path要是在doc启动要配置你的环境变量 是数据源名! 我把JNDI的名和数据源一个! 我在外边启动的weblogic服务!我在里边不能启动有错误:A configuration error occurred during startup.Please verify the preference field with the prompt:Cannot find a free socket for the debugger 所以就从外边启动的,在里边的服务我配置的都正确,经过老师验证,她都不知道怎么个错法? 不是数据源的问题!今天我们老师还说来,可能是因为服务不能在里边启动的原因!我就按照老师说的那个方法:把mysql的驱动程序复制到user_projects\domains\base_domain\lib下.但是结果还是没有找到驱动程序!不知道为什么? 我用你的代码得到和你所说一样的结果,但我的代码运行正常,你的代码来自《Java EE 5实用教程--基于Weblogic和Eclipse》吧,哈哈package JDBCPoolDemo;import java.sql.*;import javax.sql.*;import javax.naming.*;import java.io.*;import java.util.*;public class JDBCPoolDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String tablename="student"; String sqlstr; Connection con=null; Statement stmt=null; ResultSet rs=null; Context ctx=null; Hashtable ht=new Hashtable(); try{ ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); ctx=new InitialContext(ht); DataSource ds=(DataSource)ctx.lookup("MysqlDataSource"); con=ds.getConnection(); stmt=con.createStatement(); sqlstr="select * from "+tablename; rs=stmt.executeQuery(sqlstr); while(rs.next()){ System.out.print(rs.getString("id")); System.out.print("\t"); System.out.print(rs.getString("name")); System.out.print("\t"); System.out.print(rs.getString("age")); System.out.print("\t"); System.out.print("\n"); } } catch(NamingException e1){ System.out.println(e1.toString()); System.out.println("驱动程序没有找到!"); } catch(SQLException e2){ System.out.println(e2.toString()); System.out.println("数据库异常!"); } finally{ try{ if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); } catch(SQLException e){ System.out.println(e.toString()); } } }} Mahout java 运行方法求助 对Mahout 不了解 求助:关于ArrayList的奇怪问题 OcxBean ox = new OcxBean(); ox内存地址如何获得 谢谢 myeclipse创建数据库后连接数据库失败,错误如下: 求助:EBJ与多线程 JAVA素数问题 初学struts,分享下错误经验 讨论下Struts传参数给jsp页面的问题 如何得到一个字符串中出现最多的字母及出现次数? java中播放音乐的问题 echo2 2.0感觉如何呀?有相关教程文档吗? [调查] 一个JAVA程序员感兴趣的话题,散分!
出错地方基本在这段代码中:
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ctx=new InitialContext(ht);
DataSource ds=(DataSource)ctx.lookup("MysqlDataSourse"); 基本上 配置是对的。
就是在DataSource ds=(DataSource)ctx.lookup("MysqlDataSourse");
这行代码出lookup“MysqlDataSourse”的时候出错的。因为ctx中找不多名为"MysqlDataSourse"的DataSource。
据我猜测可能是你写错了一个字母。把"MysqlDataSourse"中的最后一个s改为c。改完后应该是"MysqlDataSource"。你可以用这个试试看。不行再问吧。:)
数据源都正确!名字也没错!
我今天晚上又从头到尾从弄了一遍,结果还是这个错误!
不知道为什么?
不是你是数据源名,而是其对应的JNDI名
要是在doc启动要配置你的环境变量
我在里边不能启动有错误:
A configuration error occurred during startup.Please verify the preference field with the prompt:Cannot find a free socket for the debugger 所以就从外边启动的,在里边的服务我配置的都正确,经过老师验证,她都不知道怎么个错法?
今天我们老师还说来,可能是因为服务不能在里边启动的原因!
我就按照老师说的那个方法:把mysql的驱动程序复制到user_projects\domains\base_domain\lib下.
但是结果还是没有找到驱动程序!
不知道为什么?
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.io.*;
import java.util.*;public class JDBCPoolDemo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String tablename="student";
String sqlstr;
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
Context ctx=null;
Hashtable ht=new Hashtable();
try{
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ctx=new InitialContext(ht);
DataSource ds=(DataSource)ctx.lookup("MysqlDataSource");
con=ds.getConnection();
stmt=con.createStatement();
sqlstr="select * from "+tablename;
rs=stmt.executeQuery(sqlstr);
while(rs.next()){
System.out.print(rs.getString("id"));
System.out.print("\t");
System.out.print(rs.getString("name"));
System.out.print("\t");
System.out.print(rs.getString("age"));
System.out.print("\t");
System.out.print("\n");
}
}
catch(NamingException e1){
System.out.println(e1.toString());
System.out.println("驱动程序没有找到!");
}
catch(SQLException e2){
System.out.println(e2.toString());
System.out.println("数据库异常!");
}
finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();
}
catch(SQLException e){
System.out.println(e.toString());
}
}
}}