通过获取 jtextfield 中的字符串,在 java 中使用 LIKE 选择所有数据
Selecting all data using LIKE in java by getting the string in jtextfield
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 %?% ";
我似乎遇到了这个错误:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4616)
如果我试试这个字符串
String Sql ="Select * from learners_info where lrn LIKE '%'?'%' ";
我使用 system.out 打印了正在使用的字符串类型,它显示了这一点。
com.mysql.jdbc.JDBC4PreparedStatement@414a0b8f: Select * from learners_info where lrn LIKE '%''1411237''%'
此查询有效,但似乎return 没有任何值
很抱歉,我对 java 中的数据库还是个新手。
从 SQL 字符串中删除“%”部分。试试这个:
String Sql ="Select * from learners_info where lrn LIKE ?";
然后尝试在您的 PreparedStatement setString()
方法中传递 %s 和您的字符串值
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 %?% ";
我似乎遇到了这个错误:
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4616)
如果我试试这个字符串
String Sql ="Select * from learners_info where lrn LIKE '%'?'%' ";
我使用 system.out 打印了正在使用的字符串类型,它显示了这一点。
com.mysql.jdbc.JDBC4PreparedStatement@414a0b8f: Select * from learners_info where lrn LIKE '%''1411237''%'
此查询有效,但似乎return 没有任何值
很抱歉,我对 java 中的数据库还是个新手。
从 SQL 字符串中删除“%”部分。试试这个:
String Sql ="Select * from learners_info where lrn LIKE ?";
然后尝试在您的 PreparedStatement setString()
方法中传递 %s 和您的字符串值