Java JDBC Mysql syntax error

Asked

Viewed 78 times

0

I have a syntax error here in the mysql query inside the Java code, in the repository. The error is both in the CHANGE method and also in the Delete.

package com.leporonitech.agenda.repositorios.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.leporonitech.agenda.entidades.Contato;
import com.leporonitech.agenda.fabricas.FabricaConexaoJdbc;
import com.leporonitech.repositorios.interfaces.AgendaRepositorio;

public class ContatoRepositorioJdbc implements AgendaRepositorio<Contato> {

    @Override
    public List<Contato> selecionar() throws SQLException, IOException {

        List<Contato> contatos = new ArrayList<Contato>();
        try (Connection conexao = FabricaConexaoJdbc.criarConexao()) {
            Statement comando = conexao.createStatement();
            ResultSet rs = comando.executeQuery("SELECT * FROM contatos");
            while (rs.next()) {
                Contato contato = new Contato();
                contato.setId(rs.getInt("id"));
                contato.setIdade(rs.getInt("idade"));
                contato.setNome(rs.getString("nome"));
                contato.setTelefone(rs.getString("telefone"));
                contatos.add(contato);
            }
        }
        return contatos;
    }

    @Override
    public void inserir(Contato entidade) throws SQLException, IOException {
        Connection conexao = null;
        try {
            conexao = FabricaConexaoJdbc.criarConexao();
            PreparedStatement comando = conexao.prepareStatement("INSERT INTO contatos (nome, idade, telefone) " + " VALUES (?, ?, ?)");
            comando.setString(1, entidade.getNome());
            comando.setInt(2, entidade.getIdade());
            comando.setString(3, entidade.getTelefone());
            comando.execute();
        } finally {
            if (conexao != null) {
                conexao.close();
            }
        }
    }

    @Override
    public void atualizar(Contato entidade) throws IOException, SQLException {
        Connection conexao = null;
        try {
            conexao = FabricaConexaoJdbc.criarConexao();
            PreparedStatement comando = conexao.prepareStatement("UPDATE contatos SET nome=?, idade=?, telefone=?");
            comando.setString(1, entidade.getNome());
            comando.setInt(2, entidade.getIdade());
            comando.setString(3, entidade.getTelefone());
            comando.execute();
        } finally {
            if (conexao != null) {
                conexao.close();
            }
        }
    }

    @Override
    public void excluir(Contato entidade) throws IOException, SQLException {
        Connection conexao = null;
        try {
            conexao = FabricaConexaoJdbc.criarConexao();
            PreparedStatement comando = conexao.prepareStatement("DELETE FROM contatos (nome, idade, telefone)");
            comando.setString(1, entidade.getNome());
            comando.setInt(2, entidade.getIdade());
            comando.setString(3, entidade.getTelefone());
            comando.execute();
        } finally {
            if (conexao != null) {
                conexao.close();
            }
        }
    }
}
  • Post a stacktrace of the bug so we can help you better.

  • In both cases you need to indicate in your query what will be the Contact (in your case) that will be changed or deleted, whether the nome or even a id. You will need to pass as a parameter of these functions an indicator that will be used in your query with the command WHERE. This link can help you https://www.codejava.net/java-se/jdbc/jdbc-tutorial-sql-insert-select-update-and-delete-examples

  • We both realized that you didn’t put the WHERE, so change everything and delete everything, that’s what you want?

No answers

Browser other questions tagged

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