Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get results from user defined query

 
Paddy Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am new to servlets and i am doing a small jdbc application, in that i want query to be fired from textarea(html) and only those columns and values to be showed in the table on the same page. my problem is how can i get the column name that are affected by query ... and also insertion and deletion,update queries to be excuted ....
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way I have used to get all results from a statement, whether that is a result set, update count or a mixture of the two, is this:

If you have a ResultSet, you can get its matching ResultSetMetaData object. With this you can retrieve the column names, types, etc. Just remember that columns start with 1, not 0.
 
Tim Moores
Bartender
Posts: 3135
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem is how can i get the column name that are affected by query

Through the ResultSetMetaData (which you can obtain from the ResultSet).
 
Paddy Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
statement.execute();
this statement does not execute ...error : The method execute(String) in the type Statement is not applicable for the arguments ()

the query works for selection purpose only my table has columns id,first,last,dob,phone,email,address ....
select id from employee; gives error column 'first ' not found

how can i differentiate between selection ,delete or update queries ... i am using eclipse javaee
please help me with the code if you can ... my code is ...



[Edit - added code tags - see UseCodeTags for details]
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paddy Joshi wrote:statement.execute();
this statement does not execute ...error : The method execute(String) in the type Statement is not applicable for the arguments ()

Sorry, my bad. I've worked with PreparedStatement so often I forgot that Statement doesn't have the execute() method, only execute(String). So in my code, just put the query inside the brackets.

One word of caution though. If you don't validate the queries users will be able to execute anything; that includes deleting records, dropping and creating tables, etc.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your user can go across tables, this question has been extensivly discussed in this thread (warning major spoilers)

http://www.coderanch.com/t/565876/JDBC/java/generate-queries-random-joins
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paddy Joshi wrote:hi,
i am new to servlets and i am doing a small jdbc application, in that i want query to be fired from textarea(html) and only those columns and values to be showed in the table on the same page. my problem is how can i get the column name that are affected by query ... and also insertion and deletion,update queries to be excuted ....

I'd say this is not going to be a "small JDBC application", to the contrary, this is going to be a large one, comparable to Toad or SqlDeveloper.

Actually, if you're on Oracle, the iSQL*Plus might cover your needs (though I don't have any experience with it), other databases might have a web client too.
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:
Paddy Joshi wrote:hi,
i am new to servlets and i am doing a small jdbc application, in that i want query to be fired from textarea(html) and only those columns and values to be showed in the table on the same page. my problem is how can i get the column name that are affected by query ... and also insertion and deletion,update queries to be excuted ....

I'd say this is not going to be a "small JDBC application", to the contrary, this is going to be a large one, comparable to Toad or SqlDeveloper.

Not necessarily. I've created a similar application, which allowed me to execute any query on nearly any database connection. It had just two parts (apart from logging in) - an editor part and a results part. The latter would just contain text areas and tables, nothing else.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic