简体   繁体   English

使用Netbeans IDE的Java RESTful Webservice CRUD Opreation

[英]Java RESTful Webservice CRUD Opreation Using Netbeans IDE

I'm working on RESTful Webservice in Java Using Database. 我正在使用Java使用数据库中的RESTful Webservice。 By using RESTful Webservice from Database option in Netbeans it generates some Classes by that we are able to expose service like count,{id},{from}/{id}. 通过在Netbeans中使用RESTful Webservice from Database选项,它生成了一些类,我们可以通过它来公开服务,如count,{id}​​,{from} / {id}。

How do we write program to Insert,Delete and Update in Netbeans using Java. 我们如何使用Java编写Netbeans中的插入,删除和更新程序。

This is my working environment. 这是我的工作环境。 在此输入图像描述

Insertion Code like follows: 插入代码如下:

@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";
}

Retrieving Data as follows: 检索数据如下:

@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>"
}

Maybe you should take a look on Spring-Data... You just have a Maven import, an interface with all the methods you need and it will use the name of your methods to make requests... 也许你应该看看Spring-Data ...你只需要一个Maven导入,一个包含你需要的所有方法的接口,它将使用你的方法的名称来发出请求......

Here's an example : http://spring.io/guides/gs/accessing-data-rest/ 这是一个例子: http//spring.io/guides/gs/accessing-data-rest/

Separate your work into two layers DAO and service 将您的工作分为两层DAO和服务

  • Inside DAO: Keep all the database interactions 在DAO中:保持所有数据库交互
  • Inside Service: Keep only your webservice 内部服务:仅保留您的Web服务

Inject the dependency of your DAO implementation into your web service layer and invoke your CRUD operations(this is EJB concept ,you can also try Spring) 将DAO实现的依赖项注入Web服务层并调用CRUD操作(这是EJB概念,您也可以尝试Spring)

You should define all your CRUD operations in a restful class. 您应该在一个宁静的类中定义所有CRUD操作。 In each method of restful class you should call a service interface method which has another class which has the implementation ie ServiceImpl. 在restful类的每个方法中,你应该调用一个服务接口方法,该方法有另一个具有实现的类,即ServiceImpl。 Each method of your service Impl should interact with Dao layer for CRUD operations. 您的服务Impl的每个方法都应该与Dao层交互以进行CRUD操作。 You should avoid loading the driver class again and again for each CRUD operation and you should define it in a separate method/static block like this:- 你应该避免为每个CRUD操作反复加载驱动程序类,你应该在一个单独的方法/静态块中定义它,如下所示: -

static Connection con;
static{
 try {
    Class.forName("org.apache.derby.jdbc.ClientDriver");
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "app", "app");
} catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
}
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM