Advertisement
Guest User

FUCK THIS PROG

a guest
Apr 11th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 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.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import javafx.application.Application;
  9. import javafx.beans.property.SimpleStringProperty;
  10. import javafx.beans.value.ObservableValue;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.geometry.Insets;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.control.ComboBox;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.ListView;
  19. import javafx.scene.control.PasswordField;
  20. import javafx.scene.control.SelectionMode;
  21. import javafx.scene.control.TableColumn;
  22. import javafx.scene.control.TableColumn.CellDataFeatures;
  23. import javafx.scene.control.TableView;
  24. import javafx.scene.control.TextField;
  25. import javafx.scene.input.MouseEvent;
  26. import javafx.scene.layout.GridPane;
  27. import javafx.stage.Stage;
  28. import javafx.stage.WindowEvent;
  29. import javafx.util.Callback;
  30.  
  31. public class Homework5Herman extends Application{
  32.  
  33. Scene scene1, scene2, scene3;
  34. Stage window;
  35. Connection connection;
  36.  
  37. public static void main(String[] args) {
  38. launch(args);
  39. }
  40.  
  41. @Override
  42. public void start(Stage primaryStage) {
  43. window = primaryStage;
  44.  
  45. loginScreen();
  46.  
  47. window.setOnCloseRequest(e -> windowClosed(e));
  48. }
  49.  
  50. public void loginScreen() {
  51. GridPane gdpOne = createGridPane();
  52.  
  53. Label lblUsername = new Label("Username :");
  54. TextField txtUsername = new TextField("ddherman");
  55. Label lblPassword = new Label("Password :");
  56. PasswordField pwfPassword = new PasswordField();
  57. pwfPassword.setText("ddhermancis357d");
  58. Button btnConnect = new Button("Connect");
  59. btnConnect.setOnMouseClicked(e -> clickedConnect(e, txtUsername, pwfPassword));
  60.  
  61. gdpOne.add(lblUsername, 0, 0);
  62. gdpOne.add(txtUsername, 1, 0);
  63. gdpOne.add(lblPassword, 0, 1);
  64. gdpOne.add(pwfPassword, 1, 1);
  65. gdpOne.add(btnConnect, 1, 2);
  66.  
  67. scene1 = new Scene(gdpOne);
  68. window.setTitle("Login");
  69. window.setScene(scene1);
  70. window.show();
  71. }
  72.  
  73. public void inputScreen() throws SQLException {
  74. GridPane gdpTwo = createGridPane();
  75.  
  76. Label lblTables = new Label("Table :");
  77. Label lblFields = new Label("Fields :");
  78. Label lblConditions = new Label("Conitions :");
  79. TextField txtConditions = new TextField();
  80. Button btnExecute = new Button("Execute");
  81. ComboBox<String> cbxTables = setTableNames();
  82. ListView<String> lstView = new ListView<>();
  83.  
  84. gdpTwo.add(lblTables, 0, 0);
  85. gdpTwo.add(cbxTables, 1, 0);
  86. gdpTwo.add(lblFields, 0, 1);
  87. gdpTwo.add(lstView, 1, 1);
  88. gdpTwo.add(lblConditions, 0, 2);
  89. gdpTwo.add(txtConditions, 1, 2);
  90. gdpTwo.add(btnExecute, 1, 3);
  91.  
  92. cbxTables.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
  93. String selectedTable = cbxTables.getValue().toString();
  94. try {
  95. lstView.getItems().clear();
  96. lstView.setItems(getListView(selectedTable));
  97. lstView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  98. } catch (SQLException ex) {
  99. ex.printStackTrace();
  100. }
  101. });
  102.  
  103. btnExecute.setOnMouseClicked(e -> {
  104. String Query = createQuery(lstView, cbxTables, txtConditions);
  105. System.out.println(Query);
  106. outputScreen(Query);
  107. });
  108.  
  109. scene2 = new Scene(gdpTwo);
  110. window.setTitle("Input");
  111. window.setScene(scene2);
  112. }
  113.  
  114. @SuppressWarnings("unchecked")
  115. public void outputScreen(String Query) {
  116. TableView<String> tableView = new TableView<String>();
  117.  
  118. ObservableList data = FXCollections.observableArrayList();
  119.  
  120. try {
  121. ResultSet rs = connection.createStatement().executeQuery(Query);
  122.  
  123. for(int i = 0 ; i < rs.getMetaData().getColumnCount(); i++){
  124. //We are using non property style for making dynamic table
  125. final int j = i;
  126. TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
  127. col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>,ObservableValue<String>>(){
  128. public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
  129. return new SimpleStringProperty(param.getValue().get(j).toString());
  130. }
  131. });
  132.  
  133. tableView.getColumns().addAll(col);
  134. System.out.println("Column ["+i+"] ");
  135. }
  136.  
  137. while(rs.next()){
  138. //Iterate Row
  139. ObservableList<String> row = FXCollections.observableArrayList();
  140. for(int i = 1 ; i <= rs.getMetaData().getColumnCount(); i++){
  141. //Iterate Column
  142. if (rs.getString(i) == null) {
  143. row.add("none");
  144. } else {
  145. row.add(rs.getString(i));
  146. }
  147. }
  148.  
  149. System.out.println("Row [1] added "+row );
  150. data.add(row);
  151. }
  152.  
  153. tableView.setItems(data);
  154.  
  155. scene3 = new Scene(tableView);
  156. window.setTitle("Output");
  157. window.setScene(scene3);
  158. } catch (SQLException e) {
  159. conditionError();
  160. }
  161. }
  162.  
  163. public void windowClosed(WindowEvent e) {
  164. if (window.getTitle() == "Output") {
  165. try {
  166. inputScreen();
  167. } catch (SQLException e1) {
  168. e1.printStackTrace();
  169. }
  170. e.consume();
  171. } else if (window.getTitle() == "Input") {
  172. loginScreen();
  173. e.consume();
  174. } else {
  175.  
  176. }
  177. }
  178.  
  179. public void conditionError() {
  180. Stage error = new Stage();
  181. GridPane gdpError = createGridPane();
  182.  
  183. Label lblInstruct = new Label("Make sure only <, >, <=, >=, " +
  184. "==, AND, or OR are being used.");
  185. Label lblInstruct2 = new Label("Also check that values are " +
  186. "surreounded in ' '");
  187.  
  188. gdpError.add(lblInstruct, 0, 0);
  189. gdpError.add(lblInstruct2, 0, 1);
  190.  
  191. Scene scene = new Scene(gdpError);
  192. error.setTitle("Invalid Conditions Error");
  193. error.setScene(scene);
  194. error.show();
  195. }
  196.  
  197. public void clickedConnect(MouseEvent e, TextField usrname, PasswordField password) {
  198. //Load the driver
  199. loadDriver();
  200.  
  201. //Establish the connection
  202. try {
  203. connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:1111/ddherman" ,
  204. usrname.getText(), password.getText());
  205. System.out.println("Connection successful");
  206.  
  207. inputScreen();
  208. } catch (SQLException e1) {
  209. System.out.println("Failed to connect");
  210. }
  211. }
  212.  
  213. public ComboBox<String> setTableNames() throws SQLException {
  214. ComboBox<String> cbx = new ComboBox<>();
  215. ObservableList<String> tableNames = FXCollections.observableArrayList();
  216.  
  217. DatabaseMetaData dbmd = connection.getMetaData();
  218. ResultSet rs = dbmd.getTables(null, null, "%", null);
  219.  
  220. while (rs.next()) {
  221. String tableName = rs.getString(3);
  222.  
  223. tableNames.add(tableName);
  224. }
  225.  
  226. cbx.setItems(tableNames);
  227.  
  228. return cbx;
  229. }
  230.  
  231. public ObservableList<String> getListView(String table) throws SQLException {
  232.  
  233. ObservableList<String> columnNames = FXCollections.observableArrayList();
  234. Statement statement = connection.createStatement();
  235. ResultSet rs = statement.executeQuery("SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + table + "'");
  236.  
  237. while (rs.next()) {
  238. columnNames.add(rs.getString("COLUMN_NAME"));
  239. }
  240.  
  241. return columnNames;
  242. }
  243.  
  244. public String createQuery(ListView<String> columns, ComboBox<String> table, TextField txt) {
  245. ObservableList<String> selectedItems = columns.getSelectionModel().getSelectedItems();
  246. String[] theQuery = {"SELECT "};
  247. selectedItems.forEach((String) -> {
  248. theQuery[0] += String + ", ";
  249. });
  250.  
  251. String editQuery = theQuery[0].substring(0, theQuery[0].length() - 2);
  252. editQuery += " FROM " + table.getValue().toString();
  253.  
  254. editQuery = checkConditions(editQuery, txt);
  255.  
  256. return editQuery;
  257. }
  258.  
  259. public String checkConditions(String query, TextField txt) {
  260. if (txt.getText().trim().isEmpty() != true) {
  261. query += " Where " + txt.getText();
  262. }
  263.  
  264. return query;
  265. }
  266.  
  267. public GridPane createGridPane() {
  268. GridPane gridPane = new GridPane();
  269. gridPane.setPadding(new Insets(10, 10, 10, 10));
  270. gridPane.setVgap(8);
  271. gridPane.setHgap(8);
  272.  
  273. return gridPane;
  274. }
  275.  
  276. public void loadDriver() {
  277. try {
  278. Class.forName("com.mysql.jdbc.Driver");
  279. System.out.println("Driver Loaded");
  280. } catch (ClassNotFoundException e1) {
  281. System.out.println("Driver failed to load");
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement