• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting data from database using ajax

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a web application using html/css/js/ajax, servlet,jsp and hibernate.

I have a method which returns Person's object, (containing properties like id, name, address, contacts). These properties has the value fetched from database.

Now, when the user enters the id in html page, i need to get his data like name , address, contacts , or rather a person object with the id entered.
For that i am using ajax.

here is the code :



Now, i don't know how to proceed.

If someone can provide a link of complete example where user gets the data from db in ajax, and sets their value in respective textfields
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of all your method which returns a Person (?) object is a Java method which runs on the server, isn't that correct?

So to send that Person object back to your AJAX code which sent the request, you'll need to convert it to something which can be sent as an HTTP response. That means text. You might choose JSON or XML or some other text format (but I would not invent my own wheel in this case).

Then you'll need to extract the information from that text in your Javascript code and deal with it according to your requirements.

And I would not recommend writing your own AJAX-processing code in the year 2012. Use jQuery to deal with all of the petty details and concentrate on writing code which deals with Persons and so on.

You should be able to find tutorials about AJAX and DHTML online fairly easily. But don't expect them to deal with getting data from databases because Javascript doesn't do that. If you want more extensive examples, I have "Ajax In Practice" on my bookshelf and I found it to be a good survey of the techniques you're asking about.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for your reply
But, i am very confused now. Shall i go for hitting the server (refreshing the page) and then load the data or JQuery? AJax?
I am unknown to JQUERY, so, with jquery can i query the database and get the details and put it into file (jsp, much better than html)??
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Ajax and jQuery (used to make Ajax easy) simply create a request to the server. Returning JSON is the easiest way to get info back to the page.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear.
That means whenever i return the object to ajax code, i will go for JSON. It will fetch the data, and put it in textfields. (purpose behind putting it into textfields is to give user to edit the details)
Am i right?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it will fetch the data and give it to your code to do whatever you want with it. If the data is to be used to populate text fields, that's up to you.

Using jQuery, the $.getJson() method is very useful for this.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not aware of jquery and json. So, if you can suggest any book or site for that
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe I did exactly that in an earlier post in this thread.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kunal Lakhani wrote:I am not aware of jquery and json. So, if you can suggest any book or site for that



My book: jQuery in Action

Chapter 8 is all about Ajax.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Thanks Paul, Bear. Will go for Jquery in Action and jquery with Ajax
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I am going through jQuery. Saw the examples on jquery on ajax. My class has a function which returns Person's object. Still, wonder, how can i get that person's object inside ajax code and use it getters to get the values?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Ajax request is like any other. It sends a request and expects a response. That response can be a JSON serialization of your data.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kunal Lakhani wrote:how can i get that person's object inside ajax code and use it getters to get the values?



Remember that the Java object is not passed back to the Ajax caller. You are using HTTP so it must be converted to text first. Hence why you might convert it to JSON format.

And then remember that the JSON data is being received by Javascript code. If you were thinking that at this point you would have a Java object whose methods you could call, that's not the case. You have a JSON representation of the object which you can extract data from using Javascript code.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply paul. What i understood is call the java class and so the method. Get the object and get the data through its properties and then convert it into json and send it back to ajax code.
Json code will be implemented in java class . Am i right?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kunal Lakhani wrote:
Json code will be implemented in java class . Am i right?


I would not write JSON conversion as part of any class -- there are many JSON libraries you can use. Gson, Jackson, Stringtree, etc...

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cannot google due to some limitations. So,after getting the data through properties,what these libraries will do? If some one can provide me a link of dealing with objects,json and ajax in java
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gson
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear

Here is what i tried (before json gets into action)


This url, inside ajax code, calls my servlet

Servlet


I am using Hibernate

DAO class



This Code then runs



But, when i change my dao class to include Hibernate related objects , like this

DAO Class


The code "System.out.println(policynumber); " doesn't prints anything and the failure code in ajax runs with "alert(request.statusText);" as "Internal server error"

Still, wonder why?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the servlet or other backend code is failing, that needs to be diagnosed. It has nothing to do with the client. A 500 error usually means that an unexpected error has been thrown. Starts with the server logs.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem Solved. Actually, i committed a very silly mistake.
I am using a Hibernate utility class to get the session. But in my cfg.xml i forgot to configure thread for current session , i.e "<property name="current_session_context_class">thread</property>"


Thanks Bear. Now, looking forward to JSON
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved
Thanks to all
reply
    Bookmark Topic Watch Topic
  • New Topic