Results 1 to 7 of 7
  1. #1
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Post 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.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

  3. #3
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    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.

  5. #5
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    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.

  7. #7
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    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

  1. ResultSet GUI
    By razmyasdfg in forum JDBC
    Replies: 4
    Last Post: 03-15-2011, 01:51 PM
  2. Problem with ResultSet
    By flaquitqm in forum JDBC
    Replies: 8
    Last Post: 01-27-2010, 08:49 AM
  3. Help with Resultset
    By xxAlemanxx in forum JDBC
    Replies: 6
    Last Post: 06-24-2008, 11:09 AM
  4. ResultSet to XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:50 AM
  5. ResultSet example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •