• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP and MySql question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I can display everything from a table to a jsp, but if i want to only display some of the data based on a query, how can I do that? I tried to pass a string from the jsp to the bean which is connected to the database, but it does not work.

public ResultSet viewQuery( String subject ) throws SQLException, Exception{
ResultSet rst = null;
try{
String queryString = ("select " + subject + "from Post;");
Statement stmt = con.createStatement();
rst = stmt.executeQuery(queryString);
}
catch (SQLException sqle){
error="SQL Exception";
}
catch (Exception e){
error="An exception occured";
}
return rst;
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe
Try this:
String queryString = ("select " + subject + " from Post");
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Broderick:
String queryString = ("select " + subject + "from Post;");


I would also remove the ";" from the query string.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This depends on the DB, but in general the ';' should not be a problem.
I suggest using error = sqle.getMessage() to see the actual error being returned by the DB. That will be quite informative in determining the error. LIkewise, use error = e.getMessage()
Also, is this code just testing the ability to query the DB? It seems as though it will return one column of all records in the table. Perhaps you meant in your queryString:
"select * from Post where SUBJECT = " + subject
And remember, if SUBJECT is a varchar, you have to enclose your subject variable with single quotes:
"select * from Post where SUBJECT = '" + subject + "'"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple :
String queryString = ("select " + subject + "from Post;");
=>>>>
String queryString = ("select " + subject + " from Post;");
"from" shouldbe preceded by a space.
Thanks
-M

Originally posted by Joe Broderick:
Hello,
I can display everything from a table to a jsp, but if i want to only display some of the data based on a query, how can I do that? I tried to pass a string from the jsp to the bean which is connected to the database, but it does not work.

public ResultSet viewQuery( String subject ) throws SQLException, Exception{
ResultSet rst = null;
try{
String queryString = ("select " + subject + "from Post;");
Statement stmt = con.createStatement();
rst = stmt.executeQuery(queryString);
}
catch (SQLException sqle){
error="SQL Exception";
}
catch (Exception e){
error="An exception occured";
}
return rst;
}

 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Btw. I get cold chills when I see people concatenating SQL strings... I always use PreparedStatement, which provides a safe way to construct the query dynamically.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse,
Perhaps you could give an example of PreparedStatement, though I realize this is veering a little off topic.
Ken
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, never mind. I use them all the time but mistakenly thought you must be referring to something far fancier.
Must be time to log off.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic