This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement" Watch "PreparedStatement" New topic
Author

PreparedStatement

Sals Hamid
Greenhorn

Joined: Jan 04, 2002
Posts: 24
I write jdbc code in jsp file and it work fine but when i use prepared statements it creates problem.Here is the code:<%@ page language ="java" import="java.sql.*"%>
<%!

int id;
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
%>
<table border ="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>
<td>E-Mail</td>
<td>Contact</td>
</tr>
<%
try
{
String idd= request.getParameter("id");

id=Integer.parseInt(idd);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc dbc:222");
PreparedStatement pstm=conn.PrepareStatement("Select * from directory where id =?");
pstm.setInt(1,id);
rs=pstm.executeQuery();
while(rs.next())
{
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("Name")%></td>
<td><%=rs.getString("Address")%></td>
<td><%=rs.getString("E-Mail")%></td>
<td><%=rs.getString("Contact")%></td>
</tr>
<%
}
}catch(Exception e){
e.printStackTrace();
}
%>

Whats the problem?


No Path of Flowers lead to glory
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
didnt you by accident declare preparestatement twice?
here:
<%!
int id;
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
%>
and here:
PreparedStatement pstm=conn.PrepareStatement("Select * from directory where id =?");

just write in the second time:
pstm=conn.prepareStatment etc.....
(drop the PreparedStatement declration again)
im not sure if it is the problem , but i hope it helps somehow.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PreparedStatement