已有101人关注
数据库预处理语句,例题运行错误
发表在Java图书答疑 2020-06-12 悬赏:3 学分 《Java从入门到精通(第4版)》第20章 数据库操作
是否精华
版块置顶:

Java从入门到精通(第5版)P370【例20.10】执行课本给的代码后报错

import java.sql.*;

public class Prep { // 创建类Prep

static Connection con; // 声明Connection对象

static PreparedStatement sql; // 声明预处理对象

static ResultSet res; // 声明结果集对象

public Connection getConnection() { // 与数据库连接方法

try {

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql:"

+ "//127.0.0.1:3306/test", "root", "123456");

} catch (Exception e) {

e.printStackTrace();

}

return con; // 返回Connection对象

}

public static void main(String[] args) { // 主方法

Prep c = new Prep(); // 创建本类对象

con = c.getConnection(); // 获取与数据库的连接

try {

// 实例化预处理对象

sql = con.prepareStatement("select * from tb_stu where id = ?");     //第22行

sql.setInt(1, 4); // 设置参数

res = sql.executeQuery(); // 执行预处理语句

// 如果当前记录不是结果集中最后一行,则进入循环体

while (res.next()) {

String id = res.getString(1); // 获取结果集中第一列的值

String name = res.getString("name"); // 获取name列的列值

String sex = res.getString("sex"); // 获取sex列的列值

// 获取birthday列的列值

String birthday = res.getString("birthday");

System.out.print("编号:" + id); // 输出信息

System.out.print(" 姓名:" + name);

System.out.print(" 性别:" + sex);

System.out.println(" 生日:" + birthday);

}

} 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)

at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2247)

at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079)

at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)

at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)

at Prep.getConnection(Prep.java:9)

at Prep.main(Prep.java:19)

Caused by: java.net.ConnectException: Connection refused: connect

at java.base/sun.nio.ch.Net.connect0(Native Method)

at java.base/sun.nio.ch.Net.connect(Net.java:493)

at java.base/sun.nio.ch.Net.connect(Net.java:482)

at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)

at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339)

at java.base/java.net.Socket.connect(Socket.java:585)

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)

... 17 more

java.lang.NullPointerException

at Prep.main(Prep.java:22)


2020-06-12 16:43:56编辑
分享到:
精彩评论 1
根号申
学分:4736 LV12
TA的每日心情
2021-07-16 23:48:46
2020-06-15
沙发

数据库连接失败,无法创建连接,你连接的MySQL服务不存在

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照