aspose file tools*
The moose likes JDBC and the fly likes Get results from user defined query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Get results from user defined query" Watch "Get results from user defined query" New topic
Author

Get results from user defined query

Paddy Joshi
Greenhorn

Joined: Oct 13, 2010
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 13, 2010
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get results from user defined query