Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.SQLException;
- import java.util.ArrayList;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.PUT;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import org.springframework.transaction.annotation.Transactional;
- @Path("/dbuser")
- public class DBUserRestServices extends DbConnectionDAO{
- @POST
- @Path("/postuser1") //Problem here???
- @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
- @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
- @Transactional
- public Response userPOSTOperation( @PathParam("userid") int userid,@PathParam("username") String username,
- @PathParam("password") String password) throws SQLException{
- UserObject userobj = new UserObject(userid, username, password);
- username= userobj.getUsername();
- password= userobj.getPassword();
- System.out.println("UserName " + username + "tt"+ "Password :" + password);
- dbConnection = new DbConnectionDAO();
- String sqlQuery = "INSERT INTO user (username, password) values ('" +"username" +"',"+ "'password" +"')";
- System.out.println("Query executed : " + sqlQuery );
- try{
- connection = DbConnectionDAO.setDBConnection();
- statement = connection.createStatement();
- numRowsChanged = statement.executeUpdate(sqlQuery);
- System.out.println("numRowsChanged : " + numRowsChanged );
- if (numRowsChanged<=0)
- {
- System.out.println("Oops!! The insertion operation failed");
- }
- else{
- System.out.println("The POST operation with username = " + username + ", password "+ password+" has been completed");
- }
- } catch (Exception e)
- {
- System.out.println(e.getMessage());
- }
- return Response.ok().build();
- }
- }
- //
- // The DbConnectionDAO.java file is
- public class DbConnectionDAO {
- public static Connection connection;
- public static Statement statement;
- public static PreparedStatement pst;
- public static ResultSet rs = null;
- private static String sqlQuery = null;
- static int numRowsChanged = 0;
- public static Connection setDBConnection() throws Exception
- {
- try
- {
- String connectionURL = "jdbc:mysql://localhost:3306/test";
- // Connection connection = null;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection(connectionURL, "root", "password");
- if (connection != null) {
- // System.out.println("Connected to the Database...");
- }
- return connection;
- }
- catch (SQLException e)
- {
- throw e;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- // The UserObject.java file is
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "userObject")
- public class UserObject {
- private int userid;
- private String username;
- private String password;
- public UserObject(){}
- public UserObject(int userid, String username, String password){
- this.userid = userid;
- this.username = username;
- this.password = password;
- }
- public String getPassword() {
- return password;
- }
- public int getUserid() {
- return userid;
- }
- public String getUsername() {
- return username;
- }
- @XmlElement
- public void setPassword(String password) {
- this.password = password;
- }
- @XmlElement
- public void setUserid(int userid) {
- this.userid = userid;
- }
- @XmlElement
- public void setUsername(String username) {
- this.username = username;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement