Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connectivity problem

 
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
Pie
Posts: 64623
86
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
Pie
Posts: 20750
30
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?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic