[英]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.