我无法从Java中的servlet在postgresql中插入数据

时间:2019-05-12 21:58:21

标签: java postgresql tomcat servlets netbeans

我到处搜索,但无法解决我的问题: 事实证明,到目前为止,我正在尝试使用postgreSQL将页面与数据库连接,但日志中出现了问题,但是设法解决了这似乎是tomcat问题,现在我正在尝试让某人进行注册,但在在输入数据时,页面会检测到该页面是否已注册,但是当我在数据库中签入时,该页面未保存。我用来从servlet插入主类中问题的方法,它可以工作,但是由于某些奇怪的原因,它没有将其保存在数据库中。

我的课堂上有我的插入方法

public class GestorBD {
    Connection conectar;
public void cerrarconexion() 
    {
        try {
            conectar.close();
        } catch (Exception ex) {
            Logger.getLogger(GestorBD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void registrar(String cuenta,String nombre,String contraseña,String Mail) 
    {

          String query="INSERT INTO usuarios.usuarios(usuario,contraseña,nombre,email) VALUES (?,?,?,?);
          PreparedStatement statement;
        try {
                statement = getconnection().prepareStatement(query);
                statement.setString(1,cuenta);
                statement.setString(2,nombre);
                statement.setString(3,contraseña);
                statement.setString(4,Mail);
                statement.executeUpdate();
                statement.close();
                cerrarconexion();
        } 
        catch (Exception ex) {
            Logger.getLogger(GestorBD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

在其中输入数据的jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registro</title>
    </head>
    <body>
        <%@page import="controller.Registro"%>
        <%@page import="controller.GestorBD"%>
        <%GestorBD gs=new GestorBD();%>
        <h1>Registra tus datos</h1>
        <form action="Registro" method="post">
        <table cellpaddin=2 cellspacing=2 border=2>
            <tr>    
                <td>Cuenta:</td>
                <td><input type="text" name="Cuenta"></td>
            </tr>
             <tr>    
                <td>Nombre:</td>
                <td><input type="text" name="Nombre"></td>
            </tr>
             <tr>    
                <td>Contraseña:</td>
                <td><input type="text" name="Contrasena"></td>
            </tr>
             <tr>    
                <td>Mail:</td>
                <td><input type="text" name="Mail"></td>
            </tr>
        </table>
            <input type="reset" value="Borrar">
            <input type="submit" value="Guardar"> 
        </form>
        <br>
        <form action="index.jsp" method="post"><input type="submit" value="Regresar"</form>
    </body>
</html>

我在其中使用插入方法的servlet

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Registro", urlPatterns ={"/Registro"})
public class Registro extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
             GestorBD gestor=new GestorBD();
             String Cuenta=request.getParameter("Cuenta");
             String Nombre=request.getParameter("Nombre");
             String Contrasena=request.getParameter("Contrasena");
             String Mail=request.getParameter("Mail");

             gestor.registrar(Cuenta, Nombre, Contrasena, Mail);

             request.setAttribute("Cuenta",Cuenta);
             request.setAttribute("Nombre",Nombre);
             request.setAttribute("Contrasena",Contrasena);
             request.setAttribute("Mail",Mail);
             request.getRequestDispatcher("/RegistroCompleto.jsp").forward(request,response);
        }
        finally
        {
            out.close();
        }
    }



    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
        processRequest(request,response);
    }
    @Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
        processRequest(request,response);
    }

}

0 个答案:

没有答案