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

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

 }

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

最佳答案

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

// 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();

关于java - 如何检查数据库表中是否存在字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968748/

相关文章:

string - 非常奇怪的列字符串 "single-quote-comment-brace-time-char"- postgres 选择中的错误替换 - 可能仅与 JDBC 相关

java - 为 DB2 和 Oracle 使用 java 插入 BLOB

java - 使用瘦驱动程序的 Oracle 12c 数据库连接引发 IO 错误

java - ms sql server 连接上的 java 存在基本问题

java - 如何通过 REST API 或 SDK 在 IoT 中心创 build 备

java - 我如何确定 Hotspot JVM 决定重新编译 JIT :ed code a second time? 的原因

java - 我应该在 Spring 5 项目中使用具有实体关系的 MongoDB 来实现端到端非阻塞吗?

java - Shiro 的 DefaultPasswordService 线程安全吗?

java - Spring Injection - 线程安全

java - com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException : Too many connections