2014-06-17 73 views
2

i是從不同頁面捕獲的參數。我將i的值看作1,它應該顯示第n行的表格,但它不顯示任何內容。從數據庫中找到第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>"); 

回答

6

這是錯誤的:"select * from employee1 limit 1 offset i";

你我變量不被取代。

它應該是"select * from employee1 limit 1 offset ?"。而替代這次使用pst.setInteger(i)

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 

因爲字符串內引號內的「i」不會被評估/替換。而你的查詢甚至不會編譯!