I have a problem to show the date in DD / MM / YYYY format in the tableview, the insertion method takes the datepicker values and inserts them into the database, but when executing the method to display those records , just show me the empty date column as I show in the image
I have read in the forums that the column where the date needs to be formatted to a format in String as DD / MM / YYYY but I do not get how, from the person class I have declared the variable fechacliente
as object
and the other fields such as String
and Integer
with the property SimpleStringProperty
and SimpleIntergerPropety
, for the date must be Object
according to what I have read
then I leave the code to please help me solve this impasse:
Code of Controlador
package application;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class ConexionController implements Initializable {
@FXML TableView<Persona> tablacliente;
@FXML TableColumn<Persona, String> nombrescol;
@FXML TableColumn<Persona,String > apellidoscol;
@FXML TableColumn<Persona, Integer> clienteid;
@FXML TableColumn<Persona,LocalDate> fechacli;
ResultSet rs=null;
Connection Conexion=null;
@FXML private Button btn;
@FXML private Button mtn;
@FXML private Button lmp;
@FXML private Button mts;
@FXML private Button bqd;
@FXML private Button bqape;
@FXML private TextField nm;
@FXML private TextField ap;
@FXML private TextField bq;
@FXML private TextField bqa;
@FXML private MenuBar menucombo;
@FXML private Button botonborrar;
@FXML private TextField borrar;
@FXML private DatePicker mifecha;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
clienteid.setCellValueFactory(new PropertyValueFactory <Persona, Integer>("id_cliente"));
nombrescol.setCellValueFactory(new PropertyValueFactory <Persona, String>("nombres"));
apellidoscol.setCellValueFactory(new PropertyValueFactory <Persona, String>("apellidos"));
fechacli.setCellValueFactory(new PropertyValueFactory <Persona, LocalDate>("fechacliente"));//
seleccionaregistros();
seleccionanombre();
seleccionapellido();
}
public void borraregistro() {
int id = Integer.parseInt(borrar.getText());
String consulta=" delete from cliente where id=? ";
Connection conn=null;{
try {
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
PreparedStatement ps =conn.prepareStatement(consulta);
ps.setInt(1, id);
ps.executeQuery();
}catch (SQLException e) {
e.printStackTrace();
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Informacion");
alert.setHeaderText(null);
alert.setContentText("Registro borrado correctamente");
alert.showAndWait();
}
seleccionaregistros();
}
public void conexion(){
try {
Conexion=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
}
catch (SQLException e) {
e.printStackTrace();
}
if(Conexion!=null) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Informacion");
alert.setHeaderText(null);
alert.setContentText("Conexion Exitosa");
alert.showAndWait();
}
}
public void insertaregistro() {
Connection conn=null;
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
Statement insertar=conn.createStatement();
insertar.executeUpdate("insert into cliente (nombre, apellido,fecha_nacimiento) values ('"+nm.getText()+"', '"+ap.getText()+"', '"+((TextField)mifecha.getEditor()).getText()+"')");
if(conn!=null) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Informacion");
alert.setHeaderText(null);
alert.setContentText("Registro Insertado correctamente");
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
}
seleccionaregistros();
}
public void seleccionaregistros() {
ObservableList <Persona> data =FXCollections.observableArrayList();
Connection conn=null;{
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
Statement mostrar=conn.createStatement();
ResultSet rs;
rs= mostrar.executeQuery("select * from cliente");
while ( rs.next() )
{
data.add(new Persona(
rs.getString("nombre"),
rs.getString("apellido"),
rs.getInt("id"),
rs.getDate(4)
));
tablacliente.setItems(data);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void seleccionanombre() {
String nombre = bq.getText();
ObservableList <Persona> busqueda =FXCollections.observableArrayList();
String consulta=" select * from cliente where nombre like ? " ;
Connection conn=null;{
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
PreparedStatement ps =conn.prepareStatement(consulta);
ps.setString(1, nombre);
ResultSet rs =ps.executeQuery();
while ( rs.next() )
{
busqueda.add(new Persona(
rs.getString("nombre"),
rs.getString("apellido"),
rs.getInt("id"),
rs.getDate(4)
));
}
} catch (SQLException e) {
e.printStackTrace();
}
tablacliente.setItems(busqueda);
}
}
public void seleccionapellido() {
String apellido = bqa.getText();
ObservableList <Persona> busquedape =FXCollections.observableArrayList();
String consulta=" select * from cliente where apellido like ? " ;
Connection conn=null;{
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
PreparedStatement ps =conn.prepareStatement(consulta);
ps.setString(1, apellido);
ResultSet rs =ps.executeQuery();
while ( rs.next() )
{
busquedape.add(new Persona(
rs.getString("nombre"),
rs.getString("apellido"),
rs.getInt("id"),
rs.getDate(4)
));
}
} catch (SQLException e) {
e.printStackTrace();
}
tablacliente.setItems(busquedape);
}
}
public void inicializacombo() {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("VistaCombo.fxml"));
Scene scene = new Scene(fxmlLoader.load());
Stage stage = new Stage();
stage.setTitle("Datos Del Cliente");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
}
}
public void limpiatexto() {
nm.clear();
ap.clear();
bq.clear();
bqa.clear();
borrar.clear();
}
public void cargarconexion() {
btn.setOnAction(e->{
conexion();
});
}
public void cargarregistro() {
mtn.setOnAction(e->{
insertaregistro();
});
}
public void borrarcasillatexto() {
lmp.setOnAction(e->{
limpiatexto();
});
}
public void mostrartodo() {
mts.setOnAction(e->{
seleccionaregistros();
});
}
public void buscanm() {
bqd.setOnAction(e->{
seleccionanombre();
});
}
public void buscaape() {
bqape.setOnAction(e->{
seleccionapellido();
});
}
public void borraregistroid() {
botonborrar.setOnAction(e->{
borraregistro();
});
}
}
Code of Clase Persona
package application;
import java.time.LocalDate;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Persona {
private StringProperty nombres;
private StringProperty apellidos;
private IntegerProperty id_cliente;
private ObjectProperty <LocalDate>fechacliente;
public Persona ( String nombres, String apellidos, Integer id_cliente, Object fechacliente) {
this.nombres= new SimpleStringProperty (nombres);
this.apellidos= new SimpleStringProperty ( apellidos);
this.id_cliente=new SimpleIntegerProperty (id_cliente);
this.fechacliente= new SimpleObjectProperty<LocalDate>();
}
public Object getFecha() {
return fechacliente.get();
}
public void setFecha(Object fechacliente) {
this.fechacliente=new SimpleObjectProperty<>();
}
public String getNombres() {
return nombres.get();
}
public void setNombres(String nombres) {
this.nombres=new SimpleStringProperty (nombres);
}
public String getApellidos() {
return apellidos.get();
}
public void setApellidos(String apellidos) {
this.apellidos=new SimpleStringProperty ( apellidos);
}
public Integer getId_cliente() {
return id_cliente.get();
}
public void setid_cliente(Integer id_cliente) {
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
}
I hope you can help me, the datecustomer field I define it as Date
in the database. I appreciate any help.