Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. import java.sql.SQLException;
  2. import java.util.ArrayList;
  3.  
  4. import javax.ws.rs.Consumes;
  5. import javax.ws.rs.DELETE;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.POST;
  8. import javax.ws.rs.PUT;
  9. import javax.ws.rs.Path;
  10. import javax.ws.rs.PathParam;
  11. import javax.ws.rs.Produces;
  12. import javax.ws.rs.core.MediaType;
  13. import javax.ws.rs.core.Response;
  14.  
  15. import org.springframework.transaction.annotation.Transactional;
  16.  
  17. @Path("/dbuser")
  18. public class DBUserRestServices extends DbConnectionDAO{
  19.  
  20.  
  21. @POST
  22. @Path("/postuser1") //Problem here???
  23. @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
  24. @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
  25. @Transactional
  26. public Response userPOSTOperation( @PathParam("userid") int userid,@PathParam("username") String username,
  27. @PathParam("password") String password) throws SQLException{
  28.  
  29. UserObject userobj = new UserObject(userid, username, password);
  30. username= userobj.getUsername();
  31. password= userobj.getPassword();
  32. System.out.println("UserName " + username + "tt"+ "Password :" + password);
  33.  
  34. dbConnection = new DbConnectionDAO();
  35. String sqlQuery = "INSERT INTO user (username, password) values ('" +"username" +"',"+ "'password" +"')";
  36.  
  37. System.out.println("Query executed : " + sqlQuery );
  38.  
  39. try{
  40. connection = DbConnectionDAO.setDBConnection();
  41. statement = connection.createStatement();
  42. numRowsChanged = statement.executeUpdate(sqlQuery);
  43.  
  44. System.out.println("numRowsChanged : " + numRowsChanged );
  45. if (numRowsChanged<=0)
  46. {
  47. System.out.println("Oops!! The insertion operation failed");
  48. }
  49. else{
  50. System.out.println("The POST operation with username = " + username + ", password "+ password+" has been completed");
  51. }
  52.  
  53. } catch (Exception e)
  54. {
  55. System.out.println(e.getMessage());
  56. }
  57. return Response.ok().build();
  58.  
  59. }
  60.  
  61.  
  62. }
  63.  
  64. //
  65. // The DbConnectionDAO.java file is
  66.  
  67. public class DbConnectionDAO {
  68.  
  69. public static Connection connection;
  70. public static Statement statement;
  71. public static PreparedStatement pst;
  72. public static ResultSet rs = null;
  73. private static String sqlQuery = null;
  74.  
  75. static int numRowsChanged = 0;
  76.  
  77. public static Connection setDBConnection() throws Exception
  78. {
  79. try
  80. {
  81. String connectionURL = "jdbc:mysql://localhost:3306/test";
  82.  
  83. // Connection connection = null;
  84. Class.forName("com.mysql.jdbc.Driver").newInstance();
  85. connection = DriverManager.getConnection(connectionURL, "root", "password");
  86. if (connection != null) {
  87. // System.out.println("Connected to the Database...");
  88. }
  89.  
  90. return connection;
  91. }
  92. catch (SQLException e)
  93. {
  94. throw e;
  95. }
  96. catch (Exception e)
  97. {
  98. throw e;
  99. }
  100.  
  101. }
  102.  
  103.  
  104. // The UserObject.java file is
  105.  
  106. import javax.xml.bind.annotation.XmlElement;
  107. import javax.xml.bind.annotation.XmlRootElement;
  108.  
  109. @XmlRootElement(name = "userObject")
  110. public class UserObject {
  111. private int userid;
  112. private String username;
  113. private String password;
  114.  
  115. public UserObject(){}
  116.  
  117. public UserObject(int userid, String username, String password){
  118. this.userid = userid;
  119. this.username = username;
  120. this.password = password;
  121. }
  122.  
  123. public String getPassword() {
  124. return password;
  125. }
  126. public int getUserid() {
  127. return userid;
  128. }
  129. public String getUsername() {
  130. return username;
  131. }
  132.  
  133. @XmlElement
  134. public void setPassword(String password) {
  135. this.password = password;
  136. }
  137.  
  138. @XmlElement
  139. public void setUserid(int userid) {
  140. this.userid = userid;
  141. }
  142.  
  143. @XmlElement
  144. public void setUsername(String username) {
  145. this.username = username;
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement