myeclipse8.5连接mysql_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();

}

}

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页

打赏作者

Ediartos

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值