如何在表视图中显示最后一条记录?

时间:2018-10-10 04:34:44

标签: javafx

您好,当我使用按钮mostrar显示记录时,所有记录都会显示,但是当我使用按钮agregar添加新记录时,该按钮不显示记录,那是什么?我做错了吗?该方法不错,但是似乎tableview中看不到最后一条记录,这是我的代码,请帮助。

班级

package application;

import javafx.collections.*;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class Mostraregistros  implements Initializable  {

     ObservableList <cliente> data =FXCollections.observableArrayList();
    @FXML TableView<cliente>  tablacliente;
    @FXML TableColumn<cliente, String> nombrescol;
    @FXML TableColumn<cliente,String > apellidoscol;
    @FXML TableColumn<cliente, Integer>  clienteid;
    @FXML private Button mtn;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

         mtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {

                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("Informacion");
                    alert.setHeaderText(null);
                    alert.setContentText("Mostrando Todos los Registros");
              alert.showAndWait();
              nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
                apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
                clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
                tablacliente.setItems(data);

                }
            });   
         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 cliente(
                        rs.getString("nombre"),
                        rs.getString("apellido"),
                        rs.getInt("id")
                        ));

             }



             if(conn!=null)
                 System.out.println("conexion exitosa");


         } catch (SQLException e) {
             e.printStackTrace();
         }
    }

    }

}

fxml代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.cell.*?>
<?import application.cliente.*?>
<?import application.Mostraregistros.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="634.0" prefWidth="626.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionSQL">
   <children>
      <Pane layoutX="5.0" layoutY="1.0" prefHeight="581.0" prefWidth="977.0">
         <children>
            <TextField fx:id="nm" layoutX="125.0" layoutY="70.0" prefHeight="32.0" prefWidth="205.0" text="nombres" />
            <TextField fx:id="ap" layoutX="125.0" layoutY="133.0" prefHeight="32.0" prefWidth="205.0" text="apellidos" />
            <Label layoutX="27.0" layoutY="70.0" prefHeight="32.0" prefWidth="137.0" text="NOMBRES" />
            <Button fx:id="btn" layoutX="21.0" layoutY="216.0" mnemonicParsing="false" onAction="#btn" prefHeight="43.0" prefWidth="84.0" text="AGREGAR" />
            <Label layoutX="27.0" layoutY="141.0" text="APELLIDOS" />
            <TableView fx:id="tablacliente" layoutX="346.0" layoutY="47.0" prefHeight="448.0" prefWidth="553.0">
              <columns> 
                  <TableColumn fx:id="clienteid" prefWidth="225.0" text="CLIENTE ID"/> 

                <TableColumn fx:id="nombrescol" prefWidth="206.0" text="NOMBRES"/>

                <TableColumn fx:id="apellidoscol" prefWidth="161.0" text="APELLIDOS"/>

              </columns>
            </TableView>
            <Button fx:id="mtn" layoutX="138.0" layoutY="216.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="144.0" text="MOSTRAR REGISTROS" />
         </children>
      </Pane>
   </children>
</AnchorPane>

这是agregar按钮代码和insert代码

package application;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;

public class ConexionSQL  extends Mostraregistros{
    @FXML  private TextField nm;
    @FXML private TextField ap;
    @FXML private Button btn;




     @FXML

     private void  btn(ActionEvent event) {

         btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {

                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("Informacion");
                    alert.setHeaderText(null);
                    alert.setContentText("Registro Insertado Exitosamente");
                    alert.showAndWait();

                }
            });


         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) values ('"+nm.getText()+"', '"+ap.getText()+"')");
             if(conn!=null)
                 System.out.println("conexion exitosa");

         } catch (SQLException e) {
             e.printStackTrace();
         }

     }

        }

这是cliente class

package application;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class cliente{


    private StringProperty nombres;
    private StringProperty apellidos;
    private IntegerProperty id_cliente;

public cliente ( String nombres, String apellidos, Integer id_cliente) {
    this.nombres=  new SimpleStringProperty (nombres);
    this.apellidos= new SimpleStringProperty ( apellidos);
    this.id_cliente=new SimpleIntegerProperty (id_cliente);
}


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);
}




}

0 个答案:

没有答案