com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect integer value: 'null' for column 'YearPublication' at row 1

0

I'm creating a web application about selling comics in java, it turns out that at the moment of "insert" I get that error that showed in the title.

Insert.jsp

HTML code

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insertar</title>
    </head>
    <body>
        <h1 align="center">Insertar datos</h1>
        <form action="">
            <table border="1" width="250" align="center">
                <tr>
                <td>ID</td>
                <td><input type="text" name="txtID"></td>
                </tr>
                <tr>
                <td>Nombre del comic</td>
                <td><input type="text" name="txtNombre"></td>
                </tr>
                <tr>
                <td>ISBN</td>
                <td><input type="text" name="txtISBN"></td>
                </tr>
                <tr>
                <td>Cantidad de paginas</td>
                <td><input type="text" name="txtCantidad"></td>
                </tr>
                <tr>
                <td>Año de publicacion</td>
                <td><input type="text" name="txtAño"></td>
                </tr>
                <tr>
                <td>Autor</td>
                <td><input type="text" name="txtAutor"></td>
                </tr>
                <tr>
                <td>Categoria</td>
                <td><input type="text" name="txtCategoria"></td>
                </tr>
                <tr>
                <td>Editorial</td>
                <td><input type="text" name="txtEditorial"></td>
                </tr>
                <tr>
                <td>Precio</td>
                <td><input type="text" name="txtPrecio"></td>
                </tr><br>
                <td><input type="submit" name="btnEnviar" value="Enviar"></td>
                </tr>
            </table>
        </form>
</body>
</html>

JAVA code:

<% 
        if(request.getParameter("btnEnviar")!=null){
        String id=request.getParameter("txtID");
        String nombreComic=request.getParameter("txtNombre");
        String ISBN=request.getParameter("txtISBN");
        String cantidad=request.getParameter("txtCantidad");
        String año=request.getParameter("txtAño");
        String autor=request.getParameter("txtAutor");
        String categoria=request.getParameter("txtCategoria");
        String editorial=request.getParameter("txtEditorial");
        String precio=request.getParameter("txtPrecio");

        Connection cnx=null;
        ResultSet rs=null;
        Statement sta=null;
            try {
           Class.forName("com.mysql.jdbc.Driver");
           cnx=DriverManager.getConnection
        ("jdbc:mysql://localhost/ventacomics?user=root&password=");
          sta=cnx.createStatement();
          sta.executeUpdate("insert into comics values('"+id+"','"+nombreComic+"','"+ISBN+"','"+cantidad+"','"+año+"','"+autor+"','"+categoria+"','"+editorial+"','"+precio+"')");
          request.getRequestDispatcher("index.jsp");
            }catch(Exception e){out.print(e+"");}
                }


        %>
    
asked by nicolasyo1WWE 19.06.2018 в 00:22
source

1 answer

1

The times that I have seen that error is usually MySql side and means that you are sending a data with greater length or incompatible with the field of the database. In this particular case I would say that it is because in your query:

sta.executeUpdate("insert into comics values('"+id+"','"+nombreComic+"','"+ISBN+"','"+cantidad+"','"+año+"','"+autor+"','"+categoria+"','"+editorial+"','"+precio+"')");

When you assign year you put it in single quotes, which is interpreted as a string. Before this there are two solutions:

  • The quick: Remove the single quotes from the year field on that line.
  • The recommended one: Send the data via parameters in your query.

An example would be:

Connection cnx=null;
ResultSet rs=null;
PreparedStatement sta=null;
try {
       Class.forName("com.mysql.jdbc.Driver");
       cnx = DriverManager.getConnection("jdbc:mysql://localhost/ventacomics?user=root&password=");
       sta = cnx.prepareStatement("Insert Into comics values(?,?,?)");
       sta.setInt(1, id);
       sta.setString(2, nombreComic);
       sta.setString(3, ISBN);
       ...
       sta.executeUpdate();
       sta.close();
       request.getRequestDispatcher("index.jsp");
}catch(Exception e){
       out.print(e+"");
}

The idea is that each unknown represents the position of each parameter, so you should complete the unknowns according to parameters you have.

    
answered by 19.06.2018 в 00:35