【问题标题】:Jtable not populated for the one record没有为一条记录填充 Jtable
【发布时间】:2014-07-25 21:35:14
【问题描述】:

我正在解决需要从数据库中获取数据然后填充到 jtable 中的问题。我正在使用带有类似语句的查询。问题是当查询与一条记录匹配时,我无法在表中看到任何记录。这是我的代码。

 ResultSet rs=null;
        String customer_name = null;
        System.out.println("In the verfiy login");
        try {
            Connect_MYSQL object = new Connect_MYSQL();
            conn = object.getConnection();
        } catch (Exception e) {
            System.out.println("Error while getting connection");
        }
        try {
            Statement statement = (Statement) conn.createStatement();
            String Query = "Select * from login where username like'%"+ name+"%' or usertype like'%"+name+"%'";
            System.out.println(Query);
             rs = statement.executeQuery(Query);
            if (rs.next()) {
           System.out.println("INsie Rs");

                return rs;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return rs;

以及我在 JTABLE 中的使用方式

   DataBase_Work object=new DataBase_Work();
        ResultSet rs2=object.Search_User(name);
        String namee=rs2.getString("username");
        System.out.println(namee+"User name");

        if(rs2==null)
        {
          JOptionPane.showMessageDialog(null, "No Record Found Against the value", " Not Record Found", JOptionPane.ERROR_MESSAGE);  
        }     
        else
        {
         Show_all_User.setModel(DbUtils.resultSetToTableModel(rs2));
        }

【问题讨论】:

标签: java mysql sql swing jtable


【解决方案1】:

问题是你在ResultSet 上调用next() 来检查它是否不为空(在返回之前),所以光标已经超过了第一条记录。如果集合只包含一个条目,则当前行无效,getString 返回 null。

使用rs.first() 代替rs.next() 来检查ResultSet 是否为空。

【讨论】:

  • 我使用了 rs.first() 但结果还是一样
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2012-04-24
  • 2014-11-09
  • 2020-02-21
  • 1970-01-01
相关资源
最近更新 更多