• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get right results from a query  RSS feed

 
vernon mweetwa
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i have developed a stand alone program that runs from the command line. this program connects to the mysql database but the results it returns are not right. i need your help on how best i can refactor the following code to provide the right results because this query should return all the firstnames in the database students.

private void prepareStatements( Connection dbConnection ) throws SQLException
{
System.out.println( "\nPreparing Statements.....");
changeFirstname = dbConnection.prepareStatement("SELECT firstname from students");

System.out.println( changeFirstname );
[ January 20, 2005: Message edited by: Bear Bibeault ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. I suspect you need to execute the PreparedStatement then process the ResultSet to see the results. What you are doing is creating a PreparedStatement, then writing that PreparedStatement out to standard output. This is the equvalent of calling PreparedStatement.toString();
I'd look again at the PreparedStatement class JavaDocs to see how you get a ResultSet from it. (You might also consider looking at the JDBC tutorial at Sun if you haven't already.)
 
Bala Kommu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please clarify what do mean by right results becos the select statement fetches all the first names available in the student table.
 
Bala Kommu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
executing the prepared statement and iterating through the rows


ResultSet rs = null;
rs = changeFirstname.executeQuery();

while (rs.next()) {
//process the data
}
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the JDBC forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!