PreparedStatement stmt=con.prepareStatement("select * from test where testname like '?%'");
stmt.setString(1,keyword);
rset=stmt.executeQuery();
while(rset.next()){
out.println(rset.getString(1));
out.println(rset.getString(2));
out.println(rset.getString(3));
out.println(rset.getString(4));
}
我使用tomcat作为服务器,例外是: java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。
这里我在表单中输入testname,并使用getParameter();方法将此值赋给字符串关键字。我想使用'like',以便我只能检索选定的测试名称。
答案 0 :(得分:1)
问号不得用引号括起来。试试这个:
PreparedStatement stmt=con.prepareStatement(
"select * from test where testname like concat(?, '%')");
或者,你可以这样做:
PreparedStatement stmt=con.prepareStatement(
"select * from test where testname like ?");
stmt.setString(1,keyword + "%");
答案 1 :(得分:0)
将您的查询更改为
PreparedStatement stmt=con.prepareStatement("select * from test where testname LIKE '%' || ? || '%' ");
http://www.coderanch.com/t/502785/JDBC/databases/preparedstatement
答案 2 :(得分:0)
你的报价中不能有?,它不会起作用。一个简单的解决方法是在查询字符串中附加实际值。
String keyword = "parametervalue";
String query = "select * from test where testname like '" + keyword +"%'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);