como guardar una fecha de jdatechooser a mysql
Publicado por uriel (3 intervenciones) el 04/02/2021 20:06:55
hola a todos buenas tardes disculpen queria pedir si podrian apoyarme por favor, estoy haciendo un inventario con ayuda de java y la base de datos es heidisql.
He buscado algunos ejemplos para guardar la fecha de un jDateChosser a la BD pero por mas que he intentado no puedo adaptarlo a mi proyecto ya que su estructura es un poco diferente. Espero puedan apoyarme por favor mi codigo es el siguiente:
EN UN FORMULARIO TENGO ESTE CODIGO DE LAS VARIABLES
Y EL BOTON GUARDAR TIENE ESTE CODIGO
EN OTRA CLASE LLAMADA VARIABLES ES DONDE TENGO DECLARADAS LAS MISMAS
Y EN OTRA CLASE LLAMADA CONEXION_CONSULTA ES DONDE REALIZA LA CONEXION A LA BASE DE DATOS Y LAS INSERCIONES, ACTUALIZACION DE DATOS Y BORRAR
anteriormente use puros String por lo que no habia ningun problema pero quiero cambiar la fecha en heidisql a tipo date, sin embargo no encuentro como hacerlo, agradeceria su ayuda por favor
He buscado algunos ejemplos para guardar la fecha de un jDateChosser a la BD pero por mas que he intentado no puedo adaptarlo a mi proyecto ya que su estructura es un poco diferente. Espero puedan apoyarme por favor mi codigo es el siguiente:
EN UN FORMULARIO TENGO ESTE CODIGO DE LAS VARIABLES
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public variables dar_valores(){
variables r = null;
String id_equipo = tfid.getText();
String marca = tfmarca.getText();
String modelo = tfmodelo.getText();
String color = tfcolor.getText();
String tamañoPantalla = tftamañoPantalla.getText();
String caracteristicas = tfcaracteristicas.getText();
float precio = Float.parseFloat(tfprecio.getText());
String proveedor = tfproveedor.getText();
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
Date fechaCompra = tffechaCompra.getDate();
String numFactura = tfnumFactura.getText();
String garantia = tfgarantia.getText();
String ubicacion = tfubicacion.getText();
String encargadoEquipo = tfencargadoEquipo.getText();
r= new variables(id_equipo,marca,modelo,color,tamañoPantalla,caracteristicas,precio,proveedor,fechaCompra,numFactura,garantia,ubicacion,encargadoEquipo);
return r;
}
Y EL BOTON GUARDAR TIENE ESTE CODIGO
1
2
3
4
String id_equipo = tfid.getText();
variables r = null;
r = dar_valores();
conexion_consulta.actualizar_reg(r, id_equipo);
EN OTRA CLASE LLAMADA VARIABLES ES DONDE TENGO DECLARADAS LAS MISMAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class variables {
String id_equipo;
String marca;
String modelo;
String color;
String tamañoPantalla;
String caracteristicas;
float precio;
String proveedor;
Date fechaCompra;
String numFactura;
String garantia;
String ubicacion;
String encargadoEquipo;
public variables(String id_equipo, String marca, String modelo, String color, String tamañoPantalla, String caracteristicas, float precio, String proveedor, Date fechaCompra, String numFactura, String garantia, String ubicacion, String encargadoEquipo) {
this.id_equipo = id_equipo;
this.marca = marca;
this.modelo = modelo;
this.color = color;
this.tamañoPantalla = tamañoPantalla;
this.caracteristicas = caracteristicas;
this.precio = precio;
this.proveedor = proveedor;
this.fechaCompra = fechaCompra;
this.numFactura = numFactura;
this.garantia = garantia;
this.ubicacion = ubicacion;
this.encargadoEquipo = encargadoEquipo;
}
public String getId_equipo() {
return id_equipo;
}
public void setId_equipo(String id_equipo) {
this.id_equipo = id_equipo;
}
public String getMarca() {
return marca;
}
public void setMarca(String marca) {
this.marca = marca;
}
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
this.modelo = modelo;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getTamañoPantalla() {
return tamañoPantalla;
}
public void setTamañoPantalla(String tamañoPantalla) {
this.tamañoPantalla = tamañoPantalla;
}
public String getCaracteristicas() {
return caracteristicas;
}
public void setCaracteristicas(String caracteristicas) {
this.caracteristicas = caracteristicas;
}
public float getPrecio() {
return precio;
}
public void setPrecio(float precio) {
this.precio = precio;
}
public String getProveedor() {
return proveedor;
}
public void setProveedor(String proveedor) {
this.proveedor = proveedor;
}
public Date getFechaCompra() {
return fechaCompra;
}
public void setFechaCompra(Date fechaCompra) {
this.fechaCompra = fechaCompra;
}
public String getNumFactura() {
return numFactura;
}
public void setNumFactura(String numFactura) {
this.numFactura = numFactura;
}
public String getGarantia() {
return garantia;
}
public void setGarantia(String garantia) {
this.garantia = garantia;
}
public String getUbicacion() {
return ubicacion;
}
public void setUbicacion(String ubicacion) {
this.ubicacion = ubicacion;
}
public String getEncargadoEquipo() {
return encargadoEquipo;
}
public void setEncargadoEquipo(String encargadoEquipo) {
this.encargadoEquipo = encargadoEquipo;
}
Y EN OTRA CLASE LLAMADA CONEXION_CONSULTA ES DONDE REALIZA LA CONEXION A LA BASE DE DATOS Y LAS INSERCIONES, ACTUALIZACION DE DATOS Y BORRAR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class conexion_consulta {
static Connection conexion=null;
static Statement sentencia;
static ResultSet resultado;
public static void conectar(){
String ruta="jdbc:mysql://localhost:3306/almacen?userTime=true&serverTimezone=UTC";
String user="root";
String pass="12345";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conexion=DriverManager.getConnection(ruta,user,pass);
sentencia= conexion.createStatement();
System.out.println("Conectado");
} catch (Exception e) {
System.out.println("No conectado");
e.printStackTrace();
}
}
public static void guardar(variables x){
String q = "INSERT INTO monitores VALUES('"+x.getId_equipo()+"','"+x.getMarca()+"','"+x.getModelo()+"','"+x.getColor()+"','"+x.getTamañoPantalla()+"','"+x.getCaracteristicas()+"','"+x.getPrecio()+"','"+x.getProveedor()+"','"+x.getFechaCompra()+"','"+x.getNumFactura()+"','"+x.getGarantia()+"','"+x.getUbicacion()+"','"+x.getEncargadoEquipo()+"')";
ejecutar(q);
}
public static void ejecutar(String q){
try {
sentencia.executeUpdate(q);
JOptionPane.showMessageDialog(null,"Correcto");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error");
}
}
anteriormente use puros String por lo que no habia ningun problema pero quiero cambiar la fecha en heidisql a tipo date, sin embargo no encuentro como hacerlo, agradeceria su ayuda por favor
Valora esta pregunta


0