This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement

 
Sals Hamid
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic