Results 1 to 7 of 7
Thread: Getting ResultSet
- 07-12-2011, 02:32 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
Getting ResultSet
Hi All,
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,
Ali.
- 07-12-2011, 04:47 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Check out Trail: JDBC(TM) Database Access (The Java™ Tutorials) for the basics.
- 07-13-2011, 06:58 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
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.
Ali.
- 07-13-2011, 09:42 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 07-13-2011, 01:29 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
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.
Ali.
- 07-13-2011, 04:25 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 08-22-2011, 01:23 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 29
- Rep Power
- 0
Hi all,
I have found the soln. to my problem. Posting the same below.
SqlParameterSource paramMap = null;
SqlRowSet srs = getNamedParameterJdbcTemplate().queryForRowSet(str ingBuffer.toString(), paramMap);
for(int i=1;i<=srs.getMetaData().getColumnCount();i++)
{
srs.getMetaData().getColumnLabel(i)
srs.getMetaData().getColumnTypeName(i)
srs.getMetaData().getColumnClassName(i)
........
}
Thanx for all the help.
Ali.
Similar Threads
-
ResultSet GUI
By razmyasdfg in forum JDBCReplies: 4Last Post: 03-15-2011, 01:51 PM -
Problem with ResultSet
By flaquitqm in forum JDBCReplies: 8Last Post: 01-27-2010, 08:49 AM -
Help with Resultset
By xxAlemanxx in forum JDBCReplies: 6Last Post: 06-24-2008, 11:09 AM -
ResultSet to XML
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 09:50 AM -
ResultSet example
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks