Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javafx.application.Application;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.value.ObservableValue;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.ComboBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.ListView;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.SelectionMode;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableColumn.CellDataFeatures;
- import javafx.scene.control.TableView;
- import javafx.scene.control.TextField;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import javafx.util.Callback;
- public class Homework5Herman extends Application{
- Scene scene1, scene2, scene3;
- Stage window;
- Connection connection;
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- window = primaryStage;
- loginScreen();
- window.setOnCloseRequest(e -> windowClosed(e));
- }
- public void loginScreen() {
- GridPane gdpOne = createGridPane();
- Label lblUsername = new Label("Username :");
- TextField txtUsername = new TextField("ddherman");
- Label lblPassword = new Label("Password :");
- PasswordField pwfPassword = new PasswordField();
- pwfPassword.setText("ddhermancis357d");
- Button btnConnect = new Button("Connect");
- btnConnect.setOnMouseClicked(e -> clickedConnect(e, txtUsername, pwfPassword));
- gdpOne.add(lblUsername, 0, 0);
- gdpOne.add(txtUsername, 1, 0);
- gdpOne.add(lblPassword, 0, 1);
- gdpOne.add(pwfPassword, 1, 1);
- gdpOne.add(btnConnect, 1, 2);
- scene1 = new Scene(gdpOne);
- window.setTitle("Login");
- window.setScene(scene1);
- window.show();
- }
- public void inputScreen() throws SQLException {
- GridPane gdpTwo = createGridPane();
- Label lblTables = new Label("Table :");
- Label lblFields = new Label("Fields :");
- Label lblConditions = new Label("Conitions :");
- TextField txtConditions = new TextField();
- Button btnExecute = new Button("Execute");
- ComboBox<String> cbxTables = setTableNames();
- ListView<String> lstView = new ListView<>();
- gdpTwo.add(lblTables, 0, 0);
- gdpTwo.add(cbxTables, 1, 0);
- gdpTwo.add(lblFields, 0, 1);
- gdpTwo.add(lstView, 1, 1);
- gdpTwo.add(lblConditions, 0, 2);
- gdpTwo.add(txtConditions, 1, 2);
- gdpTwo.add(btnExecute, 1, 3);
- cbxTables.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
- String selectedTable = cbxTables.getValue().toString();
- try {
- lstView.getItems().clear();
- lstView.setItems(getListView(selectedTable));
- lstView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- });
- btnExecute.setOnMouseClicked(e -> {
- String Query = createQuery(lstView, cbxTables, txtConditions);
- System.out.println(Query);
- outputScreen(Query);
- });
- scene2 = new Scene(gdpTwo);
- window.setTitle("Input");
- window.setScene(scene2);
- }
- @SuppressWarnings("unchecked")
- public void outputScreen(String Query) {
- TableView<String> tableView = new TableView<String>();
- ObservableList data = FXCollections.observableArrayList();
- try {
- ResultSet rs = connection.createStatement().executeQuery(Query);
- for(int i = 0 ; i < rs.getMetaData().getColumnCount(); i++){
- //We are using non property style for making dynamic table
- final int j = i;
- TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
- col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>,ObservableValue<String>>(){
- public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
- return new SimpleStringProperty(param.getValue().get(j).toString());
- }
- });
- tableView.getColumns().addAll(col);
- System.out.println("Column ["+i+"] ");
- }
- while(rs.next()){
- //Iterate Row
- ObservableList<String> row = FXCollections.observableArrayList();
- for(int i = 1 ; i <= rs.getMetaData().getColumnCount(); i++){
- //Iterate Column
- if (rs.getString(i) == null) {
- row.add("none");
- } else {
- row.add(rs.getString(i));
- }
- }
- System.out.println("Row [1] added "+row );
- data.add(row);
- }
- tableView.setItems(data);
- scene3 = new Scene(tableView);
- window.setTitle("Output");
- window.setScene(scene3);
- } catch (SQLException e) {
- conditionError();
- }
- }
- public void windowClosed(WindowEvent e) {
- if (window.getTitle() == "Output") {
- try {
- inputScreen();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- e.consume();
- } else if (window.getTitle() == "Input") {
- loginScreen();
- e.consume();
- } else {
- }
- }
- public void conditionError() {
- Stage error = new Stage();
- GridPane gdpError = createGridPane();
- Label lblInstruct = new Label("Make sure only <, >, <=, >=, " +
- "==, AND, or OR are being used.");
- Label lblInstruct2 = new Label("Also check that values are " +
- "surreounded in ' '");
- gdpError.add(lblInstruct, 0, 0);
- gdpError.add(lblInstruct2, 0, 1);
- Scene scene = new Scene(gdpError);
- error.setTitle("Invalid Conditions Error");
- error.setScene(scene);
- error.show();
- }
- public void clickedConnect(MouseEvent e, TextField usrname, PasswordField password) {
- //Load the driver
- loadDriver();
- //Establish the connection
- try {
- connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:1111/ddherman" ,
- usrname.getText(), password.getText());
- System.out.println("Connection successful");
- inputScreen();
- } catch (SQLException e1) {
- System.out.println("Failed to connect");
- }
- }
- public ComboBox<String> setTableNames() throws SQLException {
- ComboBox<String> cbx = new ComboBox<>();
- ObservableList<String> tableNames = FXCollections.observableArrayList();
- DatabaseMetaData dbmd = connection.getMetaData();
- ResultSet rs = dbmd.getTables(null, null, "%", null);
- while (rs.next()) {
- String tableName = rs.getString(3);
- tableNames.add(tableName);
- }
- cbx.setItems(tableNames);
- return cbx;
- }
- public ObservableList<String> getListView(String table) throws SQLException {
- ObservableList<String> columnNames = FXCollections.observableArrayList();
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery("SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + table + "'");
- while (rs.next()) {
- columnNames.add(rs.getString("COLUMN_NAME"));
- }
- return columnNames;
- }
- public String createQuery(ListView<String> columns, ComboBox<String> table, TextField txt) {
- ObservableList<String> selectedItems = columns.getSelectionModel().getSelectedItems();
- String[] theQuery = {"SELECT "};
- selectedItems.forEach((String) -> {
- theQuery[0] += String + ", ";
- });
- String editQuery = theQuery[0].substring(0, theQuery[0].length() - 2);
- editQuery += " FROM " + table.getValue().toString();
- editQuery = checkConditions(editQuery, txt);
- return editQuery;
- }
- public String checkConditions(String query, TextField txt) {
- if (txt.getText().trim().isEmpty() != true) {
- query += " Where " + txt.getText();
- }
- return query;
- }
- public GridPane createGridPane() {
- GridPane gridPane = new GridPane();
- gridPane.setPadding(new Insets(10, 10, 10, 10));
- gridPane.setVgap(8);
- gridPane.setHgap(8);
- return gridPane;
- }
- public void loadDriver() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver Loaded");
- } catch (ClassNotFoundException e1) {
- System.out.println("Driver failed to load");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement