Guest User

Untitled

a guest
Oct 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. package application;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.net.URL;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.time.LocalDate;
  13. import java.util.ResourceBundle;
  14.  
  15. import javafx.collections.FXCollections;
  16. import javafx.collections.ObservableList;
  17. import javafx.fxml.FXML;
  18. import javafx.fxml.FXMLLoader;
  19. import javafx.fxml.Initializable;
  20. import javafx.scene.Scene;
  21. import javafx.scene.control.Alert;
  22. import javafx.scene.control.Alert.AlertType;
  23. import javafx.scene.control.Button;
  24. import javafx.scene.control.DatePicker;
  25. import javafx.scene.control.MenuBar;
  26. import javafx.scene.control.TableColumn;
  27. import javafx.scene.control.TableView;
  28. import javafx.scene.control.TextField;
  29. import javafx.scene.control.cell.PropertyValueFactory;
  30. import javafx.stage.Stage;
  31.  
  32.  
  33.  
  34.  
  35.  
  36. public class ConexionController implements Initializable {
  37.  
  38.  
  39.  
  40. @FXML TableView<Persona> tablacliente;
  41. @FXML TableColumn<Persona, String> nombrescol;
  42. @FXML TableColumn<Persona,String > apellidoscol;
  43. @FXML TableColumn<Persona, Integer> clienteid;
  44. @FXML TableColumn<Persona,LocalDate> fechacli;
  45. ResultSet rs=null;
  46. Connection Conexion=null;
  47. @FXML private Button btn;
  48. @FXML private Button mtn;
  49. @FXML private Button lmp;
  50. @FXML private Button mts;
  51. @FXML private Button bqd;
  52. @FXML private Button bqape;
  53. @FXML private TextField nm;
  54. @FXML private TextField ap;
  55. @FXML private TextField bq;
  56. @FXML private TextField bqa;
  57. @FXML private MenuBar menucombo;
  58. @FXML private Button botonborrar;
  59. @FXML private TextField borrar;
  60. @FXML private DatePicker mifecha;
  61.  
  62. @Override
  63. public void initialize(URL arg0, ResourceBundle arg1) {
  64. clienteid.setCellValueFactory(new PropertyValueFactory <Persona, Integer>("id_cliente"));
  65. nombrescol.setCellValueFactory(new PropertyValueFactory <Persona, String>("nombres"));
  66. apellidoscol.setCellValueFactory(new PropertyValueFactory <Persona, String>("apellidos"));
  67. fechacli.setCellValueFactory(new PropertyValueFactory <Persona, LocalDate>("fechacliente"));//
  68.  
  69.  
  70.  
  71.  
  72. seleccionaregistros();
  73. seleccionanombre();
  74. seleccionapellido();
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81. public void borraregistro() {
  82. int id = Integer.parseInt(borrar.getText());
  83. String consulta=" delete from cliente where id=? ";
  84.  
  85. Connection conn=null;{
  86.  
  87. try {
  88.  
  89. conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  90. PreparedStatement ps =conn.prepareStatement(consulta);
  91. ps.setInt(1, id);
  92. ps.executeQuery();
  93.  
  94.  
  95. }catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. Alert alert = new Alert(AlertType.INFORMATION);
  99. alert.setTitle("Informacion");
  100. alert.setHeaderText(null);
  101. alert.setContentText("Registro borrado correctamente");
  102. alert.showAndWait();
  103. }
  104. seleccionaregistros();
  105. }
  106.  
  107.  
  108. public void conexion(){
  109.  
  110. try {
  111. Conexion=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  112.  
  113. }
  114.  
  115. catch (SQLException e) {
  116. e.printStackTrace();
  117. }
  118. if(Conexion!=null) {
  119. Alert alert = new Alert(AlertType.INFORMATION);
  120. alert.setTitle("Informacion");
  121. alert.setHeaderText(null);
  122. alert.setContentText("Conexion Exitosa");
  123. alert.showAndWait();
  124.  
  125. }
  126.  
  127. }
  128.  
  129. public void insertaregistro() {
  130.  
  131. Connection conn=null;
  132. try {
  133.  
  134. conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  135. Statement insertar=conn.createStatement();
  136. insertar.executeUpdate("insert into cliente (nombre, apellido,fecha_nacimiento) values ('"+nm.getText()+"', '"+ap.getText()+"', '"+((TextField)mifecha.getEditor()).getText()+"')");
  137.  
  138. if(conn!=null) {
  139. Alert alert = new Alert(AlertType.INFORMATION);
  140. alert.setTitle("Informacion");
  141. alert.setHeaderText(null);
  142. alert.setContentText("Registro Insertado correctamente");
  143. alert.showAndWait();
  144. }
  145.  
  146. } catch (SQLException e) {
  147. e.printStackTrace();
  148. }
  149. seleccionaregistros();
  150.  
  151. }
  152.  
  153.  
  154. public void seleccionaregistros() {
  155. ObservableList <Persona> data =FXCollections.observableArrayList();
  156. Connection conn=null;{
  157. try {
  158.  
  159. conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  160. Statement mostrar=conn.createStatement();
  161. ResultSet rs;
  162. rs= mostrar.executeQuery("select * from cliente");
  163.  
  164.  
  165. while ( rs.next() )
  166. {
  167. data.add(new Persona(
  168.  
  169. rs.getString("nombre"),
  170. rs.getString("apellido"),
  171. rs.getInt("id"),
  172. rs.getDate(4)
  173. ));
  174. tablacliente.setItems(data);
  175. }
  176.  
  177. } catch (SQLException e) {
  178. e.printStackTrace();
  179. }
  180.  
  181. }
  182.  
  183. }
  184.  
  185. public void seleccionanombre() {
  186. String nombre = bq.getText();
  187. ObservableList <Persona> busqueda =FXCollections.observableArrayList();
  188. String consulta=" select * from cliente where nombre like ? " ;
  189.  
  190. Connection conn=null;{
  191.  
  192. try {
  193.  
  194. conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  195. PreparedStatement ps =conn.prepareStatement(consulta);
  196. ps.setString(1, nombre);
  197. ResultSet rs =ps.executeQuery();
  198.  
  199. while ( rs.next() )
  200. {
  201. busqueda.add(new Persona(
  202.  
  203. rs.getString("nombre"),
  204. rs.getString("apellido"),
  205. rs.getInt("id"),
  206. rs.getDate(4)
  207. ));
  208.  
  209. }
  210.  
  211. } catch (SQLException e) {
  212. e.printStackTrace();
  213. }
  214.  
  215. tablacliente.setItems(busqueda);
  216. }
  217.  
  218. }
  219.  
  220. public void seleccionapellido() {
  221. String apellido = bqa.getText();
  222. ObservableList <Persona> busquedape =FXCollections.observableArrayList();
  223. String consulta=" select * from cliente where apellido like ? " ;
  224.  
  225. Connection conn=null;{
  226.  
  227. try {
  228.  
  229. conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
  230. PreparedStatement ps =conn.prepareStatement(consulta);
  231. ps.setString(1, apellido);
  232. ResultSet rs =ps.executeQuery();
  233.  
  234. while ( rs.next() )
  235. {
  236. busquedape.add(new Persona(
  237.  
  238. rs.getString("nombre"),
  239. rs.getString("apellido"),
  240. rs.getInt("id"),
  241. rs.getDate(4)
  242. ));
  243.  
  244. }
  245.  
  246. } catch (SQLException e) {
  247. e.printStackTrace();
  248. }
  249.  
  250. tablacliente.setItems(busquedape);
  251. }
  252.  
  253. }
  254.  
  255. public void inicializacombo() {
  256. try {
  257. FXMLLoader fxmlLoader = new FXMLLoader();
  258. fxmlLoader.setLocation(getClass().getResource("VistaCombo.fxml"));
  259. Scene scene = new Scene(fxmlLoader.load());
  260. Stage stage = new Stage();
  261. stage.setTitle("Datos Del Cliente");
  262. stage.setScene(scene);
  263. stage.show();
  264. } catch (IOException e) {
  265.  
  266. }
  267. }
  268.  
  269. public void limpiatexto() {
  270. nm.clear();
  271. ap.clear();
  272. bq.clear();
  273. bqa.clear();
  274. borrar.clear();
  275. }
  276.  
  277.  
  278. public void cargarconexion() {
  279. btn.setOnAction(e->{
  280. conexion();
  281. });
  282. }
  283.  
  284. public void cargarregistro() {
  285. mtn.setOnAction(e->{
  286. insertaregistro();
  287. });
  288. }
  289. public void borrarcasillatexto() {
  290. lmp.setOnAction(e->{
  291. limpiatexto();
  292. });
  293. }
  294.  
  295. public void mostrartodo() {
  296. mts.setOnAction(e->{
  297. seleccionaregistros();
  298. });
  299. }
  300.  
  301. public void buscanm() {
  302. bqd.setOnAction(e->{
  303. seleccionanombre();
  304.  
  305. });
  306. }
  307.  
  308. public void buscaape() {
  309. bqape.setOnAction(e->{
  310. seleccionapellido();
  311.  
  312. });
  313. }
  314.  
  315. public void borraregistroid() {
  316. botonborrar.setOnAction(e->{
  317. borraregistro();
  318.  
  319. });
  320. }
  321.  
  322. }
  323.  
  324. package application;
  325.  
  326.  
  327. import java.time.LocalDate;
  328.  
  329. import javafx.beans.property.IntegerProperty;
  330. import javafx.beans.property.ObjectProperty;
  331. import javafx.beans.property.SimpleIntegerProperty;
  332. import javafx.beans.property.SimpleObjectProperty;
  333. import javafx.beans.property.SimpleStringProperty;
  334. import javafx.beans.property.StringProperty;
  335.  
  336. public class Persona {
  337.  
  338.  
  339.  
  340. private StringProperty nombres;
  341. private StringProperty apellidos;
  342. private IntegerProperty id_cliente;
  343. private ObjectProperty <LocalDate>fechacliente;
  344.  
  345. public Persona ( String nombres, String apellidos, Integer id_cliente, Object fechacliente) {
  346. this.nombres= new SimpleStringProperty (nombres);
  347. this.apellidos= new SimpleStringProperty ( apellidos);
  348. this.id_cliente=new SimpleIntegerProperty (id_cliente);
  349. this.fechacliente= new SimpleObjectProperty<LocalDate>();
  350. }
  351.  
  352.  
  353. public Object getFecha() {
  354. return fechacliente.get();
  355. }
  356.  
  357. public void setFecha(Object fechacliente) {
  358. this.fechacliente=new SimpleObjectProperty<>();
  359.  
  360. }
  361.  
  362. public String getNombres() {
  363. return nombres.get();
  364. }
  365.  
  366. public void setNombres(String nombres) {
  367. this.nombres=new SimpleStringProperty (nombres);
  368. }
  369.  
  370.  
  371. public String getApellidos() {
  372. return apellidos.get();
  373. }
  374.  
  375. public void setApellidos(String apellidos) {
  376. this.apellidos=new SimpleStringProperty ( apellidos);
  377. }
  378.  
  379.  
  380. public Integer getId_cliente() {
  381. return id_cliente.get();
  382. }
  383.  
  384. public void setid_cliente(Integer id_cliente) {
  385. this.id_cliente=new SimpleIntegerProperty (id_cliente);
  386. }
  387.  
  388. }
Add Comment
Please, Sign In to add comment