Java RESTful Webservice CRUD Opreation Using Netbeans IDE

I am working on a RESTful Webservice in Java using a database. Using the RESTful Webservice from Database parameter in Netbeans, it generates some classes so that we can expose services, such as count, {id}, {from} / {id}.

How to write a program to insert, delete and update in Netbeans using Java.

This is my work environment. enter image description here

+6
source share
4 answers

Insert code, for example:

@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>" } 
+5
source

Perhaps you should take a look at Spring-Data ... You only have Maven import, an interface with all the necessary methods, and it will use the name of your methods for queries ...

Here is an example: http://spring.io/guides/gs/accessing-data-rest/

+2
source

Separate your work into two layers of DAO and service

  • Inside DAO: save all database interactions.
  • Inside the service: Keep only your web service

Add a dependency of your DAO implementation to your web service layer and invoke CRUD operations (this is an EJB concept, you can also try Spring)

+1
source

You must define all CRUD operations in a calm class. In each method of a calm class, you must call the service interface method, which has another class that has an implementation of ie ServiceImpl. Each method of your Impl service must interact with the Dao layer for CRUD operations. 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: -

 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(); } } 
+1
source

Source: https://habr.com/ru/post/989272/


All Articles