I have just started working on Java Development (Springs/Hibernate/Oracle). I have a URGENT requirement.
I want to create a method which will take in a String as an argument. This String will be an SQL Select Query.
I want the method to execute the given SQL Select Query and return the result in a ResultSet or an Arraylist.
It would be better if i could get the output in a ResultSet kind of an object so that I can extract the No. of Columns, Name of the Columns, etc. like we can get from the metadata of a ResultSet when we execute a query in Core JAVA.
Note : The SQL Select Query can have any no. of columns, any datatype and any no. of rows.
I read about NamedParameterJdbcTemplate but am unable to understand which method to use from it.
Thanx in advance,
Thanx for the reply buddy. But I think you didnt get my requirement right. I know how to use JDBC in CORE JAVA. But my requirement is I want to use the same with Springs/Hibernate.
I am using springs framework, so theres a middle layer and a database access layer. My "select query" string will be sent from middle layer to database access layer which will execute the same and return the result object back to the middle layer. With Hibernate, I dont have to create connections to the database everytime I want to access any database object, Hibernate takes care of the same. So without a connection object, how do I execute my Select Query String and get the output in a resultset kind of an object.
Hope I am able to explain my requirement.
Hibernate really doesn't like you bypassing it's connections.
I'm not even sure you can get a result set via it.
Also, the idea of passing essentially random SQL through it doesn't really fit with the design model for Hibernate. It's largely built around the idea of mapping objects to tables. Is this for some sort of reporting thing?
You can send native SQL queries to it, but that still won't give you access to the column names (I don't think), though I expect you've already looked at that. You used to be able to get the connection directly (session.connection()), but that's been deprecated for a while now.
But is there no other way wherein I can get what I need? If not thru hibernate then thru springs? using jdbctemplate,etc..?
I am able to get the output using NamedParameterJdbcTemplate().queryForList(sql, paramMap) but its giving me the data in 1 row. I wont be able to extract the columnar data.
You might be able to grab the connection as a separate data source, bypassing Hibernate?
No idea how sensible that would be.
As I asked earlier, though, is this a reporting thing?
Because if so I'd consider doing it separately to your main app.
I have found the soln. to my problem. Posting the same below.
SqlParameterSource paramMap = null;
SqlRowSet srs = getNamedParameterJdbcTemplate().queryForRowSet(str ingBuffer.toString(), paramMap);
Thanx for all the help.