Advertisement
Guest User

Update

a guest
Apr 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DatabaseMetaData;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javafx.application.Application;
  12. import javafx.beans.Observable;
  13. import javafx.collections.FXCollections;
  14. import javafx.collections.ObservableList;
  15. import javafx.geometry.Insets;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Button;
  18. import javafx.scene.control.ComboBox;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.control.ListView;
  21. import javafx.scene.control.PasswordField;
  22. import javafx.scene.control.SelectionMode;
  23. import javafx.scene.control.TextField;
  24. import javafx.scene.input.MouseEvent;
  25. import javafx.scene.layout.GridPane;
  26. import javafx.stage.Stage;
  27.  
  28. public class Homework5Herman extends Application{
  29.  
  30. Scene scene1, scene2, scene3;
  31. Stage window;
  32. Connection connection;
  33.  
  34. public static void main(String[] args) {
  35. launch(args);
  36. }
  37.  
  38. @Override
  39. public void start(Stage primaryStage) {
  40. window = primaryStage;
  41.  
  42. loginScreen();
  43. }
  44.  
  45. public void loginScreen() {
  46. GridPane gdpOne = createGridPane();
  47.  
  48. Label lblUsername = new Label("Username :");
  49. TextField txtUsername = new TextField("ddherman");
  50. Label lblPassword = new Label("Password :");
  51. PasswordField pwfPassword = new PasswordField();
  52. pwfPassword.setText("ddhermancis357d");
  53. Button btnConnect = new Button("Connect");
  54. btnConnect.setOnMouseClicked(e -> clickedConnect(e, txtUsername, pwfPassword));
  55.  
  56. gdpOne.add(lblUsername, 0, 0);
  57. gdpOne.add(txtUsername, 1, 0);
  58. gdpOne.add(lblPassword, 0, 1);
  59. gdpOne.add(pwfPassword, 1, 1);
  60. gdpOne.add(btnConnect, 1, 2);
  61.  
  62. scene1 = new Scene(gdpOne);
  63. window.setTitle("HW 5 DB");
  64. window.setScene(scene1);
  65. window.show();
  66. }
  67.  
  68. public void inputScreen() throws SQLException {
  69. //Create nextWindow
  70. GridPane gdpTwo = createGridPane();
  71.  
  72. Label lblTables = new Label("Table :");
  73. Label lblFields = new Label("Fields :");
  74. ComboBox<String> cbxTables = setTableNames();
  75. ListView<String> lstView = new ListView<>();
  76.  
  77. gdpTwo.add(lblTables, 0, 0);
  78. gdpTwo.add(cbxTables, 1, 0);
  79. gdpTwo.add(lblFields, 0, 1);
  80. gdpTwo.add(lstView, 1, 1);
  81.  
  82. cbxTables.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
  83. String selectedTable = cbxTables.getValue().toString();
  84. try {
  85. lstView.getItems().clear();
  86. lstView.setItems(getListView(selectedTable));
  87. lstView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  88. //gdpTwo.add(lstView, 1, 1);
  89. } catch (SQLException ex) {
  90. ex.printStackTrace();
  91. }
  92. });
  93.  
  94. scene2 = new Scene(gdpTwo);
  95. window.setScene(scene2);
  96. }
  97.  
  98. public void clickedConnect(MouseEvent e, TextField usrname, PasswordField password) {
  99. //Load the driver
  100. loadDriver();
  101.  
  102. //Establish the connection
  103. try {
  104. connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:1111/ddherman" ,
  105. usrname.getText(), password.getText());
  106. System.out.println("Connection successful");
  107.  
  108. inputScreen();
  109. } catch (SQLException e1) {
  110. System.out.println("Failed to connect");
  111. }
  112. }
  113.  
  114. public ComboBox<String> setTableNames() throws SQLException {
  115. ComboBox<String> cbx = new ComboBox<>();
  116. ObservableList<String> tableNames = FXCollections.observableArrayList();
  117.  
  118. DatabaseMetaData dbmd = connection.getMetaData();
  119. ResultSet rs = dbmd.getTables(null, null, "%", null);
  120.  
  121. while (rs.next()) {
  122. String tableName = rs.getString(3);
  123.  
  124. tableNames.add(tableName);
  125. }
  126.  
  127. cbx.setItems(tableNames);
  128.  
  129. return cbx;
  130. }
  131.  
  132. public ObservableList<String> getListView(String table) throws SQLException {
  133.  
  134. ObservableList<String> columnNames = FXCollections.observableArrayList();
  135. Statement statement = connection.createStatement();
  136. ResultSet rs = statement.executeQuery("SELECT DISTINCT COLUMN NAME" +
  137. " FROM INFORMATION SCHEMA.COLUMNS" +
  138. " WHERE TABLE_NAME = '" + table + "'");
  139.  
  140. while (rs.next()) {
  141. columnNames.add(rs.getString("COLUMN_NAME"));
  142. }
  143.  
  144. return columnNames;
  145. }
  146.  
  147. public GridPane createGridPane() {
  148. GridPane gridPane = new GridPane();
  149. gridPane.setPadding(new Insets(10, 10, 10, 10));
  150. gridPane.setVgap(8);
  151. gridPane.setHgap(8);
  152.  
  153. return gridPane;
  154. }
  155.  
  156. public void loadDriver() {
  157. try {
  158. Class.forName("com.mysql.jdbc.Driver");
  159. System.out.println("Driver Loaded");
  160. } catch (ClassNotFoundException e1) {
  161. System.out.println("Driver failed to load");
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement