0

我是 java 服务器页面的新手,并为涉及 jdbc 连接的 jsp 文件编写了代码,它给了我一个错误“方法 resultString(int, String) 未定义为 PreparedStatement 类型”。这是我的源代码

<%@ page import = "java.sql.*"%>
<html>
<head>
<title>Player Details</title>
</head>
<body>
<center>
Welcome to Players Details Page
<br><br>
Player Details are
<br><br>
<table border=7>
<tr>
<td>Player No</td>
<td>Player Name</td>
<td>Country</td>
<td>Club</td>
</tr>
<%
 String Player_No = request.getParameter("Player_No");
 try
 {
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:SYSTEM/rambabu@localhost:8081:XE");
PreparedStatement ps = con.prepareStatement("Select * from Player where Player_No=?");
ps.resultString(1,Player_No);
ResultSet rs = ps.executeQuery();
if(rs.next())
{   
%>
<tr>
<td><%=rs.getString(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
<td><%=rs.getString(4) %></td>
</tr> 
<%
    }
    rs.close();
ps.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
%>

</table>
</center>
</body>
</html>

`

4

1 回答 1

0

没有名为ps.resultString的方法,它的public void setString(int parameterIndex, String x) throws SQLException

采用

 ps.setString(1,Player_No);

一旦为 SQL 语句创建(准备)了 PreparedStatement,您就可以在问号的位置插入参数。这是使用许多 setXXX() 方法完成的。

xxx 可以是 String ,Int ,...

the above statement sets Player_No at the first question mark in the SQL Query. as a result SQL query will be as follow :(assuming that request.getParameter("Player_No") returns 5 for example)

"Select * from Player where Player_No=5"
于 2012-06-25T07:19:08.353 回答