简体   繁体   English

如何检查列中是否存在特定数据

[英]How check whether a particular data exits in column

I am working on java swing project with database. 我正在使用数据库进行Java swing项目。 There is a search by option (dropdown) to select by which attribute the user want to search and very next to dropdown their is a textfield where user can enter value and then click search. 有一个“按选项搜索”(下拉菜单),用于选择用户要搜索的属性,下拉菜单旁边是一个文本字段,用户可以在其中输入值,然后单击“搜索”。 now i want to search for that particular value in that particular column(which is selected by user through the dropdowm) if it is exits i want to print them "found" if not then i want to display not found message.. Please find the attached images & code below. 现在,我想在该特定列(由用户通过dropdowm选择)中搜索该特定值(如果存在),我想将其打印为“找到”(如果没有),那么我想显示未找到消息。随附的图片和代码如下。

For example: from dropdown if user select 'customer id' which is a column name and they enter id in the textfiled as '123456' then if it is exits in the column then i need to print found message else i need to print not found. 例如:从下拉列表中,如果用户选择“客户ID”(这是列名),并且他们在文本文件中输入ID为“ 123456”,则如果在列中退出,则我需要打印找到的消息,否则我需要打印未找到。

String column = jcombo2.getSelectedItem().toString();
    String value = key.getText();
    DefaultTableModel model = (DefaultTableModel) customerinfo.getModel();
    DefaultTableModel model2 = (DefaultTableModel) customerinfo.getModel();

    try {
     Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/customerinfo", "root", "");
     Statement st = con.createStatement();
     ResultSet rs;
     String mysqlQuery = "SELECT * FROM `cust_info` WHERE `"+column+"` ='"+value+"'";
     rs =st.executeQuery(mysqlQuery);
     while(rs.next()) {
         String ci = rs.getString("customer id");
         model.addRow(new Object[] {ci});
     }


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

在此处输入图片说明

Try this: 尝试这个:

String mysqlQuery = "SELECT COUNT(*) FROM `cust_info` WHERE `"+column+"` ='"+value+"'";
rs = st.executeQuery(mysqlQuery);
if (rs.next() && (rs.getInt(1) != 0))
  printFoundIt(); // or whatever you want to do
else
  printDidntFindIt(); // or whatever you want to do
st.close();

Or in case you want to fill the table with found items contemporarily: 或者,如果您想同时用找到的项目填充表格:

String mysqlQuery = "SELECT * FROM `cust_info` WHERE `"+column+"` ='"+value+"'";
rs = st.executeQuery(mysqlQuery);
boolean found_it = false;
while (rs.next())
{
  found_it = true;
  String ci = rs.getString("customer id");
  model.addRow(new Object[] {ci});
}
st.close();

if (found_it)
  printFoundIt(); // or whatever you want to do
else
  printDidntFindIt(); // or whatever you want to do

Same as before but printing the 'found it' directly after having found the first element: 与之前相同,但是在找到第一个元素之后立即打印“找到它”:

String mysqlQuery = "SELECT * FROM `cust_info` WHERE `"+column+"` ='"+value+"'";
rs = st.executeQuery(mysqlQuery);
boolean found_it = false;
while (rs.next())
{
  if (!found_it)
  {
    printFoundIt(); // or whatever you want to do
    found_it = true;
  }
  String ci = rs.getString("customer id");
  model.addRow(new Object[] {ci});
}
st.close();

if (!found_it)
  printDidntFindIt(); // or whatever you want to do

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查文件是否退出? - How to check whether file exits or not? 如何检查特定Firestore文档中是否存在特定字段? - How to check whether a particular field exist in a particular Firestore document? 如何检查字符是否来自特定字符集 - How to check whether a char is from a particular set of characters or not 如何在一周内检查特定列的所有值 - How to check all values for a particular column in a week 如何发现特定数据是否已加密? - How can I discover whether a particular piece of data is encrypted? 使用JFileChooser检查文件是否在特定目录中 - Check whether a file is in particular directory or not with JFileChooser 如何检查该列是否已值? - How can I check whether the column already has value? 如何检查列表中的数据是否包含0或null值并删除该数据 - How to check whether data in the list contains 0 or null value and remove that data 如何检查是否从Java的组合框中选择了特定项目(确定所选元素的数量) - how to check whether a particular items is selected from combo-box in java (determining with the number of element selected) 如何检查特定行中的列的值是否不为null以及制表符是否为灰色 - How to check that the value of column in particular row is not null and is also tab delimeted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM