Solucionado (ver solución)
Solucionado
(ver solución)
7
respuestas

Buen día, me genera un error en Statement.RETURN_GENERATED_KEYS, ¿Tienen alguna recomendación?

 public void guardar(Map<String, String> producto) throws SQLException {

        Connection con= new ConnectionFactory().recuperaConexion();

        Statement statement = (Statement) con.createStatement();

        statement.execute("INSERT INTO PRODUCTO(nombre, descripcion, cantidad)"
                + "VALUES (" + producto.get("NOMBRE") + "´,´"
                        + producto.get("DESCRIPCION") + "´,"
                        + producto.get("CANTIDAD") + ")" Statement.RETURN_GENERATED_KEYS); 

        ResultSet resultSet = ((java.sql.Statement) statement).getGeneratedKeys();

        while (resultSet.next()) {
            System.out.println(String.format("Fue insertado el producto de ID &d", 
                    resultSet.getInt(1)));


        }
7 respuestas

Buenas, te está dando el error por que colocaste mal las comillas simples del query.

Tea faltado la comilla simple para abrir '" + producto.get("NOMBRE") + "' y te a faltado una coma antes de , Statement.RETURN_GENERATED_KEYS

Deberia quedar así:

statement.execute("INSERT INTO PRODUCTO(nombre, descripcion, cantidad)"
    + " VALUES ('" + producto.get("NOMBRE") + "','"
    + producto.get("DESCRIPCION") + "'," 
    + producto.get("CANTIDAD") + ")",
    Statement.RETURN_GENERATED_KEYS);

Sigo con error, me marca esto: Multiple markers at this line

- RETURN_GENERATED_KEYS cannot be resolved or is not a field
- RETURN_GENERATED_KEYS cannot be resolved or is not a field
- Line breakpoint:ProductoController [line: 66] - guardar(Map<String, String>)

Buenas, si no puede ser resuelto puede ser por que no importaste la clase correcta, para ver mejor el problema ¿puedes compartir todo el codigo? y no solo la clase guardar.

package com.alura.jdbc.controller;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alura.jdbc.ConnectionFactory;
import com.alura.jdbc.pruebas.PruebaConexion;

public class ProductoController {

    public void modificar(String nombre, String descripcion, Integer id) {
        // TODO
    }

    public void eliminar(Integer id) {
        // TODO
    }

    public List<Map<String,String>> listar() throws SQLException{
        // TODO
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost/control_de_stock?useTimeZone=true&serverTimeZone=UTC",
                "root",
                "root1234");

        java.sql.Statement statement =con.createStatement();

        statement.execute("SELECT ID, NOMBRE, DESCRIPCION, CANTIDAD FROM PRODUCTO");

        ResultSet resultSet = statement.getResultSet();

        List<Map<String,String>> resultado = new ArrayList<>();

        while(resultSet.next()) {
            Map<String, String> fila = new HashMap<>();
            fila.put("ID", String.valueOf(resultSet.getInt("ID")));
            fila.put("NOMBRE", resultSet.getNString("NOMBRE"));
            fila.put("DESCRIPCION", resultSet.getNString("DESCRIPCION"));
            fila.put("CANTIDAD", String.valueOf(resultSet.getInt("CANTIDAD")));

            resultado.add(fila);

        }

        con.close();

        return resultado;
    }

    public void guardar(Map<String, String> producto) throws SQLException {

        Connection con= new ConnectionFactory().recuperaConexion();



        Statement statement = (Statement) con.createStatement();

        statement.execute("INSERT INTO PRODUCTO(nombre, descripcion, cantidad)"
                + "VALUES (´" + producto.get("NOMBRE") + "´, ´"
                        + producto.get("DESCRIPCION") + "´, "
                        + producto.get("CANTIDAD") + ")", Statement.RETURN_GENERATED_KEYS); 

        ResultSet resultSet = ((java.sql.Statement) statement).getGeneratedKeys();

        while (resultSet.next()) {
            System.out.println(String.format("Fue insertado el producto de ID &d", 
                    resultSet.getInt(1)));


        }
        // TODO
    }

}
solución!

Tu primer import deberia ser

import java.sql.Statement;

en lugar del java.beans.Statement

Gracias por tu ayuda

Muchas gracias tenia el mismo problema.. y no lograba verlo.

Saludos.-