0
Hello, I am trying to make a simple application to register in the bank with Java and I am getting the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Count Zero', 234, 'Gibson, W', 'Aleph' )' at line 1
Bookoid:
public void abrirConexao() throws ClassNotFoundException, SQLException {
    try {
        con = Conexao.getConnection();
    }catch (SQLException e) {
        e.printStackTrace();
    }
}
@Override
public void adicionar(Livro livro) throws SQLException, ClassNotFoundException {
    abrirConexao();
    //Livro li = (Livro) livro;
    PreparedStatement stmt = null;
    try {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO livros");
        sql.append("(titulo, paginas, autor, editora) ");
        sql.append("values ");
        sql.append("(?, ?, ?, ?)");
        stmt = con.prepareStatement(sql.toString());
        System.out.println(sql);
        stmt.setString(1, livro.getTitulo());
        //System.out.println(livro.getTitulo());
        stmt.setInt(2, livro.getPaginas());
        //System.out.println(livro.getPaginas());
        stmt.setString(3, livro.getAutor());
        stmt.setString(4, livro.getEditora());
        stmt.executeUpdate();
    }catch(SQLException e) {
        e.printStackTrace();
    }
}
Servlet:
public Servlet() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Livro li = new Livro(request.getParameter("titulo")
                            ,new Integer(request.getParameter("paginas"))
                            ,request.getParameter("autor")
                            ,request.getParameter("editora"));
        System.out.print(li.toString());
        LivroDAO dao = new LivroDAO();
        dao.adicionar(li);
        System.out.println(request.getParameter("titulo"));
        System.out.println(request.getParameter("paginas"));
        System.out.println(request.getParameter("autor"));
        System.out.println(request.getParameter("editora"));
        request.getRequestDispatcher("Index.jsp").forward(request, response);
    } catch(Exception e) {
        e.printStackTrace();
        System.out.println("Erro");
        request.getRequestDispatcher("Index.jsp").forward(request, response);
    }
}
From what I understand, the error is in the query I create in the SQL String, but I cannot identify what is wrong. I have tested it inside the bank, I have put the whole query without Stringbuffer, I put/took the parentheses after values.
Your bank is using a separator of
Stringas'?– Sorack
I don’t know, how do I know?
– Matheus Torres
Would be the string
"INSERT INTO Livros"that is missing a space after the"Livros"?– Piovezan
After Voce spoke I tested just for collateral, but neither was it. Before using String Builder I was using a normal String and pointed out the same error
– Matheus Torres
Your table name is Books?
– Edjane
Yes, and just for the record, both in the bank and in the code is "books" all minuscule
– Matheus Torres
What version of Mysql and driver you are using?
– Felipe Marinho
Mysql Workbech 6.3, driver
com.mysql.jdbc.Driver– Matheus Torres
But what about the Mysql Server version and the Mysql Driver version?
– Felipe Marinho
Forgive the lack of knowledge, but how do I check the versions? If you are referring to mysql jar file, it would be the
mysql-connector-java-5.1.45– Matheus Torres
Use
SELECT VERSION();or go to the Mysql server installation folder, its name contains the version.– Felipe Marinho
ok, the Mysql Server version is
5.7.20– Matheus Torres
Try using a driver most recent, such as 6.0.6. If you do not use a dependency manager, download it here.
– Felipe Marinho
Okay, tomorrow night I’ll be and do an update here
– Matheus Torres
I don’t know what witchcraft you did, but that’s right, Felipe. I updated the driver and was first, thank you very much friend, took even a weight from me because I was for more than a week without producing anything hahaha
– Matheus Torres