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

如何基于SQLite关联JComboBox选中项填充JDateChooser?

解决SQLite数据库关联JComboBox为JDateChooser填充日期的问题

问题原因

SQLite没有原生的DATE数据类型,日期通常以字符串(如yyyy-MM-dd格式)或时间戳数字的形式存储。直接调用rs.getDate("DateOfBrith")时,JDBC驱动无法正确解析存储格式,导致抛出Error parsing time stamp异常。

解决方案

根据数据库中DateOfBrith字段的存储格式,选择对应处理方式:

情况1:字段存储为字符串格式(如yyyy-MM-dd)

先读取字符串,手动转换为java.util.Date后设置给JDateChooser:

// 替换原有的dateChooserBrithIDCard.setDate(...)代码
String dateStr = rs.getString("DateOfBrith");
if (dateStr != null && !dateStr.isBlank()) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式需与数据库存储一致
    try {
        Date date = sdf.parse(dateStr);
        dateChooserBrithIDCard.setDate(date);
    } catch (ParseException ex) {
        ex.printStackTrace();
        dateChooserBrithIDCard.setDate(null); // 解析失败时清空日期选择器
    }
} else {
    dateChooserBrithIDCard.setDate(null);
}

情况2:字段存储为时间戳(毫秒数)

直接读取长整型时间戳,转换为Date对象:

long timestamp = rs.getLong("DateOfBrith");
if (timestamp > 0) {
    Date date = new Date(timestamp);
    dateChooserBrithIDCard.setDate(date);
} else {
    dateChooserBrithIDCard.setDate(null);
}

完整优化代码

补充资源关闭逻辑,避免JDBC资源泄漏:

comboBoxIDNumberIDCard = new JComboBox();
comboBoxIDNumberIDCard.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            String sql = "select * from employee where EmployeeID=?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, (String) comboBoxIDNumberIDCard.getSelectedItem());
            rs = pst.executeQuery();
            
            // EmployeeID是主键,用if判断即可,无需while循环
            if (rs.next()) {
                txtnameIDCard.setText(rs.getString("Firstname"));
                txtIDSurname.setText(rs.getString("Surname"));
                
                // 以字符串格式日期为例处理填充
                String dateStr = rs.getString("DateOfBrith");
                if (dateStr != null && !dateStr.isBlank()) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        Date date = sdf.parse(dateStr);
                        dateChooserBrithIDCard.setDate(date);
                    } catch (ParseException ex) {
                        ex.printStackTrace();
                        dateChooserBrithIDCard.setDate(null);
                    }
                } else {
                    dateChooserBrithIDCard.setDate(null);
                }
            } else {
                // 未找到数据时清空所有控件
                txtnameIDCard.setText("");
                txtIDSurname.setText("");
                dateChooserBrithIDCard.setDate(null);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            // 关闭JDBC资源
            try {
                if (rs != null) rs.close();
                if (pst != null) pst.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
});

注意事项

  • 确保SimpleDateFormat的格式字符串与数据库中日期字符串的格式完全匹配(比如数据库是yyyy/MM/dd,格式字符串也要对应调整)。
  • 需导入必要的包:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 20:40:29