eclipse+mysql 问题

wjw20040401 2009-04-17 02:30:21
我开发的环境是eclipse3.3+mysql5.0
在我的一个项目中抛出这样的错误,现在我把错误提示帖出来,高手帮我看看问题到底出在哪?刚开始我以为是连接包的问题,但是我已经把这个包加到库里了,也刷新了工程文件,但还是出现这样的错误
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.lang.ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:429)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:369)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:357)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:83)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at cn.com.chengang.sms.db.ConnectManager.getConnection(ConnectManager.java:30)
at cn.com.chengang.sms.db.AbstractDbOperate.getUsers(AbstractDbOperate.java:35)
at cn.com.chengang.sms.archive.ArchiveEditorActionGroup$FirstAction.run
(ArchiveEditorActionGroup.java:95)
at cn.com.chengang.sms.archive.ArchiveEditorActionGroup.fireFirstAction(ArchiveEditorActionGroup.java:157)
at cn.com.chengang.sms.archive.ArchiveEditor.createPartControl(ArchiveEditor.java:58)
at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:661)
at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:426)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:592)
at org.eclipse.ui.internal.PartPane.setVisible(PartPane.java:299)
at org.eclipse.ui.internal.presentations.PresentablePart.setVisible(PresentablePart.java:179)
at org.eclipse.ui.internal.presentations.util.PresentablePartFolder.select(PresentablePartFolder.java:268)
at org.eclipse.ui.internal.presentations.util.LeftToRightTabOrder.select(LeftToRightTabOrder.java:65)
at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation.selectPart(TabbedStackPresentation.java:400)
at org.eclipse.ui.internal.PartStack.refreshPresentationSelection(PartStack.java:1256)
at org.eclipse.ui.internal.PartStack.setSelection(PartStack.java:1209)
at org.eclipse.ui.internal.PartStack.showPart(PartStack.java:1604)
at org.eclipse.ui.internal.PartStack.add(PartStack.java:499)
at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:103)
at org.eclipse.ui.internal.PartStack.add(PartStack.java:485)
at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:112)
at org.eclipse.ui.internal.EditorSashContainer.addEditor(EditorSashContainer.java:63)
at org.eclipse.ui.internal.EditorAreaHelper.addToLayout(EditorAreaHelper.java:217)
at org.eclipse.ui.internal.EditorAreaHelper.addEditor(EditorAreaHelper.java:207)
at org.eclipse.ui.internal.EditorManager.createEditorTab(EditorManager.java:774)
at org.eclipse.ui.internal.EditorManager.openEditorFromDescriptor(EditorManager.java:673)
at org.eclipse.ui.internal.EditorManager.openEditor(EditorManager.java:634)
at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2719)
at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2633)
at org.eclipse.ui.internal.WorkbenchPage.access$12(WorkbenchPage.java:2625)
at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:2577)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:67)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2572)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2556)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2539)
at cn.com.chengang.sms.navigator.NavigatorView$MyDoubleClickListener.doubleClick(NavigatorView.java:126)
at org.eclipse.jface.viewers.StructuredViewer$1.run(StructuredViewer.java:799)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
at org.eclipse.core.runtime.Platform.run(Platform.java:857)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:46)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:193)
at org.eclipse.jface.viewers.StructuredViewer.fireDoubleClick(StructuredViewer.java:797)
at org.eclipse.jface.viewers.AbstractTreeViewer.handleDoubleSelect(AbstractTreeViewer.java:1384)
at org.eclipse.jface.viewers.StructuredViewer$4.widgetDefaultSelected(StructuredViewer.java:1173)
at org.eclipse.jface.util.OpenStrategy.fireDefaultSelectionEvent(OpenStrategy.java:237)
at org.eclipse.jface.util.OpenStrategy.access$0(OpenStrategy.java:234)
at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:295)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3682)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3293)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2389)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2219)
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:289)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:461)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at cn.com.chengang.sms.Application.start(Application.java:20)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:153)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:504)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:443)
at org.eclipse.equinox.launcher.Main.run(Main.java:1169)
at org.eclipse.equinox.launcher.Main.main(Main.java:1144)

