amaliatorre / TrabajoFinal_BBDD_EEDD

MIT License
0 stars 0 forks source link

rs.moveToInsertRow(); #21

Closed amaliatorre closed 2 years ago

amaliatorre commented 2 years ago

Investigar problematica: com.mysql.jdbc.NotUpdatable: Result Set not updatable (referenced table has no primary keys).This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.

--> https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

--> Modificar : ej. PreparedStatement prepStmt= conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); http://chuwiki.chuidiang.org/index.php?title=Ejemplo_con_preparedStatement

try { Class.forName("com.mysql.jdbc.Driver");

// Es necesario useServerPrepStmts=true para que los PreparedStatement // se hagan en el servidor de bd. Si no lo ponemos, funciona todo // igual, pero los PreparedStatement se convertirán internamente a // Statements. Connection conexion = DriverManager.getConnection( "jdbc:mysql://servidor/basedatos?useServerPrepStmts=true", "usuario", "password"); ... } catch (Exception e) { e.printStackTrace(); }

opcion: http://coned.utcluj.ro/~marcel99/PT_Verificari_Teme/verificari-tema-3/match582.html https://www.cablenaranja.com/como-insertar-datos-desde-una-aplicacion-java-hacia-mysql/

amaliatorre commented 2 years ago

SOLUCION

// añadir id con el contador de filas y decirle que la clave primaria es la siguiente añadir el ide de introducir dentro del objeto resultado y mover el puntero a la reciente

public static boolean insertCliente (String nombre, String direccion) {

    try {
        // Obtenemos la tabla clientes
        System.out.print("Insertando cliente " + nombre + "...");
        ResultSet rs = getTablaClientes(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);

        int contador = 0;

        while (rs.next()) 
        {
            //nuemero de filas
            contador++;
        }
        int  id=contador+1;
        // Insertamos el nuevo registro
        rs.moveToInsertRow();
        rs.updateInt(DB_CLI_ID, id);
        rs.updateString(DB_CLI_NOM, nombre);
        rs.updateString(DB_CLI_DIR, direccion);
        rs.insertRow();
        rs.moveToCurrentRow();

        // Todo bien, cerramos ResultSet y devolvemos true
        rs.close();
        System.out.println("OK!");
        return true;

    } catch (SQLException ex) {
        ex.printStackTrace();
        return false;
    }
}