Advertisement
Guest User

EditedSQLClient

a guest
Mar 10th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1. /*
  2. What's in the file:
  3. '5', 'Fred', 'Arcen', 'A', 'address1', Southbend', 'IN', '7659999999', 'email1@iu.edu'
  4. '6', 'Thomas', 'Arthur', 'B', 'address2', Richmond', 'IN', '7659999999', 'email2@iu.edu'
  5. '9', 'Suzette', 'Austin', 'D', 'address3', Bloomington', 'IN', '7659999999', 'email3@iu.edu'
  6.  
  7. */
  8.  
  9.  
  10.  
  11. package editedsqlclient;
  12.  
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.Scanner;
  22. import javafx.application.Application;
  23. import javafx.collections.FXCollections;
  24. import javafx.geometry.Pos;
  25. import javafx.scene.Scene;
  26. import javafx.scene.control.Button;
  27. import javafx.scene.control.ComboBox;
  28. import javafx.scene.control.Label;
  29. import javafx.scene.control.PasswordField;
  30. import javafx.scene.control.SplitPane;
  31. import javafx.scene.control.TextArea;
  32. import javafx.scene.control.TextField;
  33. import javafx.scene.layout.BorderPane;
  34. import javafx.scene.layout.GridPane;
  35. import javafx.scene.layout.HBox;
  36. import javafx.scene.layout.VBox;
  37. import javafx.stage.Stage;
  38.  
  39. /**
  40.  *
  41.  * @author T
  42.  */
  43. public class EditedSQLClient extends Application {
  44.     private Connection connection;
  45.     private Statement statement;
  46.    
  47.        
  48.     private TextField tfFilename = new TextField();
  49.     private TextArea taFile = new TextArea();
  50.    
  51.     private TextField tfUsername = new TextField();
  52.     private PasswordField pfPassword = new PasswordField();
  53.     private ComboBox<String> cbURL = new ComboBox<>();
  54.     private ComboBox<String> cbDriver = new ComboBox<>();
  55.        
  56.     private Button btViewFile = new Button("View File");
  57.     private Button btView = new Button("View");
  58.     private Button btCopy = new Button("Copy");
  59.    
  60.    
  61.     private Label lblStatus = new Label();
  62.    
  63.     @Override
  64.     public void start(Stage primaryStage) {
  65.         cbURL.getItems().addAll(FXCollections.observableArrayList
  66.                ("jdbc:mysql://localhost/test"));
  67.                
  68.         cbURL.getSelectionModel().selectFirst();
  69.        
  70.         cbDriver.getItems().addAll(FXCollections.observableArrayList
  71.                 ("com.mysql.jdbc.Driver"));
  72.                
  73.         cbDriver.getSelectionModel().selectFirst();
  74.        
  75.         GridPane gridPane = new GridPane();
  76.             gridPane.add(cbURL, 1, 0);
  77.             gridPane.add(cbDriver, 1, 1);
  78.             gridPane.add(tfUsername, 1, 2);
  79.             gridPane.add(pfPassword, 1, 3);
  80.             gridPane.add(new Label("JDBC Driver: "), 0, 0); //Creates label JDBC Drivers
  81.             gridPane.add(new Label("Database URL: "), 0, 1); //Creates the label Database URL
  82.             gridPane.add(new Label("Username: "), 0, 2); //Creates the label Username
  83.             gridPane.add(new Label("Password: "), 0, 3); //Creates the label Password
  84.                        
  85.             HBox hBoxConnection = new HBox(10); //HBox for the connection
  86.             hBoxConnection.getChildren().addAll(lblStatus, btView, btCopy); //Adds the lblstatus and the connect button
  87.             hBoxConnection.setAlignment(Pos.CENTER_RIGHT); //Sets the alignment of hBox1 to the center right
  88.            
  89.            
  90.         VBox vBoxConnection = new VBox(6); //VBox for the connection
  91.             vBoxConnection.getChildren().addAll(new Label
  92.              ("Target Database Table"),
  93.              gridPane, hBoxConnection); //Creates a new label and adds the gridPane as well as hBox1
  94.                
  95.              gridPane.setStyle("-fx-border-color:black;"); //Sets a black border around the gridpane
  96.        
  97.        BorderPane bpFileName = new BorderPane();
  98.             bpFileName.setLeft(new Label("File Name"));
  99.             bpFileName.setCenter(tfFilename);
  100.             bpFileName.setRight(btViewFile);
  101.            
  102.         BorderPane bpFileContent = new BorderPane();
  103.             bpFileContent.setTop(bpFileName);
  104.             bpFileContent.setCenter(taFile);
  105.            
  106.         BorderPane bpFileSource = new BorderPane();
  107.             bpFileSource.setTop(new Label("Source Text File"));
  108.             bpFileSource.setCenter(bpFileContent);
  109.            
  110.         SplitPane splitPane = new SplitPane();
  111.             splitPane.getItems().addAll(bpFileSource, vBoxConnection);
  112.            
  113.         Scene  scene = new Scene(splitPane, 950, 350);
  114.         primaryStage.setTitle("CopyFileToStaff");
  115.         primaryStage.setScene(scene);
  116.         primaryStage.show();
  117.        
  118.         //btConnect.setOnAction(e -> connect());
  119.         btViewFile.setOnAction(e -> showFile());
  120.         btView.setOnAction(e -> executeView());
  121.         btCopy.setOnAction(e -> {
  122.            try {
  123.                copyFile();
  124.             }
  125.            
  126.            catch (Exception ex) {
  127.                lblStatus.setText(ex.toString());
  128.            }
  129.         });
  130.     }
  131.    
  132.     private void showFile() {
  133.         Scanner input = null;
  134.         try {
  135.             input = new Scanner(new File(tfFilename.getText().trim()));
  136.            
  137.             while (input.hasNext()) {
  138.                 taFile.appendText(input.nextLine() + '\n');
  139.             }
  140.         }
  141.             catch (FileNotFoundException ex) {
  142.                     System.out.println("File not found: " + tfFilename.getText());
  143.             }
  144.             finally {
  145.                 if (input != null) input.close();
  146.             }
  147.     }
  148.    
  149.     private void copyFile() throws Exception {
  150.         Class.forName(cbDriver.getSelectionModel().getSelectedItem().trim());
  151.         System.out.println("Driver loaded");
  152.        
  153.         Connection connection = DriverManager.getConnection(
  154.             cbURL.getSelectionModel().getSelectedItem().trim(),
  155.                 tfUsername.getText().trim(),
  156.                 String.valueOf(pfPassword.getText()).trim());
  157.         System.out.println("Database connected");
  158.        
  159.         taFile.setText("");
  160.         lblStatus.setText("Batch updates completed");
  161.     }
  162.    
  163.     private void executeView() {
  164.         processViewCommand();
  165.     }
  166.  
  167.     private void processViewCommand() {
  168.         try {
  169.             //try {
  170.             /*Class.forName(cbDriver.getSelectionModel().getSelectedItem().trim());
  171.             Connection connection =
  172.                 DriverManager.getConnection(cbURL.getSelectionModel().getSelectedItem().trim());
  173.                     //tfUsername.getText().trim();
  174.                     //String.valueOf(pfPassword.getText()).trim();
  175.             */
  176.             String query = "SELECT * FROM staff";
  177.             PreparedStatement ps = connection.prepareStatement(query);
  178.            
  179.             ResultSet resultSet = ps.executeQuery();
  180.            
  181.             while (resultSet.next()) {
  182.                 String columnData = resultSet.getString("id");
  183.                 taFile.appendText(columnData + '\n');
  184.             }
  185.            
  186.             /*catch (ClassNotFoundException e) {
  187.                 e.printStackTrace();
  188.             }
  189.         */
  190.         }
  191.         catch (SQLException ex) {
  192.             ex.printStackTrace();
  193.         }
  194.     }
  195.    
  196.      public static void main(String[] args) throws SQLException {
  197.     launch(args);
  198.     }
  199. }
  200.  
  201.     //Old code; didn't work but saved it in case I needed it
  202.     /*    String query = "SELECT * from staff";
  203.         String viewCommand = taFile.getText().trim();
  204.        
  205.    
  206.     try {
  207.  
  208.  
  209.       /// Get a new statement for the current connection
  210.         statement = connection.createStatement();
  211.  
  212.         // Execute SQL command
  213.         //query = "SELECT * from staff";
  214.         ResultSet resultSet = statement.executeQuery(query);
  215.  
  216.         // Find the number of columns in the result set
  217.         int columnCount = resultSet.getMetaData().getColumnCount();
  218.             String row = "";
  219.  
  220.         // Display column names
  221.         for (int i = 1; i <= columnCount; i++) {
  222.             row += resultSet.getMetaData().getColumnName(i) + "\t";
  223.         }
  224.  
  225.         taFile.appendText(row + '\n');
  226.  
  227.         while (resultSet.next()) {
  228.             // Reset row to empty
  229.             row = "";
  230.  
  231.         for (int i = 1; i <= columnCount; i++) {
  232.           // A non-String column is converted to a string
  233.           row += resultSet.getString(i) + "\t";
  234.         }
  235.  
  236.            taFile.appendText(row + '\n');
  237.         }
  238.         }
  239.         catch (SQLException ex) {
  240.            taFile.setText(ex.toString());
  241.         }
  242.     }\
  243.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement