Guest User

Untitled

a guest
Oct 10th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. package application;
  2.  
  3. import javafx.beans.property.IntegerProperty;
  4. import javafx.beans.property.SimpleIntegerProperty;
  5. import javafx.beans.property.SimpleStringProperty;
  6. import javafx.beans.property.StringProperty;
  7.  
  8. public class cliente{
  9.  
  10.  
  11. private StringProperty nombres;
  12. private StringProperty apellidos;
  13. private IntegerProperty id_cliente;
  14.  
  15. public cliente ( String nombres, String apellidos, Integer id_cliente) {
  16. this.nombres= new SimpleStringProperty (nombres);
  17. this.apellidos= new SimpleStringProperty ( apellidos);
  18. this.id_cliente=new SimpleIntegerProperty (id_cliente);
  19. }
  20.  
  21.  
  22. public String getNombres() {
  23. return nombres.get();
  24. }
  25.  
  26. public void setNombres(String nombres) {
  27. this.nombres=new SimpleStringProperty (nombres);
  28. }
  29.  
  30.  
  31.  
  32. public String getApellidos() {
  33. return apellidos.get();
  34. }
  35.  
  36.  
  37. public void setApellidos(String apellidos) {
  38. this.apellidos=new SimpleStringProperty ( apellidos);
  39. }
  40.  
  41.  
  42.  
  43. public Integer getId_cliente() {
  44. return id_cliente.get();
  45. }
  46.  
  47.  
  48.  
  49. public void setid_cliente(Integer id_cliente) {
  50. this.id_cliente=new SimpleIntegerProperty (id_cliente);
  51. }
  52.  
  53. }
  54.  
  55. package application;
  56.  
  57.  
  58. import java.sql.Connection;
  59. import java.sql.DriverManager;
  60. import java.sql.SQLException;
  61. import java.sql.Statement;
  62.  
  63.  
  64. import javafx.event.ActionEvent;
  65. import javafx.event.EventHandler;
  66. import javafx.fxml.FXML;
  67. import javafx.scene.control.*;
  68. import javafx.scene.control.Alert.AlertType;
  69. import javafx.scene.input.MouseEvent;
  70.  
  71. public class ConexionSQL extends Mostraregistros{
  72. @FXML private TextField nm;
  73. @FXML private TextField ap;
  74. @FXML private Button btn;
  75.  
  76.  
  77.  
  78. @FXML
  79. private void btn(ActionEvent event) {
  80.  
  81. btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
  82. @Override
  83. public void handle(MouseEvent event) {
  84.  
  85. Alert alert = new Alert(AlertType.INFORMATION);
  86. alert.setTitle("Informacion");
  87. alert.setHeaderText(null);
  88. alert.setContentText("Registro Insertado Exitosamente");
  89. alert.showAndWait();
  90.  
  91. tablacliente.setItems(data);
  92.  
  93.  
  94. }
  95. });
  96.  
  97.  
  98. Connection conn=null;
  99. try {
  100. try {
  101. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  102. } catch (ClassNotFoundException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. }
  106. conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
  107. Statement insertar=conn.createStatement();
  108. insertar.executeUpdate("insert into cliente (nombre, apellido) values ('"+nm.getText()+"', '"+ap.getText()+"')");
  109.  
  110.  
  111. if(conn!=null)
  112. System.out.println("conexion exitosa");
  113.  
  114. } catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117.  
  118.  
  119. }
  120.  
  121. package application;
  122.  
  123. import javafx.collections.*;
  124.  
  125. import java.net.URL;
  126. import java.sql.Connection;
  127. import java.sql.DriverManager;
  128. import java.sql.ResultSet;
  129. import java.sql.SQLException;
  130. import java.sql.Statement;
  131. import java.util.ResourceBundle;
  132.  
  133.  
  134. import javafx.event.EventHandler;
  135. import javafx.fxml.FXML;
  136. import javafx.fxml.Initializable;
  137. import javafx.scene.control.*;
  138. import javafx.scene.control.Alert.AlertType;
  139. import javafx.scene.control.cell.PropertyValueFactory;
  140. import javafx.scene.input.MouseEvent;
  141.  
  142. public class Mostraregistros implements Initializable {
  143.  
  144. ObservableList <cliente> data =FXCollections.observableArrayList();
  145. @FXML TableView<cliente> tablacliente;
  146. @FXML TableColumn<cliente, String> nombrescol;
  147. @FXML TableColumn<cliente,String > apellidoscol;
  148. @FXML TableColumn<cliente, Integer> clienteid;
  149. @FXML private Button mtn;
  150.  
  151.  
  152.  
  153.  
  154.  
  155. @Override
  156. public void initialize(URL arg0, ResourceBundle arg1) {
  157.  
  158.  
  159. Connection conn=null;{
  160. try {
  161. try {
  162. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  163. } catch (ClassNotFoundException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. }
  167. conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
  168. Statement mostrar=conn.createStatement();
  169. ResultSet rs;
  170. rs= mostrar.executeQuery("select * from cliente");
  171.  
  172.  
  173. while ( rs.next() )
  174. {
  175. data.add(new cliente(
  176. rs.getString("nombre"),
  177. rs.getString("apellido"),
  178. rs.getInt("id")
  179. ));
  180.  
  181. }
  182.  
  183.  
  184.  
  185. if(conn!=null)
  186. System.out.println("conexion exitosa");
  187.  
  188.  
  189. } catch (SQLException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. mtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
  194. @Override
  195. public void handle(MouseEvent event) {
  196.  
  197. Alert alert = new Alert(AlertType.INFORMATION);
  198. alert.setTitle("Informacion");
  199. alert.setHeaderText(null);
  200. alert.setContentText("Mostrando Todos los Registros");
  201. alert.showAndWait();
  202. clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
  203. nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
  204. apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
  205.  
  206. tablacliente.setItems(data);
  207.  
  208. }
  209. });
  210. }
  211.  
  212. }
  213.  
  214. <?xml version="1.0" encoding="UTF-8"?>
  215.  
  216.  
  217. <?import application.cliente.*?>
  218.  
  219. <?import application.Mostraregistros.*?>
  220. <?import javafx.scene.text.*?>
  221. <?import javafx.scene.control.*?>
  222. <?import java.lang.*?>
  223. <?import javafx.scene.layout.*?>
  224. <?import javafx.scene.layout.AnchorPane?>
  225.  
  226. <AnchorPane prefHeight="634.0" prefWidth="626.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionSQL">
  227. <children>
  228. <Pane layoutX="5.0" layoutY="1.0" prefHeight="581.0" prefWidth="977.0">
  229. <children>
  230. <TextField fx:id="nm" layoutX="125.0" layoutY="70.0" prefHeight="32.0" prefWidth="205.0" text="nombres" />
  231. <TextField fx:id="ap" layoutX="125.0" layoutY="133.0" prefHeight="32.0" prefWidth="205.0" text="apellidos" />
  232. <Label layoutX="27.0" layoutY="70.0" prefHeight="32.0" prefWidth="137.0" text="NOMBRES" />
  233. <Button fx:id="btn" layoutX="21.0" layoutY="216.0" mnemonicParsing="false" onAction="#btn" prefHeight="43.0" prefWidth="84.0" text="AGREGAR" />
  234. <Label layoutX="27.0" layoutY="141.0" text="APELLIDOS" />
  235. <TableView fx:id="tablacliente" layoutX="346.0" layoutY="47.0" prefHeight="448.0" prefWidth="553.0">
  236. <columns>
  237. <TableColumn fx:id="clienteid" prefWidth="225.0" text="CLIENTE ID"/>
  238.  
  239. <TableColumn fx:id="nombrescol" prefWidth="206.0" text="NOMBRES"/>
  240.  
  241. <TableColumn fx:id="apellidoscol" prefWidth="161.0" text="APELLIDOS"/>
  242.  
  243. </columns>
  244. </TableView>
  245. <Button fx:id="mtn" layoutX="138.0" layoutY="216.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="144.0" text="MOSTRAR REGISTROS" />
  246. </children>
  247. </Pane>
  248. </children>
  249. </AnchorPane>
Add Comment
Please, Sign In to add comment