You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing脑挑战游戏:SQLite多表查询加载用户数据报错求助

排查Java Swing脑挑战游戏的SQLite数据加载错误与页面跳转失效问题

我正在开发一款基于Java Swing的脑挑战小游戏,目前在游戏初始阶段遇到两个棘手问题:

  • 尝试从SQLite数据库的两张不同表加载当前用户详情时,总会触发代码中标记的“未知错误”提示
  • 错误分支里的页面跳转代码似乎没有生效

以下是相关的核心代码:

//This is the method that loads the data
private void showDetails(){
 try(Connection dbConn = dbConnect.initConn()){
 Statement stmt = dbConn.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT * FROM status");
 //Display the name of the Current User
 if(rs.next()){
 currentUserLbl.setText(rs.getString("currentUser"));
 else{
 JOptionPane.showMessageDialog(null, "An Unknown Error Occureds.");
 dispose();
 indexMenu im = new indexMenu();
 im.setVisible(true);
 }
 stmt.close();
 //Update the form with the other values from the database
 PreparedStatement uStmt = dbConn.prepareStatement("SELECT Score, Level FROM users WHERE Name = ?");
 uStmt.setString(1, currentUserLbl.getText());
 ResultSet uRs = uStmt.executeQuery();
 if(rs.next()){
 levelLbl.setText(rs.getString("Level"));
 highscoreValue.setText(rs.getString("Score"));
 } else{
 //This error shows up anytime I run the code
 JOptionPane.showMessageDialog(null, "An Unknown Error Occured.");
 //This line also seems not to work
 this.dispose();
 indexMenu im = new indexMenu();
 im.setVisible(true);
 }
 uStmt.close();
 }
 }catch(Exception e){
 JOptionPane.showMessageDialog(null, e);
 }
 }

问题排查与解决思路

1. 语法错误导致的逻辑混乱

第一个if(rs.next())代码块缺少闭合的},这会直接破坏代码结构,导致编译或运行时的逻辑异常。
修复方案:给第一个if块补上闭合大括号,正确包裹用户名称赋值的代码:

if(rs.next()){
    currentUserLbl.setText(rs.getString("currentUser"));
} else{
    JOptionPane.showMessageDialog(null, "An Unknown Error Occureds.");
    dispose();
    indexMenu im = new indexMenu();
    im.setVisible(true);
}

2. 结果集引用错误(核心问题)

在查询users表的逻辑中,你错误地复用了第一个查询的rs结果集,而没有使用当前查询对应的uRs。这会导致永远无法获取到用户的分数和等级数据,直接进入else分支触发“未知错误”。
修复方案:将第二个判断改为uRs.next(),并使用uRs获取对应字段的值:

if(uRs.next()){
    levelLbl.setText(uRs.getString("Level"));
    highscoreValue.setText(uRs.getString("Score"));
} else{
    // ... 错误分支代码
}

3. 资源关闭不规范

虽然你用了try-with-resources管理数据库连接,但Statement和PreparedStatement手动关闭存在风险,最好也纳入try-with-resources自动管理,避免资源泄漏:

try(Connection dbConn = dbConnect.initConn();
    Statement stmt = dbConn.createStatement();
    PreparedStatement uStmt = dbConn.prepareStatement("SELECT Score, Level FROM users WHERE Name = ?")) {
    // 所有查询逻辑放在这里,无需手动关闭stmt和uStmt
}

4. 页面跳转失效的修复

Swing的UI操作必须在事件调度线程(EDT)中执行,直接调用dispose()和setVisible(true)可能因为线程问题失效。可以用SwingUtilities.invokeLater()确保UI操作在正确线程执行:

SwingUtilities.invokeLater(() -> {
    this.dispose();
    indexMenu im = new indexMenu();
    im.setVisible(true);
});

同时检查indexMenu的构造函数是否正确初始化了所有UI组件(比如是否调用了可视化工具生成的initComponents()方法)。

5. 异常信息优化

当前catch块只展示异常对象本身,不利于定位问题。建议打印详细的异常信息和堆栈轨迹:

catch(Exception e){
    JOptionPane.showMessageDialog(null, "加载数据失败:" + e.getMessage());
    e.printStackTrace(); // 在控制台打印完整堆栈,方便调试
}

内容的提问来源于stack exchange,提问作者Qwabhina McFynn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:24:28