高手看看问题出在哪?还有什么条件不全的也提出来,我在补全!
...全文
463 点赞 收藏 18
写回复
18 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wjw20040401 2009-04-28
谁有MYSQL3.3良好的连接包文件吧,我再试试,看能不能解决
回复
wjw20040401 2009-04-24
大哥,我用你说的方法配置了连接方式,在Myeclipse是可以成功的,按理说在我的项目中也没有问题才对啊,和Eclipse的没什么两样了!
现在我有点怀疑不是数据库连接的问题,可能是我项目中什么文件存在问题
回复
wjw20040401 2009-04-24
我怎么觉得你说的是Myeclipse的,我的连接文件包是放在../lib下的
回复
wjw20040401 2009-04-24
这么跟大家说吧!我在测试数据库连接的时候用的插入数据是可以实现的
public class InsertAndSelect {

public static void main(String[] args){
Connection con=null;
Statement sm=null;
ResultSet rs=null;
String className="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/sms?useUnicode=true&characterEncodeing=utf8";
String username="root";
String password="123456";
try{
Class.forName(className);
con=DriverManager.getConnection(url,username,password);
sm=con.createStatement();
//清空表,然后插入四条记录,最后将它打印出来
String s="陈刚";
sm.executeUpdate("insert into test_table(name0) values('"+s+"')");
sm.executeUpdate("insert into test_table(name1) values('"+s+"')");
sm.executeUpdate("insert into test_table(name2) values('"+s+"')");
sm.executeUpdate("insert into test_table(name3) values('"+s+"')");
//读取记录
rs=sm.executeQuery("select * from test_table");
while(rs.next())
System.out.println(rs.getInt("Id")+"_"+rs.getString("name0")+"_"+rs.getString("name1")+"_"+rs.getString("name2")+"_"+rs.getString("name3"));

}catch(Exception e){
e.printStackTrace();
}finally{//关闭连接,释放资源
if(rs!=null){
try{rs.close();}catch(SQLException e){e.printStackTrace();}
rs=null;
}
if(sm!=null){
try{sm.close();}catch(SQLException e){e.printStackTrace();}
sm=null;
}
if(con!=null){
try{con.close();}catch(SQLException e){e.printStackTrace();}
con=null;
}
}
}
}
但是,在实际的项目中,在debug的时候,每次运行到下面的DriverManager.getConnection(url,username,password);时就会报错,连不上数据库,这让我很郁闷啊!也不知道是其他的文件的问题还是怎么的,我现在才刚学不久,还不是很会找在出错的信息里找原因,只是看到报错的提示里的有自己新建的文件的当放就重点看,但我和参考书比较了好久,也没发现什么问题!苦恼啊!
public class ConnectManager {

private static Connection con;
private ConnectManager(){}

public static Connection getConnection()throws SQLException{
if(con!=null&&!con.isClosed())//不为空且没关闭
return con;
//TODO后期将这些变量改为由界面自由设置
String className="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/sms";
String username="root";
String password="123456";
//生成一个数据库连接
try{
Class.forName(className);
con=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException e){e.printStackTrace();}
return con;

}
//提供一个真正关闭Connection的方法
public static void closeConnection(){
try{con.close();}catch(SQLException e){e.printStackTrace();}
con=null;
}
}

回复
mkk113 2009-04-24
话说我觉得我好像也曾遇到过这个问题!~

当时我按照网上的步骤来的~
引入了Mysql的jar包~可就是给我抛出错误~
后来我又手动的添加了jar包~

后来解决了~
回复
cjdxhc 2009-04-24
那只有重装下了!估计你的Eclipse有问题了!
第一:jar包要对
第二:拷贝到web-inf下的lib文件当中!
成功之后,会在项目中多出现Referenced Libraries刚到的jar包会在里面看的到!
回复
hy99527 2009-04-23
jar包放置后还需要对插件项目进行设置,看看“builder path”和“runtime”选项就知道了、
回复
keyingkeying1 2009-04-23
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.109/zbb_nb?useUnicode=true&characterEncoding=UTF-8
jdbc.username=provence
jdbc.password=123456

