try{ String Sql ="Select * from learners_info where lrn LIKE %?% "; pst=conn.prepareStatement(Sql); pst.setString(1, txtSearch.getText()); System.out.print(pst); rs = pst.executeQuery(); DefaultTableModel ts = (DefaultTableModel)tableSearch.getModel(); ts.setRowCount(0); while (rs.next()){ Object searchTable[] = { rs.getInt("lrn"), rs.getString("learner_firstname"), rs.getString("learner_middlename"), rs.getString("learner_lastname"), }; ts.addRow(searchTable); } } catch(Exception e){ JOptionPane.showMessageDialog(null, "Invalid info idnumber"); e.printStackTrace(); } // finally { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } try { pst.close(); } catch (Exception e) { e.printStackTrace(); } try { } catch (Exception e) { e.printStackTrace(); } }问题是我找不到合适的字符串来让我从文本字段中获取 SQL 中的 LIKE 术语**使用这个字符串**String Sql ="Select * from learners_info where lrn LIKE %?% ";如果我尝试这个字符串String Sql ="Select * from learners_info where lrn LIKE '%'?'%' ";我使用 system.out 打印了正在使用的字符串类型,它显示了这一点。com.mysql.jdbc.JDBC4PreparedStatement@414a0b8f: Select * from learners_info where lrn LIKE '%''1411237''%'此查询有效,但似乎没有返回任何值我很抱歉,因为我对 java 中的数据库还是新手。
1 回答
浮云间
TA贡献1829条经验 获得超4个赞
从 SQL 字符串中删除 '%' 部分。试试这个:
String Sql ="Select * from learners_info where lrn LIKE ?";
setString()
然后尝试在 PreparedStatement方法中传递 %s 和 String 值
添加回答
举报
0/150
提交
取消