首页 > 解决方案 > 如何检查数据库表中是否存在字符串

问题描述

 String s="abcd";  // The user that needs to be excluded in the table, 
 the string can be dynamic.

 String sql="SELECT Username,Email FROM login where not 
 Username=userName"; //here I use the query not to exclude the particular user.


 PreparedStatement pst = con.prepareStatement(sql);
 ResultSet rs = pst.executeQuery();
 while(rs.next()){
   inputString=(String)rs.getString("Username");                        
   model.addRow(new Object[{rs.getString("Username"),rs.getString("Email"),false});

 }

但产生的结果是空的,其他用户的详细信息也被排除在外。

标签: javajdbc

解决方案


尝试修改后的查询并动态传递参数,如下所述。

// The user that needs to be excluded in the table, the string can be dynamic.
String s="abcd";  

//here I use the query not to exclude the particular user.
String sql="SELECT Username,Email FROM login where Username != ?"; 

PreparedStatement pst = con.prepareStatement(sql);
pstatement.setString(1, s);
ResultSet rs = pst.executeQuery();

推荐阅读