Results 1 to 13 of 13
Like Tree4Likes
  • 1 Post By Tolls
  • 2 Post By SurfMan
  • 1 Post By Tolls

Thread: How to add Computed column to ResultSet at runtime ?

  1. #1
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Question How to add Computed column to ResultSet at runtime ?

    Hello,
    I'm working in a project where a particular part has been assigned to me.

    I've a ResultSet of informations like below:
    EmpId EmpFirstName EmpLastName EmpAge
    1 ABC DEF 43
    2 PQR XYZ 37



    Now I've to send this ResultSet(to another function) with EmpFullName in following manner.
    EmpId EmpFirstName EmpLastName EmpFullName EmpAge
    1 ABC DEF DEF, ABC 43
    2 PQR XYZ XYZ, PQR 37


    There is no permission for me that I can change SQL query(or anything like this), and I've to return the modified ResultSet(not TableModel). I googled a lot to implement AbstractResultSet, but no luck for me.

    How can I achieve this ? Can anyone help me with an example ?
    Thanks in Advance.
    Last edited by TempExpt; 04-07-2014 at 10:54 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to add Computed column to ResultSet at runtime ?

    Changing the SQL query is easy, takes 30 seconds, chance of errors are near zero, and still you want to implement ResultSet? Have you seen the ResultSet interface? What makes you think this is a viable solution? What are the reasons not to change the query?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: How to add Computed column to ResultSet at runtime ?

    There is no permission for me that I can change SQL query(or anything like this)
    That, I guess. Although I wouldn't know what that means. You have no permission to do your work properly?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: How to add Computed column to ResultSet at runtime ?

    Why add that column anyway?
    The model should be more than capable of concatenating the names together.
    SurfMan likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: How to add Computed column to ResultSet at runtime ?

    No, that part has been done by another one. There I can get the ResultSet by calling a function written by her. Now I've to modify this ResultSet & return it back to her. That is not a simple SQL query, the query is bit tricky. This SQL query is being passed by another function. Now I'm advised not to modify the existing JDBC code, rather just modify the ResultSet & return.

  6. #6
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: How to add Computed column to ResultSet at runtime ?

    @Tolls: These are not the exact column names that I'm using in my project. This is for a sample Column names. And I've to return as a ResultSet not a TableModel.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to add Computed column to ResultSet at runtime ?

    You are on the wrong path here. ResultSet is an interface defined by JDBC, the database vendor implements them: com.mysql.jdbc.ResultSetImpl, oracle.jdbc.driver.OracleResultSetImpl, net.sourceforge.jtds.jdbc.JtdsResultSet with all kinds of extra, vendor specific methods. That means you have to subclass one of those implementations, override methods of which you don't have the source which makes it a lot harder to see what happens under the hood. You are in for a whole lot of trouble doing it like this.

    Edit: in afterthought, you can create a class that implements ResultSet, and use the Delegate pattern to delegate most of the methods to the original ResultSet. Having said that, there are more objects to modify. How about ResultSetMetadata? I doubt that those are even mutable...
    Last edited by SurfMan; 04-07-2014 at 12:52 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: How to add Computed column to ResultSet at runtime ?

    @ SurfMan: I couldn't get you. Can you please post a sample code here ...

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to add Computed column to ResultSet at runtime ?

    My main message is that this exercise is futile. You might mistake the ResultSet for a "smart hashmap", that just holds records, with key/values for you to get, but the reality is, is that it's a complex object that interacts with the Connection object very closely as you loop through the results. Those are all vendor specific classes you are looking at. JDBC just gives you the interface, vendors do something useful with it. I don't see how you can override/delegate this with your own code.

    If you undertake this, there are plenty of classes you need to update with new information, of which most of them are probably immutable and heavily depend on vendor specific code, not to mention 'live' data that is pulled directly off a Connection object.

    I have no sample code of how to override/delegate a ResultSet. I am not going to even try this since it the worst solution for your problem.

    If you want to know more about the delegate pattern, read this: https://en.wikipedia.org/wiki/Delega...#Java_examples

    If my boss demands me to do this, while a quick SQL query/TableModel fix is available and solves all my (his) problems in 30 seconds, I would quit my job, visit the local witchdoctor and have my wisdom tooth removed without anasthaetic with a set of rusty pliers while she burns me with candlewax in the eye. It will be less painful.

    PS: I'm not saying it's impossible. Perhaps one of the gurus here have done it before. I hope they show an example if they did to prove me wrong.
    jashburn and gimbal2 like this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How to add Computed column to ResultSet at runtime ?

    I've worked in Development, and I've worked in Support. If I see such ResultSet-modifying code while in the latter, I'll throw it back to Development and ask them to redo it.

    It will be a nightmare to maintain due to the large ResultSet API and the dependencies with other clases such as ResultSetMetaData, InputStream, Reader, Ref, SQLXML, etc.. The design that necessitates modification of the ResultSet object to add a custom column is just wrong. The cost of maintenance (because there's a lot of code to change) and regression testing (because it will be very fragile) for every little modification (be it for defect fix or for enhancement) far outstrips the cost of fixing the design.

    The only compromise that I can see at the moment is an agreement with the party that owns the code that expects the custom column, that the returned (modified) ResultSet would support only a small subset of the ResultSet API. All unsupported methods are then to throw UnsupportedOperationException. This reduces the cost of implementation, but does not reduce the cost of maintenance. It effectively adds (a lot of) technical debt into the application that your organisation will pay doubly over in the future compared to fixing the design now. I'm mentioning this option only to inform you of the consequences should someone resist the design fix, and ask for this to be done instead.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: How to add Computed column to ResultSet at runtime ?

    Sorry, I'm not even going to think about a work around for a mistake that happens outside of the code. What has to change here is that people are forcing a solution that works on a resultset; fix it at the core, fire a proper query. For all I care you stick in a database view in between there so you can do select * on that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: How to add Computed column to ResultSet at runtime ?

    @All: I'm not sure whether it's possible or not, so I'm asking the following.
    If I get the data from ResultSet into a TableModel, & modify the TableModel as per my requirement, then is it possible to convert the TableModel(modified) into a ResultSet ?

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

    Default Re: How to add Computed column to ResultSet at runtime ?

    No (other than to write your own implementation of ResultSet which has nothing to do with JDBC, and would be an abomination).

    To be honest, outside of some toy application, handing a ResultSet directly to the View and skipping out the Model entirely is the root cause of all your problems. I have no idea why you feel you have to do this (apart from it being dictated to you). You do not manipulate ResultSets like this.

    So, unless you are going to expand on what this is all about, my advice to you is to tell whoever it is asking you to do this that this is a bad design.
    SurfMan likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 04-07-2014, 10:52 AM
  2. Replies: 0
    Last Post: 10-18-2013, 06:03 PM
  3. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  4. Problem while retrieving column size of ResultSet
    By gurrapuk in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2011, 04:52 PM
  5. how to get/call the computed value
    By shizuka in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:38 AM

Tags for this Thread

Posting Permissions

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