求助啊,Eclipse中java连mysql连不上,折腾了好久,还是不行。。

taihushui_li 2012-04-12 03:00:52
操作系统是mac osx lion。
连接测试代码
package pak1;
import java.sql.*;

public class JDBCTest {
public static void main(String[] args){

try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功!");

// 连续数据库

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");


// statement用来执行SQL语句
Statement statement = conn.createStatement();

// 要执行的SQL语句
String sql = "select Num from grade";

// 结果集
ResultSet rs = statement.executeQuery(sql);

while(rs.next()) {
// 输出结果
System.out.println(rs.getString("Num"));
}

rs.close();
conn.close();

} catch(ClassNotFoundException e) {

System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();

} catch(SQLException e) {

e.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

}
}
}


错误内容:
加载驱动成功!
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at pak1.JDBCTest.main(JDBCTest.java:14)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 15 more
...全文
1447 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
首先确保驱动关联了。

package com.tools;

import java.sql.*;

public class JDBConnection {
private final static String url = "jdbc:mysql://localhost:3306/student?user=root&password=123456&useUnicode=true&characterEncoding=gb2312";
private final static String dbDriver = "com.mysql.jdbc.Driver";
private Connection con = null;
// 通过构造方法加载数据库驱动
static {
try {
Class.forName(dbDriver).newInstance();
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}

// 创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url);
con.setAutoCommit(true);

} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}

// 对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
}

// 对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}

}

ipromise_u 2012-05-11
  • 打赏
  • 举报
回复
LZ可以结贴了,上面的答案已经很全了
a452890354 2012-05-11
  • 打赏
  • 举报
回复
import java.sql.*;
public class TestInsert {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"+ "user=root&password=root");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}


将mysql的jar包(三个)导入eclipse中,然后测试这段程序,没有报错的话,证明连接成功,记得修改程序中你自己mysql的密码和数据库名
xttxqjfg 2012-05-10
  • 打赏
  • 举报
回复
楼主可以给我邮件,我这里有可以运行的连接到mysql的源代码…yibo3513@hotmail.com
winter10 2012-05-01
  • 打赏
  • 举报
回复
驱动包是不是放到了eclipse的相应目录里了?驱动包在mysql的安装目录里面有
步骤:右键单击 项目名 -> 然后build path -> Add External Archives -> 找到你下载好的JDBC驱动Jar包 ->确定 -> 会自动添加到这个Referenced Libraries中然后重启,就可以连接上了!
驱动包的名字:mysql-connector-java-5.0.5-bin.jar
驱动官方下载:http://dev.mysql.com/downloads/connector/
ETCentury 2012-05-01
  • 打赏
  • 举报
回复
应该是用户名密码错了
suibianla11 2012-04-20
  • 打赏
  • 举报
回复
mysql服务没开吧~
wenqiguo 2012-04-20
  • 打赏
  • 举报
回复
检查MySQL数据库服务是否开启,各种配置是否正确。
shaozj2011 2012-04-20
  • 打赏
  • 举报
回复
查看你的驱动包 数据库名称,密码 是否都用对了
zhonghuafy 2012-04-20
  • 打赏
  • 举报
回复
同样遇到了这样的问题,求解答
zhenchuang99 2012-04-12
  • 打赏
  • 举报
回复
去eclipse中的MyEclipse Database Explorer中看看数据库连接是否启动。。。。
游一游走一走 2012-04-12
  • 打赏
  • 举报
回复
请尝试下以下动作
cmd命令下
1. telnet localhost 3306 确认是否出现mysql的字样
2. 以下是否正常
set MYSQL_HOME=E:\tools\mysql-5.5.16-win32 修改为你的路径
%MYSQL_HOME%\bin\mysql -uroot -p123456
mysql> show databases
cseu 2012-04-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

亲,,你的mysql服务开了?
[/Quote]
magong 2012-04-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
Caused by: java.net.ConnectException: Connection refused
[/Quote]
看看服务器是否正常。用MySQL自己的控制台是否可以登录。
  • 打赏
  • 举报
回复
亲,,你的mysql服务开了?

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