2013-11-27 52 views
0

coressponding數據這是我的代碼:怎麼寫JSON網絡服務來獲取從數據庫表中的Java

package dao; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.util.ArrayList; 

import dto.FeedObjects; 


public class Project { 


    public ArrayList<FeedObjects> GetFeeds(Connection connection) throws Exception 
    { 
     ArrayList<FeedObjects> feedData = new ArrayList<FeedObjects>(); 
     try 
     { 
      //String uname = request.getParameter("uname"); 
      PreparedStatement ps = connection.prepareStatement("SELECT id,title,description,url FROM website ORDER BY id DESC"); 
      //ps.setString(1,uname); 
      ResultSet rs = ps.executeQuery(); 
      while(rs.next()) 
      { 
       FeedObjects feedObject = new FeedObjects(); 
       feedObject.SetId(rs.getInt("id")); 
       feedObject.setTitle(rs.getString("title")); 
       feedObject.setDescription(rs.getString("description")); 
       feedObject.setUrl(rs.getString("url")); 
       feedData.add(feedObject); 
      } 
      return feedData; 
     } 
     catch(Exception e) 
     { 
      throw e; 
     } 
    } 

} 

這個培訓班裏越來越數據庫表中的數據和JSON格式轉換:

package webService; 

import java.util.ArrayList; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 

import model.ProjectManager; 

import com.google.gson.Gson; 

import dto.FeedObjects; 

@Path("/WebService") 
public class FeedService { 

    @GET 
    @Path("/GetFeeds") 
    @Produces("application/json") 
    public String feed() { 
     String feeds = null; 
     try { 
      ArrayList<FeedObjects> feedData = null; 
      ProjectManager projectManager = new ProjectManager(); 
      feedData = projectManager.GetFeeds(); 
      Gson gson = new Gson(); 
      feeds = gson.toJson(feedData); 

     } catch (Exception e) { 
      System.out.println("error"); 
     } 
     return feeds; 
    } 

} 

封裝模型;

import java.sql.Connection; 
import java.util.ArrayList; 

import dao.Database; 

import dao.Project; 
import dto.FeedObjects; 

public class ProjectManager { 


public ArrayList<FeedObjects> GetFeeds(String id)throws Exception { 
    ArrayList<FeedObjects> feeds = null; 
    try { 
      Database database= new Database(); 
      Connection connection = database.Get_Connection(); 
      Project project= new Project(); 
      feeds=project.GetFeeds(connection); 

    } catch (Exception e) { 
     throw e; 
    } 
    return feeds; 
} 

} 

還有一個類,我們已經得到了設定值。我能夠顯示使用該URL JSON格式的所有數據庫表的價值,但我希望當我通過ID:

http://localhost:8080/RESTfulProject/REST/WebService/GetFeeds?id=1 

,那麼它應該只顯示一個ID對應的姓名,標題,URL。我已經使用這個

http://www.9lessons.info/2012/10/restful-web-services-json-api.html的例子,但無法做到試過這種plz幫助我

回答

0

你必須基於價值增加@QueryParam("id")String idfeed方法參數,並得到了id的值,並執行操作id和返回JSON string.Please看看下面

@GET 
    @Path("/GetFeeds") 
    @Produces("application/json") 
    public String feed(@QueryParam("id") String id) { 
     String feeds = null; 
     try { 
      ArrayList<FeedObjects> feedData = null; 
      ProjectManager projectManager = new ProjectManager(); 
      // Modify the GetFeeds method by sending the value of id 
      // and prepare feed data based on id 
      feedData = projectManager.GetFeeds(id); 
      Gson gson = new Gson(); 
      // create the json data for the feed data of the corresponding id value 
      feeds = gson.toJson(feedData); 

     } catch (Exception e) { 
      System.out.println("error"); 
     } 
     return feeds; 

    } 

在你GetFeeds ProjectManager,工程類的GetFeeds方法的方法是通過將連接類型的參數調用。修改它來添加字符串類型的參數傳遞的ID從QueryParam值,然後修改查詢,如下

SELECT id,title,description,url FROM website where id = 'ID_VALUE_IN_QUERY_PARAM' 

希望這清楚地說明了一切。這將只返回查詢字符串中傳遞ID值的行。

+0

plz在我的代碼中添加coz新建這樣做 – user2782773

+0

feedData = projectManager.GetFeeds(id);我們如何在這裏只傳遞一個參數public ArrayList GetFeeds(Connection connection)拋出異常 – user2782773

+0

代碼不使用Project類中的GetFeeds,而是使用ProjectManager。你可以修改ProjectManager類中的字符串參數類型'id'的方法GetFeeds – Keerthivasan