如何将Java微调器值发送到SQL?我想在SQL中以日期时间格式保存它?

时间:2016-06-30 11:03:00

标签: java swing jframe jspinner

第一部分是按钮点击部分。第二部分是sql部分

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

    try {
        String id1 = jTextField1.getText();
        String subj = kk.getSelectedItem().toString();
        String name_teacher = tt.getText();
        String grde = jComboBox1.getSelectedItem().toString();
        String st_d = spinner.getSelectedItem().toString();
        int en_d = jSpinner1.getSelectedItem().toString();

        String url = "jdbc:sqlserver://localhost:1433;databaseName=class ";
        String username = "mali";
        String password = "12345";
        Connection con = DriverManager.getConnection(url, username, password);
        Statement st = con.createStatement();
        //String sub = kk.getText();
        //String gg =tt.getText();
        String QUERY = "INSERT INTO addteacher(id,sub,namet,grd,start_time,end_time)VALUES('" + id1 + "','" + subj + "', '" + name_teacher + "','" + grde + "','" + st_d + "','" + en_d + "')";
        st.execute(QUERY);
    } catch (Exception e) {

    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试此解决方案:

JSpinner hour = new JSpinner();
JSpinner min = new JSpinner();

//Your date format
SimpleDateFormat format = new SimpleDateFormat("HHmm");

//Get values from your spinners
String HOUR = (String) hour.getValue();
String MIN = (String) min.getValue();

//Create a date 
Date parsed = format.parse(HOUR + MIN);
java.sql.Time sql = new java.sql.Time(parsed.getTime());

//Insert your date
System.out.println(sql);