• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing database result set from server to client  RSS feed

 
Priti Mehta
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

I am very much new to java, and i am developing webservice. Problem which I am facing is not able to figure out how to send resultset from the database
in server to client in a tabular form.

Please help me asap....


 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not pass the resultset from server to client in tabular form.
What you can do is pass Java objects.
Now it is your task to find
  • how to convert resultset data into Java objects
  • how to pass the Java object from server to client
  •  
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    List<object[]> records=new ArrayList<object[]>();
    while(resultSet.next()){
    int cols = resultSet.getMetaData().getColumnCount();
    Object[] arr = new Object[cols];
    for(int i=0; i<cols; i++){
    arr[i] = resultSet.getObject(i+1);
    }
    records.add(arr);
    }

    Is this the correct way to put it in object...?
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:Is this the correct way to put it in object...?

    This is definitely a way to do it.
    But there are more appropriate ways to do it.
    IMO, a good way is to replace the object[] with a POJO.

    for example.
    let us assume SELECT query returns something like this

    Then you can replace List<object[]> with List<ProductBean>
    ProductBean being something like this.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot for reply!!!

    Can you be please more precise as I am not able to understand it..My code for quering the databse is like this....

    I need to write something after
     
    Liutauras Vilda
    Marshal
    Posts: 4820
    330
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please check how to UseCodeTags (<- link to click on) properly. I have fixed them for you.

    Welcome to the Ranch.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:
    Is this the correct way to put it in object...?


    What do you have inside the object[] ?

    PS: Analyze Tapas Chand's answer correctly. You will get it. He has given you a simple day to day object example.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    I can understand what Tapas Chand says but I already have a class for this..So I was thinking whether to use that as a object or something different. Here is my code.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    I used following code but is error for type mismatch. Error is "Type Mismatch: Cannot convert for Object to UAVSearch". Any idea...?

     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta,

    Ok. So you have a model class. Based on what would you have formed the model class?

    Note: Think, once you get the correct answer you will be able to solve this yourself.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    I am really sorry to say that I have found this style of coding in many examples before I had started coding so made this model class.

    Also 1 more thing I made the above class implements serializable but still I am not able to make its object in client which is altogether a different project. Do i need to keep this model class in both the server and client projects?
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:I am really sorry to say that I have found this style of coding in many examples before I had started coding so made this model class.

    There is no need to be sorry. This is actually the correct method AFAIK. Just think on what basis your model class is formed? Just go through the examples you follow and see based on which entity, they are forming their model class.

    Priti Mehta wrote:Also 1 more thing I made the above class implements serializable but still I am not able to make its object in client which is altogether a different project. Do i need to keep this model class in both the server and client projects?

    No. You I think you wouldn't be needing to create the same model class. How do you receive this model class object in your client project?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I think I should keep Client and server in one projects only but different packages..so that i can use the objects.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote: I am very much new to java, and i am developing webservice.

    For a web service, It doesn't make much sense when you use client and server in same project.

    Did you figure out based on what they have formed the model classes?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    For model classes and model object I found in one link.

    The term Model Object is an informal term, with no widely accepted definition. Here, Model Objects (MOs) refer to data-centric classes which encapsulate closely related items.
    Model Objects:

    are very common, and are used in almost all applications
    are often central to an application, since they usually model problem domain objects
    often map roughly to the records of a corresponding database table
    are often used as return values for Data Access Object methods
    are easily tested using JUnit (or a similar tool)
    can be used to implement the Model in a Model-View-Controller pattern
    Should Model Objects follow the JavaBeans conventions?
    Not necessarily. In fact, some argue that the JavaBeans style is to be avoided as a general model for Model Objects.

    Should Model Objects be immutable?

    Given the deep simplicity of immutable objects, some prefer to design their Model Objects as immutable. However, when the underlying data changes, a new object must be created, instead of simply calling a setXXX method on an existing object. Some argue that this penalty is too high, while others argue that it is a micro-optimization - especially in cases where the data is "read-mostly", and the state of corresponding Model Objects changes only rarely.

    Implementing Model Objects as immutable seems particularly natural in web applications. There, Model Objects are most commonly placed in request scope, not session scope. In this case, there is no long-lived object for the user to directly alter, so the Model Object can be immutable.


     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But if we make different projects for client and servers then how do we use objects accross?

    Also I am having error in the code for making an object for resultant set.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:But if we make different projects for client and servers then how do we use objects across?

    Can you tell me how client access data from a webservice?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    using URI
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:using URI

    Yes that is you will be sending a request to the server. So you will receive a response from the web service. So how can we send data through response?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No idea about this...I am not able to figure out which is best method for communication between client and server and sending data across them.

    I saw sockets, QueryParam, @produces and @consumes etc but now got confused on which one to use and how.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:No idea about this...I am not able to figure out which is best method for communication between client and server and sending data across them.

    I saw sockets, QueryParam, @produces and @consumes etc but now got confused on which one to use and how.


    Don't confuse with all this. If you want send data through response you will be able to send it via JSON or XML. So what would be your next step with result set?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Write the resultant set in xml or in json file.

    One question will this file whether xml or json file be accessible on client side?
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:Write the resultant set in xml or in json file.

    Not a file but to a object. You will have to convert your result set to a JSON object (preferred way) or an xml object. This is where @Produces comes into action. So how do you convert your result set to JSON Object?

    Priti Mehta wrote:One question will this file whether xml or json file be accessible on client side?

    Yes. Your client should be able to consume it and this is where @Consumes comes into action.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you send me the code for this if possible? this is help me a lot...

    Thanks in advance..
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Come on don't give up. This is easy. You have neared the result. I will give you a hint. Model classes are based on the tables in your db. Repeat after me, Model classes are based on the tables in your db. So, google for conversion of Java objects to JSON objects.
    Priti Mehta wrote:Can you send me the code for this if possible? this is help me a lot...

    I am pretty much working with spring and jsf nowadays. I don't have any codes for web service based on servlets. And it is Code ranch policy, we should not give away codes.
    Check it out here <--- Come on click on this.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I hope this is correct...

    now on client side how do i get same json object using @consumes?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Inside JSon Class
    [{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0}]

    The problem coming is I am getting multiple same records in json array instead of only one.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:

    I hope this is correct...


    Unfortunately no. This is not correct. In your DBClass, use your model to get values from result set.
    What you have done in the below code is correct but it needs a little modification with respect to your model.



    Again here the hint is your model class is based on the table from your db. You are very near don't give up. Just think how you can use your model class in your DBclass after result set.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now i really getting confused with what you are saying we need to use list or json object.
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:Inside JSon Class
    [{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0},{"elevation":160,"uavband":1,"uavname":"Priti","secuplinkfreq":800000000,"priuplinkfreq":789000000,"uavtypeid":123,"detectiontime":1440474895,"azimuth":210,"downlinkfreq":560000000,"downlinkbw":156000,"secuplinkbw":0,"priuplinkbw":0}]

    The problem coming is I am getting multiple same records in json array instead of only one.

    This happens because of the loop here

    Notice that you are retrieving the entrie object from resultSet and adding it to jsonArray. You go ahead with the model class changes I have hinted. You will get your proper results.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Uday seriously I am getting confused on what to use where...as i dont have much grip on this...for couple of years i have been in C, and C++ and never on java. This is my first module in java..
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:Now i really getting confused with what you are saying we need to use list or json object.


    Don't get confused. I will give it to you in steps.

    1) Go to your DBclass processUAVBandRequest() method


    2) Think of how you can use your model class here and then retrieve the result to the following method

    and voila, you have completed.

    Hint: Same hint(Model Class depends on tables in your DB)
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have used model class only which is UAVSearch then where json object will come in picture.

    Ohh God please help me....!!!
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:for couple of years i have been in C, and C++ and never on java. This is my first module in java..

    For starters, you should not have started with Web service as your first module. Now that you have started, complete it and please go back to basic java, servlets and jsp before proceeding.

    Note: My name is Parthe not Uday.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for name...As there was no option for me for the next project allocation so I had to take this module up..I dont want to change my language to java. I am happy with C and CPP only...JAva is too complex.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please somebody help me asap.....
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    AngularJS Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priti Mehta wrote:Java is too complex.

    No java is easy when you compare it to C, CPP. You will have to get the concepts right.

    Priti Mehta wrote:I have used model class only which is UAVSearch then where json object will come in picture.


    What was my hint? Model classes are based on tables in your DB. So,

    1) Your UAVSearch class will represent a table in your database
    2) Your UAVSearch class will contain the variables which are columns in the respective table.
    (ie) your table should have the following columns:
    elevation
    uavband
    uavname
    secuplinkfreq
    priuplinkfreq
    uavtypeid
    detectiontime
    azimuth
    downlinkfreq
    downlinkbw
    secuplinkbw
    priuplinkbw

    So when you retrieve the resultset, do this


    Retrieve this list to your method and then convert it to JSON and add it to the response. Since now its a list of Java objects, it can be easily converted to JSON object.

    A more simpler way would be adding a constructor with all your variables to your Model class as,



    and in result set you can do this directly


    and finally take the list, convert it to JSON object and send it with the response.
    In your client, retrieve it as JSON object, convert it to Java list and get the UAVSearch object and variables associated with it.

    That is it. If you would have thought a little harder on the hint, you would have figured this out yourself.

    Important: XXX here represents the return type of the objects

    Note: The ranch community is going to be mad on me for giving away the code. I apologise to the community.
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey thanks for the help...

    But I am unable to convert list to json object. The examples I saw there they are using Gson which i dont know is correct or not.
    Can you please guide me. And there is no need to write @produces annotations with @POST and @PATH?
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can anybody please help me on this....
     
    Priti Mehta
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ranchers help please..........
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!