【问题标题】:to check whether the search element isn't available in DB检查搜索元素是否在 DB 中不可用
【发布时间】:2015-11-14 23:08:44
【问题描述】:

我正在使用 phpmy admin,如果在数据库中找不到搜索元素,我需要显示“未找到”消息。 使用的代码在这里。

Connection c = DBconnect.connect();
Statement s = c.createStatement();
String e = txtempId.getText();
 ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");

我使用这种方法搜索empId,如果empId在db中不可用我需要显示一条消息。请给我一个解决方案如何检测,如果empId在db中不可用。

【问题讨论】:

    标签: java phpmyadmin


    【解决方案1】:

    像这样使用 if 语句

    Connection c = DBconnect.connect();
    Statement s = c.createStatement();
    String e = txtempId.getText();
    ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");
    if(rs.next())
    {
         do
         {
             // If there is data, then process it
         }
         while(rs.next());
    }
    else
        System.out.println("Not Found");
    

    【讨论】:

      【解决方案2】:

      将文本解析为整数,假设 empId 是整数。

      int empId = Integer.parseInt(txtempId.getText());
      try (Connection c = DBconnect.connect()) {
          String sql = "SELECT *" +
                        " FROM nonacademic" +
                       " WHERE empId = ?";
          try (Statement s = c.prepareStatement(sql)) {
              s.setInt(1, empId);
              try (ResultSet rs = s.executeQuery()) {
                  if (! rs.next()) {
                      // not found
                  } else {
                      // found, call rs.getXxx(...) to get values
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:
         if (rs != null)
        {
          out.println("result set has got something");
          while (rs.next())
          { 
            //I am processing result set now
          }
        }
        else
        {
          out.println("Not Found");
        }
        

        【讨论】:

          【解决方案4】:

          只需使用基本的简单 if & else 语句。如果 ResultSet 为“null”或不包含任何记录,则显示消息,否则读取数据并显示。

          Connection c = DBconnect.connect();
          Statement s = c.createStatement();
          String e = txtempId.getText();
          ResultSet rs = s.executeQuery("SELECT * FROM nonacademic WHERE empId='" +e+ "'");
          if(rs.next())
                   // record found do the processing
          else
              System.out.println("Not Found"); 
          

          【讨论】:

            【解决方案5】:
              String e = txtempId.getText();
              String sql="select *from nonacademic where empId='"+ e+"' ";
               try {
                boolean status=DatabaseConnection.checkValue(sql);
                 if (status) {
                     JOptionPane.showMessageDialog(null,
                      "This id is available");    
            
                      } else {
            
                      JOptionPane.showMessageDialog(null,
                      "Not found");
                                  }
                     } catch (Exception e) {
                }
            

            此方法返回检查搜索元素是否存在

               public static boolean checkValue(String sql) throws Exception {
                boolean b = false;
                ResultSet rst = null;
                Statement st = getStatement();
                rst = st.executeQuery(sql);
                if (rst.next()) {
                    b = true;
                }
                return b;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-11-09
              • 2022-01-07
              • 2011-11-03
              • 2015-01-12
              • 1970-01-01
              • 1970-01-01
              • 2023-01-20
              相关资源
              最近更新 更多