Save Date to a mysql database using Java

Asked

Viewed 81 times

-2

I am creating an application that has a mysql database containing a table of banks and another of banking movements.

The bank movements table has the following attributes:

Bench

Movement date

Description

Valor

The program that creates the moves is the one that comes further down, but you have a syntax error in the statement "pstm.setDate(2, date);" (in the Audi doPost zone) . What is failing?

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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("/CriarMovimentosBancos")
public class CriarMovimentosBancos extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int banco=Integer.parseInt(request.getParameter("id"));
            PrintWriter out=response.getWriter();
            out.println ("<html>");
            out.println ("<head>");
            out.println("<title>Criar Movimentos Banco</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form method='POST'>");
            out.println("Banco:<br><input type='int' name='txtBanco' readonly='readonly' value='"+banco+"'>");
            out.println("<br>");
            out.println("Data:<br><input  type='date' name='txtData'>");
            out.println("<br>");
            out.println("Descrição:<br><input type='text' name='txtDescricao' ");
            out.println("<br>");
            out.println("<br>");
            out.println("Valor:<br><input type='number' placeholder='0.00' min='0' name='txtvalor'");
            out.println("<br>");
            out.println("<br>");
            out.println("<input type='submit' value='Validar'>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
         }  catch (IOException e) {
            }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter ();
        int banco  = Integer.parseInt(request.getParameter("txtBanco"));
            try {
        DateFormat formatter;
                formatter = new SimpleDateFormat("dd-MM-yyyy");
                Date data = formatter.parse(request.getParameter("txtData"));
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        String descricao = request.getParameter("txtDescricao");
        Double valor = Double.valueOf(request.getParameter("txtvalor"));
        try {
            Class.forName("com.mysql.jdbc.Driver"); 
            String SQL="Insert into mov__banco(banco,  data,  descricao, valor) Values(?,?,?,?)";
                    Connection  conn =  DriverManager.getConnection("jdbc:mysql://localhost/Contabilidade_Familiar", "root", "1234");
                    PreparedStatement pstm = conn.prepareStatement(SQL);
                    pstm.setInt(1,banco);
                    pstm.setDate(2, date);
                    pstm.setString(3,descricao);
                    pstm.setDouble(4,valor);
                    pstm.execute();
                    pstm.close();
                    conn.close();
response.sendRedirect("http://localhost:8080/Contabilidade/MovimentosBanco?id="+banco);
                } catch (Exception e ) {
                    out.println("Erro - "+e.getMessage());
                }
    }
}

1 answer

0

Try it this way:

pstm.setDate(2, new java.sql.Date(date.getTime());

The method gets a java.sql.Date as parameter and not java.util.Date

  • Thanks for your help, but unfortunately I haven’t been able to get it to work yet. The "doGet" date is not being recognised, even though I extracted it with the instructions: Try { Dateformat Formatter; Formatter = new Simpledateformat("dd-MM-yyyy"); Date data = Formatter.parse(request.getParameter("txtData"); } catch (Parseexception E1) { E1.printStackTrace(); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.