Java / MySQL - Error in query

0

Hello, I'm trying to make a simple application to register with the bank with Java and I'm 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

ADVERTISEMENT:

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 that I create in the SQL String, but I can not identify what is wrong. I already tested the same inside the database, I already put the whole query without StringBuffer, I placed / took the parentheses after values.

    
asked by anonymous 14.02.2018 / 22:25

0 answers