Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package customer;
  6.  
  7. import java.io.Serializable;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10.  
  11. /**
  12. *
  13. * @author
  14. */
  15. public class DatabaseOperations implements Serializable
  16. {
  17. private static Connection connection;
  18. public DatabaseOperations()
  19. {
  20. try
  21. {
  22. String username = "root";
  23. String password = "root";
  24. String url = "jdbc:mysql://localhost/test";
  25. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  26. connection = DriverManager.getConnection (url, username, password);
  27.  
  28. System.out.println("Database connection established");
  29. }
  30. catch(Exception e)
  31. {
  32.  
  33. }
  34. }
  35.  
  36. public static Connection getConnection()
  37. {
  38. return connection;
  39. }
  40. }
  41.  
  42. public void addCustomer(CustomerBean customer) throws SQLException {
  43. DatabaseOperations db = new DatabaseOperations();
  44. connection = DatabaseOperations.getConnection();
  45. statement = connection.createStatement();
  46.  
  47. String query = "insert into customer (name, address, phone, email) "
  48. + "values (" + customer.name + ","
  49. + customer.address + ","
  50. + customer.phone + ","
  51. + customer.email + "," + ")";
  52.  
  53. statement.executeUpdate(query);
  54. }
  55.  
  56. /*
  57. * To change this template, choose Tools | Templates
  58. * and open the template in the editor.
  59. */
  60. package customer;
  61.  
  62. import java.io.IOException;
  63. import java.io.PrintWriter;
  64. import javax.servlet.ServletException;
  65. import javax.servlet.http.HttpServlet;
  66. import javax.servlet.http.HttpServletRequest;
  67. import javax.servlet.http.HttpServletResponse;
  68. import customer.CustomerBean;
  69. import javax.servlet.RequestDispatcher;
  70.  
  71. /**
  72. *
  73. * @author
  74. */
  75. public class CustomerServlet extends HttpServlet {
  76.  
  77. /**
  78. * Processes requests for both HTTP
  79. * <code>GET</code> and
  80. * <code>POST</code> methods.
  81. *
  82. * @param request servlet request
  83. * @param response servlet response
  84. * @throws ServletException if a servlet-specific error occurs
  85. * @throws IOException if an I/O error occurs
  86. */
  87. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  88. throws ServletException, IOException {
  89. response.setContentType("text/html;charset=UTF-8");
  90. PrintWriter out = response.getWriter();
  91.  
  92. CustomerBean customer = new CustomerBean();
  93. try {
  94. out.println("tests");
  95.  
  96. customer.setName(request.getParameter("name"));
  97. customer.setEmail(request.getParameter("email"));
  98. customer.setAddress(request.getParameter("address"));
  99. customer.setPhone(request.getParameter("phone"));
  100.  
  101. /************** ADD CUSTOMER TO DB HERE***********************/
  102. customer.addCustomer(customer);
  103.  
  104. request.setAttribute("cust", customer);
  105. request.getRequestDispatcher("/index.jsp").forward(request, response);
  106. }
  107. catch (Exception e)
  108. {
  109. e.printStackTrace();
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement