• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP and MySql question  RSS feed

 
Joe Broderick
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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;
}
 
Rajagopal Sundarrajan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe
Try this:
String queryString = ("select " + subject + " from Post");
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • 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 + "'"
 
Monsieure Scaramouche
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!