aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Getting data from database using ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Getting data from database using ajax" Watch "Getting data from database using ajax" New topic
Author

Getting data from database using ajax

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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


kunal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

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

Joined: Jun 05, 2010
Posts: 618
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)??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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

Joined: Jun 05, 2010
Posts: 618
I am not aware of jquery and json. So, if you can suggest any book or site for that
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

I believe I did exactly that in an earlier post in this thread.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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

Joined: Jun 05, 2010
Posts: 618
Okay. Thanks Paul, Bear. Will go for Jquery in Action and jquery with Ajax
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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
Bartender

Joined: Oct 14, 2005
Posts: 18113
    
    8

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

Joined: Jun 05, 2010
Posts: 618
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

Joined: Jun 05, 2010
Posts: 618
Thanks Bear
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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

Joined: Jun 05, 2010
Posts: 618
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

Gson
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

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

Joined: Jun 05, 2010
Posts: 618
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

Joined: Jun 05, 2010
Posts: 618
Solved
Thanks to all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting data from database using ajax
 
Similar Threads
how to use ajax
A problem in hiding a button
Displaying a Greeting message using Ajax
ajax not working with jsp
jsp login page with normal button, not submitt button