diff --git a/src/main/java/com/roshka/bootcamp/BD.java b/src/main/java/com/roshka/bootcamp/BD.java index 10b1cf1..fdd6d48 100644 --- a/src/main/java/com/roshka/bootcamp/BD.java +++ b/src/main/java/com/roshka/bootcamp/BD.java @@ -43,6 +43,7 @@ public class BD extends HttpServlet { String apellido = rs.getString("apellido"); int cantidad = rs.getInt("Cantidad_factura"); + out.println("----------------------------------------------"); out.println("

NOMBRE = \\" + nombre + "

"); out.println("

APELLIDO = \\" + apellido + "

"); out.println("

CANTIDAD FACTURA = \\" + cantidad + "

"); diff --git a/src/main/java/com/roshka/bootcamp/Cliente.java b/src/main/java/com/roshka/bootcamp/Cliente.java new file mode 100644 index 0000000..4e52446 --- /dev/null +++ b/src/main/java/com/roshka/bootcamp/Cliente.java @@ -0,0 +1,57 @@ +package com.roshka.bootcamp; + +public class Cliente { + private int id; + private String nombre; + private String apellido; + private int nro_cedula; + private String telefono; + + public Cliente() + { + + } + + public Cliente(String nombre, String apellido, int nro_cedula, String telefono) { + this.nombre = nombre; + this.apellido = apellido; + this.nro_cedula = nro_cedula; + this.telefono = telefono; + } + + public int getId() { + return id; + } + + public String getName() { + return nombre; + } + + public void setName(String name) { + this.nombre = name; + } + + public String getApellido() { + return apellido; + } + + public void setApellido(String apellido) { + this.apellido = apellido; + } + + public int getNro_cedula() { + return nro_cedula; + } + + public void setNro_cedula(int nro_cedula) { + this.nro_cedula = nro_cedula; + } + + public String getTelefono() { + return telefono; + } + + public void setTelefono(String telefono) { + this.telefono = telefono; + } +} diff --git a/src/main/java/com/roshka/bootcamp/ClienteServlet.java b/src/main/java/com/roshka/bootcamp/ClienteServlet.java new file mode 100644 index 0000000..1eeae94 --- /dev/null +++ b/src/main/java/com/roshka/bootcamp/ClienteServlet.java @@ -0,0 +1,40 @@ +package com.roshka.bootcamp; + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.io.PrintWriter; + +@WebServlet("/inserteCliente") +public class ClienteServlet extends HttpServlet { + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + int status = 0; + resp.setContentType("text/html"); + + PrintWriter out= resp.getWriter(); + + String nombre=req.getParameter("nombre"); + String apellido=req.getParameter("apellido"); + int nro_cedula= Integer.parseInt(req.getParameter("nro_cedula")); + String telefono=req.getParameter("telefono"); + + Cliente cliente = new Cliente(nombre, apellido, nro_cedula, telefono); + + status = SentenciaInsert.insertCliente(cliente); + + if(status>0){ + out.print("

Record saved successfully!

"); + req.getRequestDispatcher("index.jsp").include(req, resp); + }else{ + out.println("Sorry! unable to save record"); + } + + out.close(); + } +} diff --git a/src/main/java/com/roshka/bootcamp/SentenciaInsert.java b/src/main/java/com/roshka/bootcamp/SentenciaInsert.java new file mode 100644 index 0000000..84cf482 --- /dev/null +++ b/src/main/java/com/roshka/bootcamp/SentenciaInsert.java @@ -0,0 +1,37 @@ +package com.roshka.bootcamp; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; + +public class SentenciaInsert { + static Connection getConnection(){ + Connection con=null; + try{ + Class.forName("org.postgresql.Driver"); + con= DriverManager.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market", + "postgres", "postgres"); + }catch(Exception e){System.out.println(e);} + return con; + } + + public static int insertCliente(Cliente cliente){ + int status=0; + try{ + Connection con=SentenciaInsert.getConnection(); + PreparedStatement ps=con.prepareStatement( + "insert into Cliente(nombre,apellido,nro_cedula,telefono) values (?,?,?,?)"); + ps.setString(1,cliente.getName()); + ps.setString(2,cliente.getApellido()); + ps.setInt(3,cliente.getNro_cedula()); + ps.setString(4,cliente.getTelefono()); + + status=ps.executeUpdate(); + + con.close(); + }catch(Exception ex){ex.printStackTrace();} + + return status; + } + +} diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index b91b855..8a4dd03 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,5 +1,6 @@

Anda a alguna URL

+ Inserte un cliente en la db - + \ No newline at end of file diff --git a/src/main/webapp/inserteCliente.html b/src/main/webapp/inserteCliente.html new file mode 100644 index 0000000..dd6bf02 --- /dev/null +++ b/src/main/webapp/inserteCliente.html @@ -0,0 +1,21 @@ + + + + + Inserte Cliente + + + +
+ +
+ +
+ +
+ +
+ +
+ + \ No newline at end of file diff --git a/target/classes/com/roshka/bootcamp/BD.class b/target/classes/com/roshka/bootcamp/BD.class index fd73819..eecbc90 100644 Binary files a/target/classes/com/roshka/bootcamp/BD.class and b/target/classes/com/roshka/bootcamp/BD.class differ diff --git a/target/classes/com/roshka/bootcamp/Cliente.class b/target/classes/com/roshka/bootcamp/Cliente.class new file mode 100644 index 0000000..9584bed Binary files /dev/null and b/target/classes/com/roshka/bootcamp/Cliente.class differ diff --git a/target/classes/com/roshka/bootcamp/ClienteServlet.class b/target/classes/com/roshka/bootcamp/ClienteServlet.class new file mode 100644 index 0000000..ccb66e3 Binary files /dev/null and b/target/classes/com/roshka/bootcamp/ClienteServlet.class differ diff --git a/target/classes/com/roshka/bootcamp/SentenciaInsert.class b/target/classes/com/roshka/bootcamp/SentenciaInsert.class new file mode 100644 index 0000000..6ed737f Binary files /dev/null and b/target/classes/com/roshka/bootcamp/SentenciaInsert.class differ