Error copying data from one table to another

Asked

Viewed 86 times

0

Good afternoon Guys, I’m making an application that makes a Dvs loan request, I managed to make the application to enter the reservation, now I need to make the dvd owner confirm the rental and copy the data in the rented dvd table but I have no idea if this is right, because only error comes out when I try to copy the data to the rented table.

Here I requested the loan and this working:

public class Apply for loan {

private static String horaPedido;

private static String dataPedido;

public void solicitarEmprestimo(ClubeDoDvdPedidoEmprestimo e) throws SQLException{
    StringBuilder sql = new StringBuilder();
    sql.append("INSERT INTO solicitacaoemprestimo ");
    sql.append("(dataemprestimo,horaemprestimo,  codigo_socio_solicitou_emprestimo, dvd_codigo, socio_codigo) ");
    sql.append("VALUES (?, ?, ?, ?, ?) ");

    Connection conexao = FabricaDeConexao.conectar();
    PreparedStatement comando = conexao.prepareStatement(sql.toString());

    comando.setString(1, e.solicitarDataPedido());
    comando.setString(2, e.solicitarHoraPedido());
    comando.setLong(3, e.getCodigoSocioSolicitouEmprestimo());
    comando.setLong(4, e.getDvd().getCodigo());
    comando.setLong(5, e.getSocio().getCodigo());

    comando.executeUpdate();


}

public List<ClubeDoDvdPedidoEmprestimo> selicionarPedidosDeEmprestimo(ClubeDoDvdPedidoEmprestimo clube) throws SQLException{
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT codemprestimo, dataemprestimo, horaemprestimo, codigo_socio_solicitou_emprestimo, dvd_codigo, socio_codigo ");
    sql.append("FROM solicitacaoemprestimo ");
    sql.append("WHERE socio_codigo = ? ");
    sql.append("ORDER BY dataemprestimo ASC, horaemprestimo ASC ");

    Connection conexao = FabricaDeConexao.conectar();

    PreparedStatement comando = conexao.prepareStatement(sql.toString());
    comando.setLong(1, clube.getSocio().getCodigo());

    ResultSet resultado = comando.executeQuery();

    List<ClubeDoDvdPedidoEmprestimo> retorno = new ArrayList<>();

    while(resultado.next()){

        final ClubeDoDvdPedidoEmprestimo cdpe = new ClubeDoDvdPedidoEmprestimo();
        cdpe.setCodEmprestimo((resultado.getLong("codemprestimo")));
        cdpe.setDataPedido(resultado.getString("dataemprestimo"));
        cdpe.setHoraPedido(resultado.getString("horaemprestimo"));
        cdpe.setCodigoSocioSolicitouEmprestimo(resultado.getLong("codigo_socio_solicitou_emprestimo"));
        Socio s = new Socio();
        s.setCodigo(resultado.getLong("socio_codigo"));
        cdpe.setSocio(s);

        Dvd d = new Dvd();
        d.setCodigo(resultado.getLong("dvd_codigo"));
        cdpe.setDvd(d);

        retorno.add(cdpe);
    }
    return retorno;

}

Here I tried to make the loan request table data go to rented DVD table:

public class Dvdrented {

public void dvdAlugados(Alugados e) throws SQLException{
    StringBuilder sql = new StringBuilder();
    sql.append("INSERT INTO alugados (dataemprestimo, horaemprestimo, solicitacaoemprestimo_codemprestimo, solicitacaoemprestimo_dvd_codigo, solicitacaoemprestimo_socio_codigo) ");
    sql.append("SELECT dataemprestimo, horaemprestimo, codemprestimo ,dvd_codigo, socio_codigo ");
    sql.append("FROM solicitacaoemprestimo ");
    sql.append(" WHERE codemprestimo = ? ");

    Connection conexao = FabricaDeConexao.conectar();
    PreparedStatement comando = conexao.prepareStatement(sql.toString());

    comando.setLong(1, e.getClubeDoDvdEmprestimo().getCodEmprestimo());
    comando.setString(2, e.getDataEmprestimo());
    comando.setString(3, e.getHoraEprestimo());
    comando.setLong(4, e.getClubeDoDvdEmprestimo().getCodEmprestimo());
    comando.setLong(5, e.getClubeDoDvdEmprestimo().getDvd().getCodigo());
    comando.setLong(6, e.getClubeDoDvdEmprestimo().getSocio().getCodigo());

    comando.executeUpdate();

}
  • When so, try to post the exession Stacktrace that occurs tb.

1 answer

0

Your SQL in the "dvdAlugados" method only asks you to provide the loan code (codemprestimo):

sql.append("INSERT INTO alugados (dataemprestimo, horaemprestimo, solicitacaoemprestimo_codemprestimo, solicitacaoemprestimo_dvd_codigo, solicitacaoemprestimo_socio_codigo) ");
sql.append("SELECT dataemprestimo, horaemprestimo, codemprestimo ,dvd_codigo, socio_codigo ");
sql.append("FROM solicitacaoemprestimo ");
sql.append(" WHERE codemprestimo = ? ");

But you are trying to associate 5 other parameters that are not needed and should be removed:

comando.setLong(1, e.getClubeDoDvdEmprestimo().getCodEmprestimo());
//comando.setString(2, e.getDataEmprestimo());
//comando.setString(3, e.getHoraEprestimo());
//comando.setLong(4, e.getClubeDoDvdEmprestimo().getCodEmprestimo());
//comando.setLong(5, e.getClubeDoDvdEmprestimo().getDvd().getCodigo());
//comando.setLong(6, e.getClubeDoDvdEmprestimo().getSocio().getCodigo());

Browser other questions tagged

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