如何检查数据库表中是否存在字符串

时间:2019-03-03 12:22:27

标签: java jdbc

 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});

 }

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

1 个答案:

答案 0 :(得分:1)

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

// 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();
相关问题