Java 调用函数时Servlet没有响应
我有一个servlet:Java 调用函数时Servlet没有响应,java,function,servlets,Java,Function,Servlets,我有一个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.lo
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.register(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;
}
编辑:每当我调用需要连接到数据库的函数时,它都不起作用。
这是连接类:
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();
}
}
原因似乎是
register
方法抛出了一个异常,该异常会出现在doPost
或doGet
方法中。
由于在processRequest
方法上没有捕获异常,因此调用register
后的行永远不会执行
您有一些日志语句应该包含有关特定错误的更多详细信息。不相关,但为什么catch然后立即重新抛出SQLException?在任何情况下,听起来您都需要调试
register
。实际上,我刚刚发现问题出在与数据库的连接上。