Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hello guys, kindly help me on this problem

 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am new to JSP and was attempting to connect to the database. There is no error and the code runs, but only the 'else' part is executed. But all the similar type of coding runs successfully

The code is as follows:

<%@ page import="java.sql.*"%>
<html>

<%!
Connection con;
PreparedStatement pst;
ResultSet rs;
%>
<body>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbcracle:thin:@10.100.1.30:1521:xxx","scott","tiger");
pst=con.prepareStatement("select modelno, qty from cust_cart where username=?");
pst.setString(1,"sujai");
//out.println("hi1");
rs=pst.executeQuery();
//out.println("hi2");
if(rs.next())
{
%>
<table border='1'>
<tr>
<td><font color='#7A5DC7'><b>ModelNumber</b></font></td>
<td><font color='#7A5DC7'><b>Quantity</b></font></td>
</tr>
<%do
{
%>
<tr>
<td><font color='#7A5DC7'><b><%=rs.getString("modelno")%></b></font></td>
<td><font color='#7A5DC7'><b><%=rs.getInt("qty")%></b></font></td>
</tr>
<%
}while(rs.next());
}
else
{
out.println("Connection Failure");
}
con.setAutoCommit(true);
con.close();
%>

</table>
<%
}
catch(Exception e)
{
out.println(e);
}

%>
</body>
</html>
 
Aalok Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

That if block is the main cause of the error in your program. remove it and use while loop inside a try-catch block. This will give you the exact output.
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Aalok :thumb: ,
that has now solved my problem! b/w, I used the if statement to retrieve from the database in some of the previous examples that I have tried, but really dunno what went wrong here!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!