0

I have some pre defined time in mysql database in varchar format for example - 16:45, 00:30, 09:15, 20:50 and 10 more.

I want to display any one of these time in jspinner I am trying but getting error this my jspinner setup where I am displaying the time this is inside constructor-

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);

and this my modify button code where I fetching the time as string from database trying to show them in 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")) {
    }

run: illegal value

Error is coming in netbeans 7.1

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2747954
  • 97
  • 1
  • 5
  • 16
  • Be aware that building an SQL statement by concatenating user input to a string creates a common and serious vulnerability known as [SQL injection](http://en.wikipedia.org/wiki/SQL_injection). Instead, you should call `con.prepareStatement` and call the `setString` method of the PreparedStatement. – VGR Sep 21 '13 at 00:02

1 Answers1

2

Although not obvious, the error is telling you the type used to set the JSpinner and the String read in from the database are incompatible. SpinnerDateModel uses a Date as the underlying object type

Try

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
arr_time.setValue(format.parseObject(rs.getString(2))); // e.g. input 16:45
Reimeus
  • 158,255
  • 15
  • 216
  • 276