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

Clase Date

Estas en el tema de Clase Date en el foro de Java en Foros del Web. Buenas y santas, tengo un problema con el manejo de la clase Date en java. Resulta que la base de Datos tengo el campo Date. ...
  #1 (permalink)  
Antiguo 28/10/2011, 08:03
 
Fecha de Ingreso: octubre-2010
Mensajes: 154
Antigüedad: 13 años, 6 meses
Puntos: 5
Clase Date

Buenas y santas, tengo un problema con el manejo de la clase Date en java.
Resulta que la base de Datos tengo el campo Date. Y en mi programa en java (uso eclipse helios) tengo mis clases que tienen atributos Date().
bueno la recuperacion de los datos esta todo OK. Ahora cuando intento ingresar una nueva tupla en la tabla, me salta un error pero porque no puedo encontrar la forma de pasarle el formato de fecha de la base (yyyy-mm-dd) que maneja.

les paso un fragmento de codigo y la salida de la misma.

************************* CODIGO ************************
public boolean insertClientes(String nomCli, String apecli, String dni, int nro_doc, String direccion, int idLocalidad, int situacion, Date fechaNac, int telCarac, int tel, String sexo, String userOwner, Date fechaElimi,String tipoSexo)
{
System.out.println("dentro de ingreso de nuevo cliente");
String sql = "INSERT INTO `garantiasdb`.`clientes` (`NOMBRE`, `APELLIDO`, `TIPO_DOC`, " +
"`NRO_DOC`, `DIRECCION`, `IDLOCALIDA`, `SITUACION_`, `FECHA_NAC`, " +
"`TEL_CARAC`, `TELEFONO`, `SEXO`, `USEROWNER`, `FECHAELIMI`, `TIPO_SEXO`, `CODIGO`) VALUES ('"+nomCli+"', '"+apecli+"', '"+dni+"', '"+nro_doc+"', '"+direccion+"', '"+idLocalidad+"', '"+situacion+"', '"+fechaNac+"', " +
"'"+telCarac+"', '"+tel+"', '"+sexo+"', '"+userOwner+"', '"+fechaElimi+"', '"+tipoSexo+"', NULL);";
System.out.println("dentro de procesos, antes del TRY-CATCH del ingreso de nuevo cliente");
System.out.println(sql);
try {
System.out.println("dentro de procesos, apenas entrado al TRY-CATCH");
PreparedStatement st = Conexion.ObtenerConexion().prepareStatement(sql);
/*
* actualiza el cliente con dni numero "nro_doc"
* */
ResultSet rs = st.executeQuery();
} catch (SQLException e) {
System.out.print("ERROR EN LA ACTUALIZACION DE DATOS DE LA BASE EN LA INSERCION DE CLIENTE");
e.printStackTrace();
return false;
}
return true;
}


public static void main(String[] args) throws ParseException {
Procesos proc = new Procesos();
System.out.println("dentro del MAIN principal de la clase PROCESOS");
System.out.print("ACTUALIZACION DE CLIENTES \n\n");
int doc = 33444555;
String nombre, apellido, tipDoc, Dir, sexo, userO, tipoSexo,fnac, feli;
int idLoc, sit, telCarac, telef, cod;
Date fechaNac, fechaEli;
nombre = "nomCli2";
apellido = "apecli";
tipDoc = "dni2";
Dir = "calle2 4321";
sexo = "F";
userO = "0";
tipoSexo = "0";
idLoc = 4993;
sit = 3;
telCarac = 3483;
telef = 444999;

fnac = new String ("2011-7-15");
feli = new String ("2020-7-25");

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

fechaNac = sdf1.parse(fnac);
System.out.println("fechaNac parse: "+sdf1.parse(fnac));
System.out.println("fechaNac format: "+sdf1.format(fechaNac));
fechaEli = sdf1.parse(feli);
System.out.println("fechaEli: "+sdf1.format(fechaEli));

proc.insertClientes(nombre, apellido, tipDoc, doc, Dir, idLoc, sit, fechaNac, telCarac, telef, sexo, userO, fechaEli, tipoSexo);
System.out.println("inserto un nuevo cliente con DNI: "+doc);
}

************************* SALIDA ************************

dentro del MAIN principal de la clase PROCESOS


ACTUALIZACION DE CLIENTES

fechaNac parse: Fri Jul 15 00:00:00 GMT-03:00 2011
fechaNac format: 2011-07-15
fechaEli: 2020-07-25
dentro de ingreso de nuevo cliente
dentro de procesos, antes del TRY-CATCH del ingreso de nuevo cliente
INSERT INTO `garantiasdb`.`clientes` (`NOMBRE`, `APELLIDO`, `TIPO_DOC`, `NRO_DOC`, `DIRECCION`, `IDLOCALIDA`, `SITUACION_`, `FECHA_NAC`, `TEL_CARAC`, `TELEFONO`, `SEXO`, `USEROWNER`, `FECHAELIMI`, `TIPO_SEXO`, `CODIGO`) VALUES ('nomCli2', 'apecli', 'dni2', '33444555', 'calle2 4321', '4993', '3', 'Fri Jul 15 00:00:00 GMT-03:00 2011', '3483', '444999', 'F', '0', 'Sat Jul 25 00:00:00 GMT-03:00 2020', '0', NULL);
dentro de procesos, apenas entrado al TRY-CATCH
Conexion Exitosa1
Conexion Exitosa2
ERROR EN LA ACTUALIZACION DE DATOS DE LA BASE EN LA INSERCION DE CLIENTEjava.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:927)
at com.mysql.jdbc.StatementImpl.checkForDml(Statement Impl.java:468)
at com.mysql.jdbc.PreparedStatement.executeQuery(Prep aredStatement.java:2182)
at Implement.Procesos.insertClientes(Procesos.java:82 2)
at Implement.Procesos.main(Procesos.java:1074)

inserto un nuevo cliente con DNI: 33444555



************************* FIN CODIGO ************************

Lo que necesito es pasarle a la base la fecha con el formato yyyy-mm-dd, estimo que el error es la fecha, por la salida por consola que es nada que ver al formato que necesito pasarle...
cualquier ayuda sera bien venida y agradecida.
Abrazo.
Luciano.
  #2 (permalink)  
Antiguo 28/10/2011, 08:23
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Clase Date

Puedes usar la clase SimpleDateFormat.

Código Javascript:
Ver original
  1. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  2. Date fecha = format.parse(new Date());
  #3 (permalink)  
Antiguo 28/10/2011, 08:28
 
Fecha de Ingreso: octubre-2011
Ubicación: Peru
Mensajes: 18
Antigüedad: 12 años, 6 meses
Puntos: 2
Respuesta: Clase Date

Usa Try para que no te salga error

SimpleDateFormat formato1 = new SimpleDateFormat("yyyy-MM-dd")

try {
Date fechaEnviar = formato1.parse(new date());
} catch (ParseException ex) {
ex.printStackTrace();
}
  #4 (permalink)  
Antiguo 28/10/2011, 08:31
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Clase Date

Cita:
Iniciado por juliocock Ver Mensaje
Usa Try para que no te salga error

SimpleDateFormat formato1 = new SimpleDateFormat("yyyy-MM-dd")

try {
Date fechaEnviar = formato1.parse(new date());
} catch (ParseException ex) {
ex.printStackTrace();
}
Gracias por aclararlo juliocock, se me habia pasado. ;) +1

Etiquetas: clase, date, mysql, sql
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 09:46.