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

Como Enviar una fecha (STRING) a un registro tipo DATE

Estas en el tema de Como Enviar una fecha (STRING) a un registro tipo DATE en el foro de Java en Foros del Web. Saludos a todos quisiera que me ayudara a resolver el siguiente problema: Tengo dodo clase Representante y Representante DAO: Representante.java package Objetos; import java.util.*; import ...
  #1 (permalink)  
Antiguo 24/06/2010, 16:08
 
Fecha de Ingreso: junio-2010
Mensajes: 1
Antigüedad: 13 años, 9 meses
Puntos: 0
Como Enviar una fecha (STRING) a un registro tipo DATE

Saludos a todos

quisiera que me ayudara a resolver el siguiente problema:

Tengo dodo clase Representante y Representante DAO:

Representante.java
package Objetos;
import java.util.*;
import java.text.SimpleDateFormat;

public class Representante{

int tipoIdent;
int cedula;
String Pnombre;
String Snombre;
String Papellido;
String Sapellido;
Date fechanac;
String licencia;
String dirresidencia;
int pais;
int departamento;
int ciudad;
int telefono;
int celular;
String email;
String nomhotel;
int telhotel;
String dirhotel;



public Representante(int tipoIdent,int cedula,String Pnombre,String Snombre, String Papellido, String Sapellido, Date fechanac, String licencia,String dirresidencia, int pais, int departamneto, int ciudad, int telefono, int celular,String email,String nomhotel, int telhotel, String dirhotel)
{
this.tipoIdent=tipoIdent;
this.cedula=cedula;
this.Pnombre=Pnombre;
this.Snombre=Snombre;
this.Papellido=Papellido;
this.Sapellido=Sapellido;
this.fechanac=fechanac;
this.licencia=licencia;
this.dirresidencia=dirresidencia;
this.pais=pais;
this.departamento=departamento;
this.ciudad=ciudad;
this.telefono=telefono;
this.celular=celular;
this.email=email;
this.nomhotel=nomhotel;
this.telhotel=telhotel;
this.dirhotel=dirhotel;

}
public int getcedula()
{
return cedula;
}

public int gettipoIdent()
{
return tipoIdent;
}

public String getPnombre()
{
return Pnombre;
}

public String getSnombre()
{
return Snombre;
}

public String getPapellido()
{
return Papellido;
}

public String getSapellido()
{
return Sapellido;
}

public Date getfechanac()
{
return fechanac;
}

public String getlicencia()
{
return licencia;
}
public String getdirresidencia()
{
return dirresidencia;
}

public int getpais()
{
return pais;
}

public int getdepartamento()
{
return departamento;
}

public int getciudad()
{
return ciudad;
}

public int gettelefono()
{
return telefono;
}

public int getcelular()
{
return celular;
}

public String getemail()
{
return email;
}


public String getnomhotel()
{
return nomhotel;
}
public String getdirhotel()
{
return dirhotel;
}

public int gettelhotel()
{
return telhotel;
}



}

RepresentanteDAO.java


package Objetos;
import java.sql.*;

import java.util.ArrayList;
import java.util.List;
;
import java.util.Date;



