• 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

Get results from user defined query

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ....
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if your user can go across tables, this question has been extensivly discussed in this thread (warning major spoilers)

https://coderanch.com/t/565876/JDBC/java/generate-queries-random-joins
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic