如何用sql在标签上显示结果

时间:2022-09-12 22:48:35

UPDATE
i changed sql query (just test it and it works on sql but still not working with java dont no why :(

UPDATE我更改了sql查询(只是测试它,它适用于sql但仍然不能使用java不要为什么:(

Hi how to add get result on jLabel with this code? and is it possible to show results on jTable?

您好如何使用此代码在jLabel上添加get结果?是否可以在jTable上显示结果?

private void searchTeacherActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    String sql = ("select student_ID, firstName, afterName FROM student JOIN studentHom ON course = studentHom_ID WHERE prefect = ?

    try {
        pst = connection.prepareStatement(sql);

        pst.setString(1, larareSoka.getText());
        pst.execute();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }
}

2 个解决方案

#1


1  

First don't use (?) you can just use ?:

首先不要使用(?)你可以使用?:

select ... where course.courseStart = ? and course.corse.end = ?

Second you have to set parametters to your query in your case you have tow, so you have to use :

其次,在你拖曳的情况下,你必须为你的查询设置参数,所以你必须使用:

pst.setString(1, value_of_courseStart);
pst.setString(2, value_of_corse.end);

Third to get results you have to use ResultSet like this :

第三,要获得结果,您必须使用ResultSet,如下所示:

ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    String firstname = result.getString(1);
    //----------------------------------^
    //...same for the other columns
}

or you can use the name of column like this:

或者你可以像这样使用列的名称:

ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    String firstname = result.getString("firstName");
    //--------------------------------------^^
    //...same for the other columns
}

Note

  1. If you want to get multiple result you ca use while instead of if.
  2. 如果你想获得多个结果,你可以使用而不是if。

  3. Your query is a little wired why you are using :
  4. 您的查询有点用于您使用的原因:

course.courseStart = (?) and course.corse.end = (?)
//no point---^                           ^------Why this point here

Did you mean course.courseStart = ? and course.corseEnd = ?

你的意思是course.courseStart =?和course.corseEnd =?

#2


0  

ResultSet rs = pst.executeQuery(); 
String firstname = rs.getString("firstname");
..
jLable.setText(firstname);

...You need to read the data from the resultset

...您需要从结果集中读取数据

#1


1  

First don't use (?) you can just use ?:

首先不要使用(?)你可以使用?:

select ... where course.courseStart = ? and course.corse.end = ?

Second you have to set parametters to your query in your case you have tow, so you have to use :

其次,在你拖曳的情况下,你必须为你的查询设置参数,所以你必须使用:

pst.setString(1, value_of_courseStart);
pst.setString(2, value_of_corse.end);

Third to get results you have to use ResultSet like this :

第三,要获得结果,您必须使用ResultSet,如下所示:

ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    String firstname = result.getString(1);
    //----------------------------------^
    //...same for the other columns
}

or you can use the name of column like this:

或者你可以像这样使用列的名称:

ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    String firstname = result.getString("firstName");
    //--------------------------------------^^
    //...same for the other columns
}

Note

  1. If you want to get multiple result you ca use while instead of if.
  2. 如果你想获得多个结果,你可以使用而不是if。

  3. Your query is a little wired why you are using :
  4. 您的查询有点用于您使用的原因:

course.courseStart = (?) and course.corse.end = (?)
//no point---^                           ^------Why this point here

Did you mean course.courseStart = ? and course.corseEnd = ?

你的意思是course.courseStart =?和course.corseEnd =?

#2


0  

ResultSet rs = pst.executeQuery(); 
String firstname = rs.getString("firstname");
..
jLable.setText(firstname);

...You need to read the data from the resultset

...您需要从结果集中读取数据