使用Netbeans IDE的Java RESTful Webservice CRUD Opreation

时间:2015-06-17 10:56:01

标签: java rest netbeans

我正在使用Java使用数据库中的RESTful Webservice。通过在Netbeans中使用RESTful Webservice from Database选项,它可以生成一些类,因为我们可以公开服务,例如count,{id}​​,{from} / {id}。

我们如何使用Java在Netbeans中编写插入,删除和更新程序。

这是我的工作环境。enter image description here

3 个答案:

答案 0 :(得分:5)

插入代码如下:

@POST
@Path("insertion")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String register(@FormParam("passhash") String passhash, @FormParam("email") String email,@FormParam("$pswdhash") String pwd, @FormParam("phno") String phno) {
    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
        PreparedStatement pst = con.prepareStatement("insert  into  MMX_REGISTRATION(name,email,pswd,phno) values(?,?,?,?)");
        pst.setString(1, passhash);
        pst.setString(2, email);
        pst.setString(3, pwd);
        pst.setString(4, phno);
        int result = pst.executeUpdate();
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "success";
    //return "listform.html";
}

按如下方式检索数据:

@Context private HttpServletRequest request; 
@GET
@Path("session")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String session(@QueryParam("lname") String name1) {
    String response2 = null;
   //String name11 = "praveen";

     //String a[] = null;

    try {

        Class.forName("org.apache.derby.jdbc.ClientDriver");
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
        //PreparedStatement pst = con.prepareStatement("insert  into  restdb_insertion(id,company) values(?,?)");
        //String str1="select * from restdb_insertion where registration=?";
        PreparedStatement pst = con.prepareStatement("select * from MMX_REGISTRATION where name='"+name1+"'");
        System.out.println("select * from MMX_REGISTRATION where name='"+name1+"'");

        ResultSet rs = pst.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int cols = rsmd.getColumnCount();


        while (rs.next()) {
            if(!"null".equals(rs.getString(1))){
                 request.getSession(true);
                 HttpSession session = request.getSession();    
                 session.setAttribute("name","value");
                 session.setAttribute("UserName", rs.getString(2));
                 String username = (String)session.getAttribute("UserName");
                 System.out.println(username);
               //  System.out.println(name);
                 //request.getSession(false);
                 //request.getSession().invalidate();
                 //String user = (String)session.getAttribute("UserName");
                 //System.out.println(user);
                return "success"+" "+username;
            }
        }

       //response = name1;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "fail";

    //"<rss version='2.0'><channel><id>" + id + "</id><cmp>" +response.toArray()[0] + "</cmp></channel></rss>"
}

答案 1 :(得分:3)

也许你应该看一下Spring-Data ...你只需要一个Maven导入,一个包含你需要的所有方法的接口,它将使用你的方法的名称来发出请求......

以下是一个示例:http://spring.io/guides/gs/accessing-data-rest/

答案 2 :(得分:2)

将您的工作分为两层DAO和服务

  • 内部DAO:保留所有数据库交互
  • 内部服务:仅保留您的网络服务

将DAO实现的依赖项注入Web服务层并调用您的CRUD操作(这是EJB概念,您也可以尝试Spring)

相关问题