Results 1 to 4 of 4
  1. #1
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    36
    Rep Power
    0

    Default Take data from db and convert to Json help required

    Hello All,

    I am not new to the forum but have not been coding since I left college.

    in Java I need to take data from a db2 database and convert it to json and print it out.

    So far I have data coming back from the database and I can print it to the console, but I am a bit confused as to the steps I need to take to get it to json format.

    I looked on the web and it talks about java script etc...can someone give me some pointers...

    here is part of my sample code
    Java Code:
    String query = querydecision ;
    		ResultSet rs = null;
    		PreparedStatement stmt = null;
    		HashMap<Integer,Double> getSpeed = new HashMap<Integer,Double>();
    
    		
    
    
    		
    		try {
    			
    			
    			conn = this.getIotreportsDB().getConnection();
    		
    			stmt = conn.prepareStatement(query);
    			stmt.setInt(1, LINK_ID);
    		
    			rs = stmt.executeQuery();
    			while (rs.next()){
    				int avg_speed = rs.getInt("avg_speed");
    				String datetime = rs.getString("datetime");
    				String link_name = rs.getString("link_name");
    				System.out.println("link name:" + link_name + "datetime:" + datetime + "avg_speed:" + avg_speed);	
    				
    			
    				
    			    }
    Here is the console return results from the System.out.println

    link name:link 1datetime:2014-Sepavg_speed:23
    link name:link 1datetime:2014-Octavg_speed:45
    link name:link 1datetime:2014-Novavg_speed:45
    link name:link 1datetime:2014-Decavg_speed:56

  2. #2
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: Take data from db and convert to Json help required

    You will have to do a tutorial on Json so you can get up to speed then the rest will fall in place.
    Everything great is a challenge and all the greats were challenged at one time just like us.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: Take data from db and convert to Json help required

    go thru this link..i will help u
    Java MySQL JSON Display Records using Jquery.
    Ramya:cool:

  4. #4
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    36
    Rep Power
    0

    Default Re: Take data from db and convert to Json help required

    Thanks very much for the response. I had a look at the code and I am getting errors loading the

    Gson gson = new Gson();
    String json = gson.toJson(result);
    Caused by: org.apache.cxf.interceptor.Fault: com/google/gson/Gson
    at org.apache.cxf.service.invoker.AbstractInvoker.cre ateFault(AbstractInvoker.java:163)
    ... 1 more
    Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson

    I installed 2 different versions of the jar file from maven but it makes no difference.

    I had a look at the import statement and it is fine

    import com.google.gson.Gson;

    Is their some small reason why it wont run. The only difference I can see is that I have it running from a java class and not a servlet

    Thanks in advance

Similar Threads

  1. Convert Json Format
    By jeffwang66 in forum New To Java
    Replies: 1
    Last Post: 10-29-2014, 01:25 AM
  2. convert xml to JSON using XPath dynamically
    By blertadhimitri in forum New To Java
    Replies: 2
    Last Post: 11-17-2013, 01:40 PM
  3. How to convert xml to json using JSONWriter?
    By blertadhimitri in forum New To Java
    Replies: 3
    Last Post: 11-08-2013, 02:13 PM
  4. could not convert the xml to json using java
    By cgk_js in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2012, 09:31 AM
  5. convert html to json
    By nigina in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 09:38 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
  •