public class RepresentanteDAO{


public int GuardarRepre(Representante re)
{
int fila=0;

try
{

//Cargar el controlador
Class.forName("com.mysql.jdbc.Driver");
//crear una coneccion con la DB
Connection conexion=DriverManager.getConnection("jdbc:mysql://localhost/db","root","1234");
//crear objeto para realizar consultas,inseciones, borrados, etc
PreparedStatement instruccion = conexion.prepareStatement("insert into representante_legal values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
instruccion.setInt(1,re.gettipoIdent());
instruccion.setInt(2,re.getcedula());
instruccion.setString(3,re.getPnombre());
instruccion.setString(4,re.getSnombre());
instruccion.setString(5,re.getPapellido());
instruccion.setString(6,re.getSapellido());
instruccion.setDate(7,re.getfechanac());
instruccion.setString(8,re.getlicencia());
instruccion.setString(9,re.getdirresidencia());
instruccion.setInt(10,re.getpais());
instruccion.setInt(11,re.getdepartamento());
instruccion.setInt(12,re.getciudad());
instruccion.setInt(13,re.gettelefono());
instruccion.setInt(14,re.getcelular());
instruccion.setString(15,re.getemail());
instruccion.setString(16,re.getnomhotel());
instruccion.setInt(17,re.gettelhotel());
instruccion.setString(18,re.getdirhotel());




///Ejecuto la instruccion
fila=instruccion.executeUpdate();

}catch(Exception e)
{
System.out.println("Error:"+e);
}
//retorno el numero de filas que guardo
return fila;

}

/*public int EliminarPais(int id)
{
int fila=0;

try
{

//Cargar el controlador
Class.forName("com.mysql.jdbc.Driver");
//crear una coneccion con la DB
Connection conexion=DriverManager.getConnection("jdbc:mysql://localhost/db","root","1234");
//crear objeto para realizar consultas,inseciones, borrados, etc
PreparedStatement instruccion = conexion.prepareStatement("delete from pais where id=?");
instruccion.setInt(1,id);
///Ejecuto la instruccion
fila=instruccion.executeUpdate();

}catch(Exception e)
{
System.out.println("Error:"+e);
}
//retorno el numero de filas que guardo
return fila;

}*/

/*public List getRepr()
{
int fila=0;
ArrayList<Representante> repre=null;

try
{

//Cargar el controlador
Class.forName("com.mysql.jdbc.Driver");
//crear una coneccion con la DB
Connection conexion=DriverManager.getConnection("jdbc:mysql://localhost/db","root","1234");
//crear objeto para realizar consultas,inseciones, borrados, etc
PreparedStatement instruccion = conexion.prepareStatement("select * from representante_legal");
///Ejecuto la instruccion
ResultSet r=instruccion.executeQuery();

repre= new ArrayList<Representante> ();

while(r.next())
{

Representante rp=new Representante(Integer.parseInt(r.getString(1)), Integer.parseInt(r.getString(2)),r.getString(3),r. getString(4), r.getString(5), r.getString(6), r.getString(7), r.getString(8), r.getString(9),Integer.parseInt(r.getString(10)),I nteger.parseInt(r.getString(11)),Integer.parseInt( r.getString(12)),Integer.parseInt(r.getString(13)) ,Integer.parseInt(r.getString(14)),r.getString(15) ,r.getString(16),Integer.parseInt(r.getString(17)) ,r.getString(18));
repre.add(rp);

}


}catch(Exception e)
{
System.out.println("Error: "+e);
}
//retorno el numero de filas que guardo
return repre;

} */

public static void main (String arg []) {
try{

RepresentanteDAO red= new RepresentanteDAO();
//Representante re=Representante(85,1082,"antonio","jose","brtio", "jimenez",'10'+'01'+'1990',"78899-ab","calle 13",1,1,1,4206987,3014578,"[email protected]","tama ca",4782586,"km 48 via gaira");
//red.GuardarRepre(re);

}

catch (Exception e)
{

System.out.println ("Error en el main: "+e);
}


}


}

Cuado Compilo esta ultima clase me genera el siguiente error:

cannot find symbol method setDate(int,java.util.Date)

este error marca la siguiente instruccion: instruccion.setDate(7,re.getfechanac());


Por favor alguien me puede decir como soluciono este inconveniente!!!!


De ante mano les agradezco su atencion!!!!

Última edición por syriek; 25/06/2010 a las 07:24
  #2 (permalink)  
Antiguo 26/06/2010, 14:20
 
Fecha de Ingreso: septiembre-2009
Mensajes: 5
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Como Enviar una fecha (STRING) a un registro tipo DATE

como yo lo entiendo, no es posible mandarle un string y esperar que magicamente se convierta en un date ....

en tu main tienes la fecha de este estilo....

'10'+'01'+'1990'

y eso no es posible hasta donde yo se..... porque en tu base de datos dependiendo cual sea se manejaria como año/mes/dia o año-mes-dia....

si hay una forma de convertir un string a un date......dejame busco entre mis cosas......

QUE Manejador de Base de datos usas ?
  #3 (permalink)  
Antiguo 26/06/2010, 14:25
 
Fecha de Ingreso: septiembre-2009
Mensajes: 5
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Como Enviar una fecha (STRING) a un registro tipo DATE

es MySQL ya lo vi y como te decia jeje....

el formato de fecha es AÑO-MES-DIA

intenta esto.....

importa

import java.sql.Date;

y lo usas de esta forma...

Date fecha=Date.valueOf("2010-06-26");

espero te sirve...

hay otro método, pero deja ver si lo encuentro.....
  #4 (permalink)  
Antiguo 26/06/2010, 21:16
Avatar de dackiller  
Fecha de Ingreso: septiembre-2003
Ubicación: The Matrix
Mensajes: 341
Antigüedad: 20 años, 7 meses
Puntos: 4
Respuesta: Como Enviar una fecha (STRING) a un registro tipo DATE

Usa la clase

SimpleDateFormat

Ella te puede ayudar a realizar esa operacion en ambos sentidos.

Solo tienes que especificar el formato de la fecha:

Código Java:
Ver original
  1. SimpleDateFormat formato = new SimpleDateFormat("dd/mm/YYYY");

Puedes realizar la conversion de String a Date y viceversa.

Los metodos son format y parse.

Saludos
__________________
--
NOTA: Si haz conseguido la solución a tu problema, por favor edita el titulo del tema colocando el prefijo [SOLUCIONADO], para que otros usuarios puedan encontrar soluciones más rápido.

Etiquetas: date, enviar, registro, string, tipo, fechas
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 10:26.