Java/Mysql - Query error

Asked

Viewed 135 times

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 String as '?

  • I don’t know, how do I know?

  • Would be the string "INSERT INTO Livros" that is missing a space after the "Livros"?

  • 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

  • Your table name is Books?

  • Yes, and just for the record, both in the bank and in the code is "books" all minuscule

  • What version of Mysql and driver you are using?

  • Mysql Workbech 6.3, driver com.mysql.jdbc.Driver

  • But what about the Mysql Server version and the Mysql Driver version?

  • 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

  • Use SELECT VERSION(); or go to the Mysql server installation folder, its name contains the version.

  • ok, the Mysql Server version is 5.7.20

  • Try using a driver most recent, such as 6.0.6. If you do not use a dependency manager, download it here.

  • Okay, tomorrow night I’ll be and do an update here

  • 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

Show 10 more comments
No answers

Browser other questions tagged

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