SHARE
TWEET

Untitled

a guest Dec 6th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Servlet;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7.  
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. public class Servlet extends HttpServlet {
  15.     @Override
  16.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17.         // TODO Auto-generated method stub
  18.  
  19.         String name = req.getParameter("name");
  20.         String lastname = req.getParameter("lastname");
  21.         String cedula = req.getParameter("cedula");
  22.  
  23.         try {
  24.             InsertClient(name, lastname, cedula);
  25.         } catch (Exception e) {
  26.             // TODO Auto-generated catch block
  27.             e.printStackTrace();
  28.         }
  29.  
  30.         resp.getWriter().println("<html>");
  31.         resp.getWriter().println("<head>");
  32.         resp.getWriter().println("<title>Cliente agregado!</title>");
  33.         resp.getWriter().println("</head>");
  34.         resp.getWriter().println("<body>");
  35.         resp.getWriter().println("Cliente fue agregado.");
  36.         resp.getWriter().println("</body>");
  37.         resp.getWriter().println("</html>");
  38.     }
  39.  
  40.     public static void InsertClient(String Name, String Lastname, String Cedula) throws Exception{      
  41.         try{
  42.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "1990");
  43.  
  44.             PreparedStatement insert = con.prepareStatement("INSERT INTO Cliente(Nombre, Apellido, Cedula) VALUES ('"+Name+"', '"+Lastname+"', '"+Cedula+"')");
  45.  
  46.             insert.executeUpdate();
  47.  
  48.         }catch(Exception e){System.out.println(e);}
  49.         finally{System.out.println("Insertion complete.");}
  50.  
  51.  
  52.     }  
  53.  
  54. }
  55.    
  56. Class.forName("com.mysql.jdbc.Driver");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top