Sentencia cliente terminada

parent bb365c60
...@@ -43,6 +43,7 @@ public class BD extends HttpServlet { ...@@ -43,6 +43,7 @@ public class BD extends HttpServlet {
String apellido = rs.getString("apellido"); String apellido = rs.getString("apellido");
int cantidad = rs.getInt("Cantidad_factura"); int cantidad = rs.getInt("Cantidad_factura");
out.println("----------------------------------------------");
out.println("<p>NOMBRE = \\" + nombre + "</p>"); out.println("<p>NOMBRE = \\" + nombre + "</p>");
out.println("<p>APELLIDO = \\" + apellido + "</p>"); out.println("<p>APELLIDO = \\" + apellido + "</p>");
out.println("<p>CANTIDAD FACTURA = \\" + cantidad + "</p>"); out.println("<p>CANTIDAD FACTURA = \\" + cantidad + "</p>");
......
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;
}
}
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("<p>Record saved successfully!</p>");
req.getRequestDispatcher("index.jsp").include(req, resp);
}else{
out.println("Sorry! unable to save record");
}
out.close();
}
}
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;
}
}
<html> <html>
<body> <body>
<h2>Anda a alguna URL</h2> <h2>Anda a alguna URL</h2>
<a href="./inserteCliente.html"> Inserte un cliente en la db </a>
</body> </body>
</html> </html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inserte Cliente</title>
</head>
<body>
<form method="post" action="inserteCliente">
<label>Nombre: </label>
<input type="text" name="nombre"><br/>
<label>Apellido: </label>
<input type="text" name="apellido"><br/>
<label>Numero de Cedula: </label>
<input type="number" name="nro_cedula"><br/>
<label>Telefono: </label>
<input type="text" name="telefono"><br/>
<input type="submit">
</form>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment