Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- 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();
- tablacliente.setItems(data);
- }
- });
- Connection conn=null;
- try {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
- 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();
- }
- }
- 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) {
- Connection conn=null;{
- try {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
- 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();
- }
- }
- 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();
- clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
- nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
- apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
- tablacliente.setItems(data);
- }
- });
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <?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>
Add Comment
Please, Sign In to add comment