Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. package employee;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javafx.application.Application;
  11. import static javafx.application.Application.launch;
  12. import javafx.event.ActionEvent;
  13. import javafx.geometry.Insets;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.ComboBox;
  18. import javafx.scene.layout.GridPane;
  19. import javafx.stage.Stage;
  20.  
  21. public class Employee extends Application {
  22. private GridPane root;
  23. private ComboBox<String> cboEmployees;
  24. @Override
  25. public void start(Stage primaryStage) {
  26.  
  27. Button btnCreateCon = new Button();
  28. btnCreateCon.setText("Connect");
  29. btnCreateCon.setMinWidth(200);
  30. btnCreateCon.setOnAction(e -> OnCreateConnection(e));
  31.  
  32. Button btnGetData = new Button();
  33. btnGetData.setText("get EMP");
  34. btnGetData.setMinWidth(200);
  35. btnGetData.setOnAction(e -> OnGetData(e));
  36.  
  37. cboEmployees = new ComboBox<>();
  38. cboEmployees.setPromptText("list");
  39. cboEmployees.setVisibleRowCount(2);
  40.  
  41. root = new GridPane();
  42. root.setAlignment(Pos.CENTER);
  43. root.setPadding(new Insets(20));
  44. root.setVgap(20);
  45. root.addRow(0, btnCreateCon);
  46. root.addRow(1, btnGetData);
  47.  
  48. Scene scene = new Scene(root, 600, 400);
  49.  
  50.  
  51. primaryStage.setScene(scene);
  52. primaryStage.show();
  53. }
  54.  
  55. public static void main(String[] args) {
  56. launch(args);
  57. }
  58.  
  59. private void OnCreateConnection(ActionEvent e) {
  60. Connection con = null;
  61. if(con == null){
  62. con = GetConnection();
  63.  
  64. }
  65. }
  66.  
  67. private Connection GetConnection(){
  68. Connection con = null;
  69. try{
  70. Class.forName("com.mysql.jdbc.Driver");
  71. String url = "jdbc:mysql://localhost:3306/examdb";
  72. String user = "root";
  73. String password = "";
  74. con = DriverManager.getConnection(url,user,password);
  75. } catch (ClassNotFoundException | SQLException ex) {
  76. Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  77. }
  78. return con;
  79. }
  80.  
  81. private void OnGetData(ActionEvent e) {
  82. ResultSet emp = GetData();
  83. try{
  84. while(emp.next())
  85. {
  86. int ID = emp.getInt("ID");
  87. String name = emp.getString("name");
  88. boolean age = emp.getBoolean("gender");
  89. String country = emp.getString("country");
  90. int salary = emp.getInt("salary");
  91.  
  92.  
  93. cboEmployees.getItems().add((ID + " " +name + " "+ age+ " "+ country+ " "+ salary));
  94. }
  95. } catch (SQLException ex) {
  96. Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  97. }
  98. root.addRow(2, cboEmployees);
  99. }
  100.  
  101. private ResultSet GetData(){
  102. ResultSet results = null;
  103. Connection con= null;
  104. try{
  105. con = GetConnection();
  106. Statement s = con.createStatement();
  107. String sqlSelect = "SELECT `ID`, `Name`, `Age`, `Gender`, `Country`, `salary` FROM `employee` WHERE Country = 'jordan' AND salary >=500";
  108. results = s.executeQuery(sqlSelect);
  109. } catch (SQLException ex) {
  110. Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  111. }
  112. return results;
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement