This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

connectivity problem  RSS feed

 
geetu hindu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code works if i simple use(select * from MYTABLE) with create statement but gives error if i write the following:


String sql= "Select * from FLIGHT_SCHEDULE where DAT=? and DEPARTURE=? and RETURN_DAT=? and ARRIVAL=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setDate(1,request.getParameter("a"));
pstmt.setString(2,request.getParameter("b"));
pstmt.setDate(3, request.getParameter("c"));
pstmt.request.getParameter("d"));
ResultSet rset=pstmt.executeQuery();
if(!rset.next())
{
System.out.println("no data found");
}
else
while(rset.next())
{
rset.getString("DAT")
}
}

can't make out where the problem lies.PLEASE HELP
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be two causes first, if there are no rows that meet the Where condition, you won't get any results. Try printing out the parameters you are passing and run the final resultant SQL to check that.

Second and an application bug :

Do you notice what happened when I formatted this, the while statement is inside the else so, it never gets called if you have results. Also every call to rset.next() will advance the ResultSet to the next record, you will never get the First Record!

Change this to:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to JDBC.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gives an error? Would you mind running it again, so you can take note of the error and tell us what it is?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!