【问题标题】:How to set predefined time in jspinner如何在jspinner中设置预定义时间
【发布时间】:2013-09-19 19:28:58
【问题描述】:

我在 mysql 数据库中以 varchar 格式预先定义了一些时间,例如 - 16:45、00:30、09:15、20:50 和 10 多个。

我想在 jspinner 中显示这些时间中的任何一个我正在尝试但出现错误 这是我的 jspinner 设置,我在其中显示时间 this 在构造函数中-

Date date = new Date();
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
arr_time.setModel(sm);
JSpinner.DateEditor ar = new JSpinner.DateEditor(arr_time, "HH:mm");
arr_time.setEditor(ar);

这是我的修改按钮代码,我从数据库中获取时间作为字符串,试图在 jspinner 中显示它们

try {
    if (evt.getActionCommand().equals("Modify")) {
        String flno=JOptionPane.showInputDialog(this, "Enter Flight Number");
        String sql="SELECT * FROM flights WHERE flightno='"+flno+"'";
        smt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        rs = smt.executeQuery(sql);
        while (rs.next()) {
            jTextField1.setText(rs.getString(1));
            arr_time.setValue(rs.getString(2));
            jTextField4.setText(rs.getString(4));
            jTextField5.setText(rs.getString(5));
        }
    } else if(evt.getActionCommand().equals("Update")) {
    }

运行:非法值

netbeans 7.1 中出现错误

【问题讨论】:

  • 请注意,通过将用户输入连接到字符串来构建 SQL 语句会产生一个常见且严重的漏洞,称为 SQL injection。相反,您应该调用 con.prepareStatement 并调用 PreparedStatement 的 setString 方法。

标签: java swing date simpledateformat jspinner


【解决方案1】:

虽然不明显,但错误告诉您用于设置 JSpinner 的类型与从数据库中读取的 String 不兼容。 SpinnerDateModel 使用 Date 作为底层对象类型

试试

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
arr_time.setValue(format.parseObject(rs.getString(2))); // e.g. input 16:45

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多