I'm trying to make a Java web program that takes data from a form and performs CRUD operations on it in a database using the MVC design pattern... The problem is that the data that is obtained in the jsp page should be directed to the servlet where I do the logic of the CRUD operations... but it tells me that my connection variable is null, therefore I suppose that the program never manages to connect to the database and I don't understand why... Any solution? this is my code... "registro_Usuario.jsp"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro Usuarios</title>
</head>
<body>
<h1> Registro</h1>
<form action="Procesar">
<table>
<tr><td>Cedula</td><td><input type="text" name="CI"></td></tr>
<tr><td>Nombre</td><td><input type="text" name="Nombre"></td></tr>
<tr><td>Apellido</td><td><input type="text" name="Apellido"></td></tr>
<tr><td>Telefono</td><td><input type="text" name="Telefono"></td></tr>
<tr><td>Direccion</td><td><input type="text" name="Direccion"></td></tr>
<tr><td>Estatus</td><td><input type="text" name="estatus"></td></tr>
<tr><td><input type="submit" name="Guardar" value="Guardar"></td></tr>
</table>
</form>
</body>
</html>
"persona.java" package usuario;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import Conectividad.Conexion;
public class persona {
private String cedula;
private String Nombre;
private String Apellido;
private String Direccion;
private String telefono;
private String estatus;
public persona() {
}
public String getCedula() {
return cedula;
}
public void setCedula(String cedula) {
this.cedula = cedula;
}
public String getNombre() {
return Nombre;
}
public void setNombre(String nombre) {
Nombre = nombre;
}
public String getApellido() {
return Apellido;
}
public void setApellido(String apellido) {
Apellido = apellido;
}
public String getDireccion() {
return Direccion;
}
public void setDireccion(String direccion) {
Direccion = direccion;
}
public String getTelefono() {
return telefono;
}
public void setTelefono(String telefono) {
this.telefono = telefono;
}
public String getEstatus() {
return estatus;
}
public void setEstatus(String estatus) {
this.estatus = estatus;
}
public boolean Agregar() {
Conexion conn=new Conexion();
Connection cn= conn.getConexion();
Statement st;
String consulta="INSERT into ejercicio1 (Cedula,Nombre,Apellido,Direccion,Telefono,Estatus) values('"+this.getCedula()+"','"+this.getNombre()+"','"+this.getApellido()+"','"+this.getDireccion()+"','"+this.getTelefono()+"','"+this.getEstatus()+"');";
try {
st=cn.createStatement();
st.executeUpdate(consulta);
} catch (SQLException e) {
return false;
}
return true;
}
public boolean editar(String CI) {
Conexion conn=new Conexion();
Connection cn= conn.getConexion();
Statement st;
String consulta="UPDATE ejercicio1 SET Nombre='"+this.getNombre()+"',Apellido='"+this.getCedula()+"',Direccion='"+this.getDireccion()+"',Telefono='"+this.getTelefono()+"',Estatus='"+this.getEstatus()+"' WHERE Cedula='"+CI.trim()+"';";
try {
st=cn.createStatement();
st.executeUpdate(consulta);
} catch (SQLException e) {
return false;
}
return true;
}
public boolean verificar(String CI) throws SQLException {
Conexion conn=new Conexion();
Connection cn= conn.getConexion();
Statement st;
ResultSet rs;
String consulta="SELECT 1 from ejercicio1 WHERE Cedula='"+CI.trim()+"';";
try {
st=cn.createStatement();
} catch (SQLException e) {
return false;
}
try {
rs=st.executeQuery(consulta);
} catch (SQLException e) {
return false;
}
return rs.next();
}
"Conexion.java" package Conectividad;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Conexion {
private static String servidor;
private static String usuario;
private static String password;
private static String driver;
private static Connection conexion;
private static String st="hola";
public Conexion() {
try {
servidor="jdbc:mysql://localhost:3306/java_EE_2";
usuario="root";
password="WVhermanos19*";
driver="com.mysql.jdbc.Driver";
Class.forName(driver);
conexion=DriverManager.getConnection(servidor,usuario,password);
this.st="Conexion realizda";
}catch(ClassNotFoundException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
st=ex.getMessage();
}catch(SQLException ex) {
st="Conexion fallida";
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
}
}
public Connection getConexion() {
return conexion;
}
public String getSituation() {
return st;
}
}
"Procesar.java" package Controlador;
import java.io.IOException;
import java.sql.SQLException;
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 usuario.persona;
/**
* Servlet implementation class Procesar
*/
@WebServlet(name="Procesar",urlPatterns="/Procesar")
public class Procesar extends HttpServlet {
private static final long serialVersionUID = 1L;
public Procesar() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
persona person= new persona();
person.setCedula(request.getParameter("CI"));
person.setNombre(request.getParameter("Nombre"));
person.setApellido(request.getParameter("Apellido"));
person.setTelefono(request.getParameter("Telefono"));
person.setDireccion(request.getParameter("Direccion"));
person.setEstatus(request.getParameter("estatus"));
if(request.getParameter("CI")!=null) {
try {
if(person.verificar(request.getParameter("CI"))) {
person.editar(request.getParameter("CI"));
}else {
person.Agregar();
response.sendRedirect("registroUsuario.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}