我有这个servlet:
import DAO.ConexionBDD;
import DAO.Operaciones;
import Modelos.Votante;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Lux
*/
public class CreaVotante 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, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
Votante v=new Votante(request.getParameter("nif"), request.getParameter("clave"));
Operaciones op=new Operaciones();
op.registrar(v, Conexion);
out.println("<html>");
out.println("<head><title>Title</title></head>");
out.println("<body>");
out.println("Hi");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(CreaVotante.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(CreaVotante.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
private Connection Conexion;
public void init() throws ServletException {
/* Establecemos la conexión, si no existe */
try{
ConexionBDD ConexBD=ConexionBDD.GetConexion();
Conexion=ConexBD.GetCon();
}
catch(ClassNotFoundException cnfe){
}
catch(SQLException sqle){
}
}
}
当我打电话给op.registrar(v,Conexion)时;它停止工作,不打印调用下的内容但是当我没有那个电话时它会打印出所有内容。 这是函数注册商:
public int registrar(Votante _Votante, Connection _Conexion) throws SQLException {
int resultado;
String nif=_Votante.getNif();
String clave=_Votante.getClave();
try{
Statement s = _Conexion.createStatement();
resultado=s.executeUpdate ("INSERT INTO votantes (nif, clave) VALUES ('nif', 'clave')");
}
catch(SQLException SQLE){
throw new SQLException(SQLE);
}
if (resultado==0)throw new SQLException();
return resultado;
}
编辑:每当我调用需要连接数据库的函数时,它都不起作用。 这是Connection类:
import java.sql.*;
public class ConexionBBDD {
private static ConexionBBDD UnicaConexion=null;
private Connection Conex;
private ConexionBBDD() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/bd_votaciones_inn";
Conex = DriverManager.getConnection(connectionUrl,"root","root");
}
public synchronized static ConexionBBDD GetConexion() throws ClassNotFoundException, SQLException{
if(UnicaConexion == null){
UnicaConexion = new ConexionBBDD();
}
return UnicaConexion;
}
public Connection GetCon(){
return Conex;
}
public void Destroy() throws SQLException{
Conex.close();
}
}
答案 0 :(得分:0)
原因似乎是registrar
方法抛出异常,该异常会冒泡到doPost
或doGet
方法。
由于processRequest
方法没有捕获异常,因此永远不会执行调用registrar
之后的行。
您有一些日志语句应包含有关特定错误的更多详细信息。