(补充了代码,上个提问老师可以不看了,谢谢)使用Druid连接池查询表格信息后,打印输出信息位置很奇怪
问题描述:
完成代码后测试了很多次,发现控制台输出Druid的系统信息都在查询表格信息的中间
相关代码:
/**
* 程序入口类,接收用户输入,使用switch判断并选择功能
*/
public class GoodsApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("1 - 查询商品信息");
System.out.println("2 - 价格区间查询商品信息");
System.out.println("3 - 添加商品");
System.out.println("4 - 修改商品价格");
System.out.println("5 - 删除商品");
System.out.println("6 - 使用Druid连接池查询商品信息");
System.out.print("请选择功能:");
int cmd = in.nextInt();
Command command = null;
switch (cmd) {
case 1:
command = new QueryCommand();
command.execute();
break;
case 2:
command = new PstmtQueryCommand();
command.execute();
break;
case 3:
command = new InsertCommand();
command.execute();
break;
case 4:
command = new UpdateCommand();
command.execute();
break;
case 5:
command = new DeleteCommand();
command.execute();
break;
case 6:
command = new DruidQueryCommand();
command.execute();
break;
}
}
}/**
* 实现使用连接池查询所有表格数据的功能
*/
public class DruidQueryCommand implements Command {
@Override
public void execute() {
// 1. 加载属性文件
Properties properties = new Properties();
String propertyFile = DruidQueryCommand.class.getResource("/druid-config.properties").getPath();
try {
propertyFile = URLDecoder.decode(propertyFile, "UTF-8");
properties.load(new FileInputStream(propertyFile));
} catch (Exception e) {
throw new RuntimeException(e);
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 2. 获取DataSource数据源对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
// 3. 创建数据库连接
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM t_goods");
rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
Float price = rs.getFloat("price");
String desp = rs.getString("desp");
Date createDate = rs.getDate("create_time");
System.out.println(id + " - " + name + " - " + price + " - " + desp + " - " + createDate);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
DbUtils.closeConnection(rs, pstmt, conn);
}
}
}相关截图:

选择6号功能

发第二个贴子时测试还是先输出查询的第一行信息“手机”,然后才打印红色字体的Druid系统信息,强迫症感觉很奇怪,不知道是什么问题造成的,希望老师解答,谢谢!
24
收起
正在回答
2回答
同学你好,测试运行上述代码没有问题,如下所示:

出现这种情况存在idea加载程序缓慢从而导致顺序错落,但对程序运行实现没有任何影响,同学可尝试重新执行程序
祝学习愉快~
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星