java - 如何检查数据库表中是否存在字符串
问题描述
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();
推荐阅读
- ios - 在其他项目共享的 iOS 通用项目中初始化 Firebase Crashlytics
- python - 当我们递归使用 yield 时,我们会创建 n 个迭代器吗?
- css - 升级到 css-loader 4.0.0 时 vue ssr 应用程序样式中断
- reactjs - React 应用程序不会在 Facebook 即时游戏 iframe 中呈现
- django - 是否有另一种方法来注释现有查询集中的计数/总和?
- java - 以与 Mockito 相同的方式使用 Spock Mocks
- swift - Swift Combine - 如何订阅嵌套的 Observable 对象
- scala - play-json 中有没有办法读取键/值映射,记住原始顺序?
- python - 列出理解遗漏值
- android - 将 GsonBuilder 中的排除字段添加回对象