Advertisement
CE_162

ZipCodeTranslator FINAL EXAM *PHASE 2*

May 16th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.72 KB | None | 0 0
  1.  
  2. /*
  3.  * Collin Emond
  4.  * 5-16-16
  5.  * Final Exam
  6.  * CSC-112
  7.  * Prof. Silvestri
  8.  * Phase 2: The Database Lookup
  9.  * Access the silvestri database with queries to return the specified data
  10.  * cremond0001@student.stcc.edu
  11.  */
  12.  
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18.  
  19. import javafx.application.Application;
  20. import javafx.geometry.Insets;
  21. import javafx.geometry.Pos;
  22. import javafx.scene.Scene;
  23. import javafx.scene.control.*;
  24. import javafx.scene.layout.*;
  25. import javafx.scene.paint.Color;
  26. import javafx.scene.shape.Line;
  27. import javafx.stage.Stage;
  28.  
  29. public class ZipCodePhase2 extends Application {
  30.     TextField txtzipcode = new TextField();
  31.     TextField txtcitymiddle = new TextField();
  32.     TextField txtcitytop = new TextField();
  33.     TextField txtstatetop = new TextField();
  34.     TextField txtstatemiddle = new TextField();
  35.     Button zipcityBtn = new Button("Zip To City");
  36.     Button citytozipBtn = new Button("City to Zip");
  37.     TextField txtzipcodes = new TextField("Zip Code(s)");
  38.     private PreparedStatement preparedStatement;
  39.     Connection conn;
  40.  
  41.     @Override
  42.     public void start(Stage primaryStage) {
  43.  
  44.         // Establish connection
  45.         StartDatabase();
  46.  
  47.         // Create a new pane to hold
  48.         BorderPane pane = new BorderPane();
  49.         pane.setTop(topHalf());
  50.         pane.setCenter(middlePart());
  51.         pane.setBottom(bottomHalf());
  52.         zipcityBtn.setOnAction(e -> checkzipcity());
  53.         citytozipBtn.setOnAction(e -> checkcitytozip());
  54.  
  55.         // Create a scene
  56.         Scene scene = new Scene(pane, 800, 300);
  57.         primaryStage.setTitle("Zip Code to City/State Translator App");
  58.         primaryStage.setScene(scene);
  59.         primaryStage.show();
  60.         primaryStage.setResizable(false);
  61.     }
  62.  
  63.     // TextStatus Field
  64.     TextField txtstatus = new TextField();
  65.  
  66.     // Method that executes Zip to City Button Code
  67.     private void checkzipcity() {
  68.         if (txtzipcode.getText().matches("(\\d{5})")) {
  69.             zipDatabase();
  70.             txtstatus.setText("Valid Zip Code - City and State retrieved from Database!");
  71.         } else {
  72.             txtstatus.setText("Not a Valid Zip Code - No Retrieval from Database!");
  73.             txtcitytop.setText("None");
  74.             txtstatetop.setText("None");
  75.         }
  76.     }
  77.  
  78.     // Method that executes City to Zip Button Code
  79.     private void checkcitytozip() {
  80.         if (txtcitymiddle.getText().matches("^[a-zA-Z\\s-]+$") && txtstatemiddle.getText().matches("^[A-Za-z]{2}")) {
  81.             cityDatabase();
  82.             txtstatus.setText("Valid City and State - Zip Code retrieved from Database!");
  83.         } else {
  84.             txtstatus.setText("Not a Valid City and State - No Retrieval from Database!");
  85.             txtzipcodes.setText("No zipcodes found");
  86.         }
  87.     }
  88.  
  89.     // Top Half of Program
  90.     public VBox topHalf() {
  91.  
  92.         HBox hbox = new HBox();
  93.         hbox.setPadding(new Insets(15, 12, 15, 12));
  94.         hbox.setSpacing(15);
  95.         hbox.setAlignment(Pos.CENTER);
  96.  
  97.         // VBox to hold HBox and Separator
  98.         VBox vbox = new VBox();
  99.         vbox.setAlignment(Pos.CENTER);
  100.  
  101.         // Add Line
  102.         Line separator = new Line();
  103.         separator.setStrokeWidth(5);
  104.         separator.setStroke(Color.BLACK);
  105.         separator.setEndX(800.0);
  106.         separator.autosize();
  107.  
  108.         // Add Labels
  109.         Label lblzipcode = new Label("Zip Code:");
  110.         Label lblcity = new Label("City:");
  111.         Label lblstate = new Label("State:");
  112.  
  113.         // Add Properties of TextField
  114.         txtzipcode.setPrefColumnCount(5);
  115.         txtzipcode.setPrefWidth(80);
  116.         txtcitytop.setPrefColumnCount(25);
  117.         txtcitytop.setPrefWidth(200);
  118.         txtcitytop.setEditable(false);
  119.         txtcitytop.setMouseTransparent(true);
  120.         txtcitytop.setFocusTraversable(false);
  121.         txtstatetop.setPrefColumnCount(2);
  122.         txtstatetop.setEditable(false);
  123.         txtstatetop.setMouseTransparent(true);
  124.         txtstatetop.setFocusTraversable(false);
  125.         txtstatetop.setPrefWidth(50);
  126.  
  127.         // Add to hbox
  128.         hbox.getChildren().addAll(lblzipcode, txtzipcode, zipcityBtn, lblcity, txtcitytop, lblstate, txtstatetop);
  129.         vbox.getChildren().addAll(hbox, separator);
  130.         return vbox;
  131.     }
  132.  
  133.     // Middle of program
  134.     public VBox middlePart() {
  135.  
  136.         HBox hbox = new HBox();
  137.         hbox.setPadding(new Insets(15, 12, 15, 12));
  138.         hbox.setSpacing(15);
  139.         hbox.setAlignment(Pos.CENTER);
  140.  
  141.         // VBox to hold HBox and Separator
  142.         VBox vbox = new VBox();
  143.         vbox.setAlignment(Pos.CENTER);
  144.  
  145.         // Add Line
  146.         Line separator = new Line();
  147.         separator.setStrokeWidth(5);
  148.         separator.setStroke(Color.BLACK);
  149.         separator.setEndX(800.0);
  150.         separator.autosize();
  151.  
  152.         // Add Labels
  153.         Label lblcity = new Label("City:");
  154.         Label lblstate = new Label("State:");
  155.  
  156.         // Add Properties of TextField
  157.         txtstatemiddle.setPrefColumnCount(2);
  158.         txtstatemiddle.setPrefWidth(50);
  159.         txtzipcodes.setPrefColumnCount(5);
  160.         txtzipcodes.setEditable(false);
  161.         txtzipcodes.setMouseTransparent(true);
  162.         txtzipcodes.setFocusTraversable(false);
  163.         txtzipcodes.setPrefHeight(100);
  164.         txtzipcodes.setPrefWidth(140);
  165.  
  166.         // Add to hbox
  167.         hbox.getChildren().addAll(lblcity, txtcitymiddle, lblstate, txtstatemiddle, citytozipBtn, txtzipcodes);
  168.         vbox.getChildren().addAll(hbox, separator);
  169.  
  170.         return vbox;
  171.     }
  172.  
  173.     // Bottom of program
  174.     public HBox bottomHalf() {
  175.  
  176.         HBox hbox = new HBox();
  177.         hbox.setAlignment(Pos.CENTER);
  178.  
  179.         // Add Label
  180.         Label lblstatus = new Label("Status:");
  181.  
  182.         // Add Properties of TextField
  183.         txtstatus.setEditable(false);
  184.         txtstatus.setMouseTransparent(true);
  185.         txtstatus.setFocusTraversable(false);
  186.         txtstatus.setPrefWidth(500);
  187.  
  188.         // Add to hbox
  189.         hbox.getChildren().addAll(lblstatus, txtstatus);
  190.  
  191.         return hbox;
  192.     }
  193.  
  194.     // Establish a connection to the silvestri database
  195.     private void StartDatabase() {
  196.         String DATABASE = "silvestri";
  197.         String USERNAME = "readonly";
  198.         String PASSWORD = "readonly";
  199.         String driver = "com.mysql.jdbc.Driver";
  200.         String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE + "?user=" + USERNAME + "&password=" + PASSWORD;
  201.         String zip = txtzipcode.getText();
  202.         String state = txtstatemiddle.getText();
  203.         String city = txtcitymiddle.getText();
  204.         try {
  205.             Class.forName(driver);
  206.             System.out.println("Driver loaded successfully");
  207.  
  208.             Connection connection = DriverManager.getConnection(url);
  209.             System.out.println("Database Connected");
  210.             String zipCode = ("Select city, state FROM Zipcodes WHERE zipcode= " + zip);
  211.             String City = ("Select zipcode from Zipcodes where state ='" + state + "' && city ='" + city + "';");
  212.  
  213.             preparedStatement = connection.prepareStatement(zipCode);
  214.             preparedStatement = connection.prepareStatement(City);
  215.         } catch (Exception ex) {
  216.             ex.printStackTrace();
  217.         }
  218.     }
  219.  
  220.     // Send ZipCode to Database
  221.     private void zipDatabase() {
  222.         try {
  223.             String zip = txtzipcode.getText();
  224.             String zipCode = ("Select city, state FROM Zipcodes WHERE zipcode= " + zip);
  225.             ResultSet rset = preparedStatement.executeQuery(zipCode);
  226.             if (rset.next()) {
  227.                 txtcitytop.setText(rset.getString(1));
  228.                 txtstatetop.setText(rset.getString(2));
  229.                 txtstatus.setText("City and State were retrieved from Database!");
  230.             } else {
  231.                 txtstatus.setText("No data retrieved from Database!");
  232.             }
  233.         } catch (SQLException ex) {
  234.             ex.printStackTrace();
  235.         }
  236.     }
  237.  
  238.     // Send City, State to Database
  239.     private void cityDatabase() {
  240.         try {
  241.             String state = txtstatemiddle.getText();
  242.             String city = txtcitymiddle.getText();
  243.             String City = ("Select zipcode from Zipcodes where state ='" + state + "' && city ='" + city + "';");
  244.             ResultSet rset = preparedStatement.executeQuery(City);
  245.             if (rset.next()) {
  246.                 String NumberofZips = "";
  247.                 if (rset.next()) {
  248.                     NumberofZips += (rset.getString(1));
  249.                 }
  250.                 txtzipcodes.setText(NumberofZips);
  251.             } else {
  252.                 txtstatus.setText("City or State you are looking for cannot be found");
  253.                 txtzipcodes.setText("No Zip Code(s)");
  254.             }
  255.         } catch (Exception ex) {
  256.             ex.getMessage();
  257.         }
  258.     }
  259.  
  260.     public static void main(String[] args) {
  261.         launch(args);
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement