Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] Error creación registro MySQL + Java

Estas en el tema de Error creación registro MySQL + Java en el foro de Java en Foros del Web. Hola Me gustaría solucionar un problema que tengo sobre el uso de LastId. Hago uso de Eclipse y MySQL y se me presenta errores al ...
  #1 (permalink)  
Antiguo 05/11/2015, 13:30
Avatar de agusax  
Fecha de Ingreso: junio-2011
Ubicación: Puerto Real (Cádiz)
Mensajes: 74
Antigüedad: 12 años, 10 meses
Puntos: 3
Error creación registro MySQL + Java

Hola
Me gustaría solucionar un problema que tengo sobre el uso de LastId.
Hago uso de Eclipse y MySQL y se me presenta errores al ejecutar el test siguiente:
Es como si no entrara en los métodos (todo esto se marca de rojo)

MapaTest.java
Código Java:
Ver original
  1. @Test
  2.     public void testCreate() throws Exception {
  3.         String nombre = "Prueba";
  4.         int capacidad = 99;
  5.         Mapa.Create(nombre, capacidad);
  6.     }
Mapa.java
Código Java:
Ver original
  1. public static Mapa Create(String nombre, int capacidad) throws Exception {
  2.         int id = Data.LastId(_con);
  3.         _con.createStatement().executeUpdate("INSERT INTO mapa VALUES (" + id + ","
  4.                 + Data.String2Sql(nombre, false, false) + "," + capacidad + ");");
  5.         Mapa m = new Mapa(id);
  6.         return m;
  7.     }
Data.java
Código Java:
Ver original
  1. public static String String2Sql(String s, boolean bAddQuotes, boolean bAddWildcards) {
  2.         char c = '\'';
  3.         String texto = "";
  4.  
  5.         if (bAddQuotes)
  6.             texto += c; // primera comilla
  7.  
  8.         if (bAddWildcards)
  9.             texto += '%';
  10.  
  11.         for (int i = 0; i < s.length(); i++) {
  12.             if (s.charAt(i) == c) {
  13.                 texto += c; // doble comilla
  14.             }
  15.             texto += s.charAt(i);
  16.         }
  17.  
  18.         if (bAddWildcards)
  19.             texto += '%';
  20.  
  21.         if (bAddQuotes)
  22.             texto += c; // segunda comilla
  23.  
  24.         return texto;
  25.     }
  26.  
  27.     public static int LastId(Connection con) throws Exception {
  28.  
  29.         ResultSet rs = null;
  30.         int id = 0;
  31.  
  32.         try {
  33.             Properties properties = Config.Properties(getPropertiesUrl());
  34.             rs = con.createStatement().executeQuery(properties.getProperty("jdbc.lastIdSentence") + "FROM mapa;");
  35.         } catch (Exception e) {
  36.             throw e;
  37.         }
  38.  
  39.         rs.next();
  40.         id = rs.getInt(1);
  41.  
  42.         return id;
  43.     }
  #2 (permalink)  
Antiguo 10/11/2015, 05:18
Avatar de agusax  
Fecha de Ingreso: junio-2011
Ubicación: Puerto Real (Cádiz)
Mensajes: 74
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Error creación registro MySQL + Java

El problema estaba en la instrucción a la BD, que no puse los paréntesis:

LAST_INSERT_ID()

Etiquetas: creación, mysql, registro, sql, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:42.