Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package customer;
- import java.io.Serializable;
- import java.sql.Connection;
- import java.sql.DriverManager;
- /**
- *
- * @author
- */
- public class DatabaseOperations implements Serializable
- {
- private static Connection connection;
- public DatabaseOperations()
- {
- try
- {
- String username = "root";
- String password = "root";
- String url = "jdbc:mysql://localhost/test";
- Class.forName ("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection (url, username, password);
- System.out.println("Database connection established");
- }
- catch(Exception e)
- {
- }
- }
- public static Connection getConnection()
- {
- return connection;
- }
- }
- public void addCustomer(CustomerBean customer) throws SQLException {
- DatabaseOperations db = new DatabaseOperations();
- connection = DatabaseOperations.getConnection();
- statement = connection.createStatement();
- String query = "insert into customer (name, address, phone, email) "
- + "values (" + customer.name + ","
- + customer.address + ","
- + customer.phone + ","
- + customer.email + "," + ")";
- statement.executeUpdate(query);
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package customer;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import customer.CustomerBean;
- import javax.servlet.RequestDispatcher;
- /**
- *
- * @author
- */
- public class CustomerServlet extends HttpServlet {
- /**
- * Processes requests for both HTTP
- * <code>GET</code> and
- * <code>POST</code> methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- CustomerBean customer = new CustomerBean();
- try {
- out.println("tests");
- customer.setName(request.getParameter("name"));
- customer.setEmail(request.getParameter("email"));
- customer.setAddress(request.getParameter("address"));
- customer.setPhone(request.getParameter("phone"));
- /************** ADD CUSTOMER TO DB HERE***********************/
- customer.addCustomer(customer);
- request.setAttribute("cust", customer);
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement