weixin_48106907
weixin_48106907
采纳率50%
2021-04-27 14:33 阅读 143
已采纳

请问为啥这段java程序报错呢?

5
import java.sql.*;

public class Gradation {														//创建类
	//连接数据库方法
	public Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3360/test", "root", "root8888");
		return connection;													//返回Connection对象
	}

	public static void main(String[] args) {								//主方法
		Gradation gradation = new Gradation();				//创建本类对象
		Connection connection = null;						//声明connection对象
		Statement statement = null;							//声 明Statement对象
		ResultSet result = null;								//声明ResultSet对象
		try {
			connection = gradation.getConnection();				//与数据库建立连接
			statement = (Statement) connection.createStatement();			//实例话Statement对象
			result = statement.executeQuery("select * from tb_stu");				//执行SQL语句,返回结果集
			while (result.next()) {						//如果当前语句不是最后一条则进入循环
				String idString = result.getString("id");
				String nameString = result.getString("name");
				String sexString = result.getString("sex");
				//获取列名是birthday的字段值
				String birthdayString = result.getString("birthday");
				System.out.println("编号:" + idString);
				System.out.println("姓名:" + nameString);
				System.out.println("性别:" + sexString);
				System.out.println("生日:" + birthdayString);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {						//依次关闭数据库资源
			if (result != null) {
				try {
					result.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					((Connection) statement).close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

 

com.mysql.cj.jdbc.exceptions.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 mysql.connector.java@8.0.24/com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
	at Gradation.getConnection(Gradation.java:7)
	at Gradation.main(Gradation.java:17)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at mysql.connector.java@8.0.24/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
	at mysql.connector.java@8.0.24/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
	at mysql.connector.java@8.0.24/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
	at mysql.connector.java@8.0.24/com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
	at mysql.connector.java@8.0.24/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
	at mysql.connector.java@8.0.24/com.mysql.cj.NativeSession.connect(NativeSession.java:144)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953)
	at mysql.connector.java@8.0.24/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)
	... 7 more
Caused by: java.net.ConnectException: Connection refused
	at java.base/sun.nio.ch.Net.connect0(Native Method)
	at java.base/sun.nio.ch.Net.connect(Net.java:576)
	at java.base/sun.nio.ch.Net.connect(Net.java:565)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
	at java.base/java.net.Socket.connect(Socket.java:645)
	at mysql.connector.java@8.0.24/com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
	at mysql.connector.java@8.0.24/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
	... 10 more
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

8条回答 默认 最新

  • 已采纳
    huayula 酷爱码 2021-04-27 17:15

    连接数据库错误,主要是你犯了下面其中之一:

    1.账号密码错误

    2.端口号错误,mysql默认端口号是3306,你有没有改成3360?

    3.确定你的mysql版本是8.0或者以上,否则驱动不能写com.mysql.cj.jdbc.Driver,而是com.mysql.jdbc.Driver

    4.链接驱动jar包要和你安装的mysql版本一致

    5.上面都没错的情况下,你的链接信息后面加上这个

    ?useUnicode=true&characterEncoding=utf8

    第七行代码这样写

    Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3360/test?useUnicode=true&characterEncoding=utf8", "root", "root8888");
    点赞 评论 复制链接分享
  • software7503 CSDN专家-赖老师(软件之家) 2021-04-27 14:38

    Communications link failure:数据库连接失败,应该是你的jar报版本有问题,com.mysql.cj.jdbc.Driver这个有问题。

    点赞 评论 复制链接分享
  • summer_du summer_du 2021-04-27 14:43

    连接数据库错误。你确定你的端口是3360,而不是3306?

    点赞 1 评论 复制链接分享
  • qq_38977566 Forrest Gump plus 2021-04-27 14:44

     

    mysql服务没有启动吧,看下服务中的mysql有没有启动,下图是启动的。

    解决措施:搜索服务->右击mysql->选择启动,

    点赞 1 评论 复制链接分享
  • qq_42145681 白驹_过隙 2021-04-27 15:16

    把第七行代码的

    Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3360/test", "root", "root8888");

    3360改成3306

    点赞 1 评论 复制链接分享
  • qq_36138652 java-zh 2021-04-27 15:48

    1.你第七行代码有错误,将3360改成3306

    Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root8888");

    2.确保你电脑mysql服务启动了,安装了mysql,如果没有,你可以检查一下

    3.如果还不行,可能是你的mysql-driver中的jar驱动版本不一致,您可以换一下

    点赞 1 评论 复制链接分享
  • QA_Assistant 有问必答小助手 2021-04-27 18:53

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 2021-04-28 17:32

    非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

    速戳参与调研>>>https://t.csdnimg.cn/Kf0y

    点赞 评论 复制链接分享

相关推荐