weixin_48106907
2021-04-27 14:33
采纳率: 50%
浏览 161
已采纳

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

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条回答 默认 最新

  • 酷爱码 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");
    评论
    解决 无用
    打赏 举报
查看更多回答(7条)

相关推荐 更多相似问题