这就是基本的连接了,把驱动包放到WebRoot->WEB-INF->lib,注意buildPath,一定要把路径放入系统,不然还是找不到的
回复
shao4517 2009-04-23
直接进Myeclipse database explorer
Driver template:Mysql Connector/】
driver name:conn(自己取)
Connection url:jdbc:mysql://localhost:3306/shao
jdbc:mysql:// (本机也可写ip:端口mysql默认3306;数据库名称)
username:数据库用户名(mysql默认root)
password:数据库密码

add jar:加驱动
Driver classname:com.mysql.jdbc.Driver(基本加载jar后自己出来)
完成

如果成功会连接数据库,失败出错,成功了以上Connection url和Driver classname就可做配置文件的正确连接了
回复
wjw20040401 2009-04-23
谁能给一段完整测试Eclipse+MYSQL5.0的连接代码,在下不胜感激啊!
回复
wjw20040401 2009-04-20
还要补充的是我用的测试文件是可以通过
/*
*@(#)CreateTestTables.java 2009/4/15
* copyright 2008 wjw20040401,all rights reserved.
*/
package cn.com.chengang.sms.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
*
* @author m12cfb018
*/
public class CreateTestTables {

public static void main(String[]args){
Connection con=null;
Statement sm=null;

try{
Class.forName("com.mysql.jdbc.Driver");
//connect database
con=DriverManager.getConnection("jdbc:mysql://localhost/sms","root","123456");
//create sql
sm=con.createStatement();
//if sms is exist then delete it or otherwise create new
sm.addBatch("DROP DATABASE IF EXISTS sms;");
sm.addBatch("CREATE DATABASE sms;");
//turn to sms
sm.addBatch("USE sms;");
sm.addBatch("DROP TABLE IF EXISTS test_table;");
//create sql string
StringBuilder sb=new StringBuilder();
sb.append("CREATE TABLE test_table(");
sb.append(" Id int(6) unsigned not null auto_increment,");
sb.append(" name0 varchar(4) default null,");
sb.append(" name1 varchar(4) default null,");
sb.append(" name2 varchar(4) default null,");
sb.append(" name3 varchar(4) default null,");
sb.append(" PRIMARY KEY (Id)");
sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
sm.addBatch(sb.toString());
//commit
sm.executeBatch();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}finally{//close connection and release the resouce
if(sm!=null){
try{sm.close();}catch(SQLException e){e.printStackTrace();}
sm=null;
}
if(con!=null){
try{con.close();}catch(SQLException e){e.printStackTrace();}
con=null;
}
}
}

}


但在我的项目中就会提示连接池有问题,这是我最想不通的地方!

回复
wjw20040401 2009-04-20
我在网上也查了一下有关错误的答案,刚开始我也怀疑是驱动包安装的方式或者是存放的位置不对,后来我从新更新了一遍,还是出现这样的错误。至于是不是驱动包版本的问题,我也不是太清楚,我用的是ECLIPSE3.3+MYSQL5.0+驱动包是:mysql-connector-java-5[1].0.4-bin.jar,不 知道有没有问题
回复
lvsongqing 2009-04-20
我觉的可能是驱动没有弄好
回复
zhangjingrun_23 2009-04-19
把MySQL驱动包放到:项目->WebRoot->WEB-INF->lib下就行了
回复
king3320x 2009-04-18
没有找到驱动包
mysql-connector-java-3.2.0-alpha-bin.jar
在网上下个 加载进去
回复
zcq268114 2009-04-17
把驱动包从新放进去,在试一试。
回复
mike_24 2009-04-17
驱动包有没有放对路径.
回复
ghostgant 2009-04-17
驱动包的版本是否过旧,还是包没倒全.
LZ检查一下吧!!!

回复
发帖
Eclipse
创建于2007-09-28

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2009-04-17 02:30
社区公告
暂无公告