我是从不同页面捕获的参数。我将i的值设为1,它应该显示表的第n行。但它没有显示任何内容。
int i=Integer.parseInt(req.getParameter("index"));
i=i-1;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","SYSTEM");
String query="select * from employee1 limit 1 offset i";
PreparedStatement pst=con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
String name = rs.getString(2);
int salary = rs.getInt(3);
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("<form action='update' method=''>");
out.println("Name:<input type='text' name='name' value="+name+"/>");
out.println("Salary:<input type='text' name='salary' value="+salary+ "/>");
out.println("<input type='submit' name='update' />");
out.println("</form>");
答案 0 :(得分:6)
这是错误的:"select * from employee1 limit 1 offset i";
您的i变量未被替换。
应该是"select * from employee1 limit 1 offset ?"
。并使用pst.setInteger(i)
答案 1 :(得分:0)
您可以使用:
String query="select * from employee1 limit 1 offset " + i;
所以,当你的i = 10时,假设你的代码是
String query="select * from employee1 limit 1 offset " + 10;
并且查询将是
select * from employee1 limit 1 offset 10
然而,您的原始查询将是,
select * from employee1 limit 1 offset i
因为&#34; i&#34;在字符串内的引号内不会被评估/替换。你的查询甚至都不会编译!!