【问题标题】:Why can I only get one record from my table?为什么我只能从表中获取一条记录?
【发布时间】:2019-06-04 12:16:02
【问题描述】:

我正在使用Employee_id从表中搜索记录,同一个Employee_id在同一日期在表中有两条记录,但根据Employee_id搜索记录后只得到一条。

我的表格中有以下列:

Device_ID, Employee_id, Employee_Name, Employee_Ext, Issue_Date

这是我的 Java 代码:

public void actionPerformed(ActionEvent ae) {
  try {
    String str = tf5.getText();
    Connection con = DB.getConnection();
    PreparedStatement st = con.prepareStatement("select * from issuedevices where Employee_id=?");
    st.setString(1, str);
    ResultSet rs = st.executeQuery();

    // Vector v = new Vector();
    if (rs.next()) {
      frame1 = new JFrame("Database Search Result");
      frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame1.setLayout(new BorderLayout());
      //TableModel tm = new TableModel();
      DefaultTableModel model = new DefaultTableModel();
      model.setColumnIdentifiers(columnNames);
      table = new JTable();
      table.setModel(model);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
      table.setFillsViewportHeight(true);
      JScrollPane scroll = new JScrollPane(table);
      scroll.setHorizontalScrollBarPolicy(
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      scroll.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      //  from = (String) c1.getSelectedItem();
      String id = "";
      String Device_ID = "";
      String Employee_id = "";
      String Employee_Name = "";
      String Employee_Ext = "";
      String Issue_Date = "";

      try {
        pst = con.prepareStatement("select * from issuedevices where Employee_id='" + str + "'");
        ResultSet rs1 = pst.executeQuery();
        int i = 0;
        if (rs1.next()) {
          id = rs1.getString("id");
          Device_ID = rs1.getString("Device_ID");
          Employee_id = rs1.getString("Employee_id");
          Employee_Name = rs1.getString("Employee_Name");
          Employee_Ext = rs1.getString("Employee_Ext");
          Issue_Date = rs1.getString("Issue_Date");

          model.addRow(new Object[] {
            id,
            Device_ID,
            Employee_id,
            Employee_Name,
            Employee_Ext,
            Issue_Date
          });
          i++;
        }
        if (i < 1) {
          JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (i == 1) {
          System.out.println(i + " Record Found");
        } else {
          System.out.println(i + " Records Found");
        }
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
      }
      frame1.add(scroll);
      frame1.setVisible(true);
      frame1.setSize(400, 300);
    }

    // st.close();
    // rs.close();
  } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "Name not Found");
  }
}

【问题讨论】:

  • 离题,但请在将代码发布到 Stack Overflow 之前对其进行整理,或者更好的是,使用 linter 即时整理。

标签: java sql swing


【解决方案1】:

你没有循环,所以你只会得到一个结果。尝试改变

if (rs1.next())

while (rs1.next())

【讨论】:

  • 如果这已经解决了您的问题,请勾选接受答案。
  • @mwarren 如果您不介意我问,是否可以使用for 循环而不是while 循环?
  • @LogicalBranch 这不是一个好主意,因为您不知道要检索多少条记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多