MyEclipse8.5 连接MYSQL5.5数据库的问题

您好!我用MyEclipse8.5 + MySQL5.5 + win7环境下编写程序测试连接数据库的程序。但是出现以下问题:
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testQuery(jdbcTest1.java:29)
at MySQL.jdbcTest1.main(jdbcTest1.java:148)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testInsert(jdbcTest1.java:62)
at MySQL.jdbcTest1.main(jdbcTest1.java:149)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testQuery(jdbcTest1.java:29)
at MySQL.jdbcTest1.main(jdbcTest1.java:150)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testUpdate(jdbcTest1.java:98)
at MySQL.jdbcTest1.main(jdbcTest1.java:151)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testQuery(jdbcTest1.java:29)
at MySQL.jdbcTest1.main(jdbcTest1.java:152)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testDelete(jdbcTest1.java:123)
at MySQL.jdbcTest1.main(jdbcTest1.java:153)
java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at MySQL.jdbcTest1.initJdbc(jdbcTest1.java:10)
at MySQL.jdbcTest1.getConnection(jdbcTest1.java:17)
at MySQL.jdbcTest1.testQuery(jdbcTest1.java:29)
at MySQL.jdbcTest1.main(jdbcTest1.java:154)
用mysql-connector-java-5.1.20-bin的jar驱动

程序代码如下:package MySQL;


import java.sql.*;
public class jdbcTest1 {

boolean bInited=false;
//加载驱动
public void initJdbc() throws ClassNotFoundException{
Class.forName("com.MySQL.jdbc.Driver");
bInited=true;
}
//获得数据库连接
public Connection getConnection() throws ClassNotFoundException,
SQLException{
if(!bInited)
initJdbc();
return DriverManager
.getConnection("jdbc:MySQL://localhost:3306/mytest?user=root&password" +
"=mjf10086&useUnicode=true&charaterEncoding=gb2312");
//注意,这里的字符集与数据库的设置一致
}

public void testQuery(){
Connection conn=null;
Statement smt = null;
ResultSet rs=null;
try{
conn = getConnection();
smt = conn.createStatement();
String sql = "select*from table1";
rs = smt.executeQuery(sql);
while(rs.next()){
System.out.println("id:"+rs.getInt("_id")+"username:"
+rs.getString("_username")+"password:"
+rs.getString("_password")+"desc:"
+rs.getString("_desc"));
}
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
if(smt!=null)
smt.close();
if(conn!=null)
conn.close();

}catch(Exception e){
e.printStackTrace();
}
}
}

public void testInsert(){
Connection conn = null;
Statement smt = null;
try{
conn = getConnection();
smt = conn.createStatement();
smt
.execute("insert into table1(_username,_password,_desc)values" +
"('马拉多纳','10','阿根廷足球运动员')");
smt
.execute("insert into table1(_username,_password,_desc)values" +
"('坎通纳','7','法国足球运动员')");
smt
.execute("insert into table1(_username,__password,_desc)values" +
"('罗马里奥','11','巴西足球运动员')");
smt
.execute("insert into table1(_username,_password,_desc)values" +
"('古利特','8','荷兰足球运动员')");

}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(smt !=null)
smt.close();
if(conn !=null)
conn.close();

}catch(Exception e){
e.printStackTrace();
}
}
}

public void testUpdate(){
Connection conn = null;
Statement smt = null;
try{
conn = getConnection();
smt = conn.createStatement();
smt
.execute("update table1 set _desc '阿根廷足球球员,一代球王'" +
"where _username='马拉多纳'");
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(smt!=null)
smt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

public void testDelete(){
Connection conn = null;
Statement smt = null;
try{
conn = getConnection();
smt = conn.createStatement();
smt.execute("delete from table1 where _username = '马拉多纳'");
smt.execute("delete from table1 where _username = '坎通纳'");
smt.execute("delete from table1 where _username = '罗马里奥'");
smt.execute("delete from table1 where _username = '古利特'");

}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(smt !=null)
smt.close();
if(conn !=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

public static void main(String[] args){
jdbcTest1 jt1 = new jdbcTest1();
jt1.testQuery();
jt1.testInsert();
jt1.testQuery();
jt1.testUpdate();
jt1.testQuery();
jt1.testDelete();
jt1.testQuery();
}
}

JueJiJiangHu1315
绝迹江湖 加入了jar包还是出现如下错误: java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at jdbcTest1.main(jdbcTest1.java:34)
7 年多之前 回复

2个回答

缺少mysql的驱动jar包,找个mysql的jar包加到项目中就可以了。

JueJiJiangHu1315
绝迹江湖 嗯!不错,多想帮忙啦!!
7 年多之前 回复
jadethao
jadethao com.MySQL.jdbc.Driver 改成 com.mysql.jdbc.Driver这不应该是大写。
7 年多之前 回复
JueJiJiangHu1315
绝迹江湖 加了,还是报错,如下: java.lang.ClassNotFoundException: com.MySQL.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at jdbcTest1.main(jdbcTest1.java:34)
7 年多之前 回复

嗯。1楼答案是对的。
缺少mysql驱动包
MySQL Connector/J

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问