Advertisement
luliu

A Zip code to City/State Translator Database APP

May 10th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 5/10/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  * Email: lliu0001@student.stcc.edu
  7.  *
  8.  * Assignment: HW # 18
  9.  * Programe Description:
  10.  * A Zip code to City/State Translator Database APP
  11.  */
  12. import javafx.application.Application;
  13. import javafx.geometry.Insets;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.TextArea;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.layout.BorderPane;
  21. import javafx.scene.layout.HBox;
  22. import javafx.scene.layout.VBox;
  23. import javafx.scene.paint.Color;
  24. import javafx.scene.shape.Line;
  25. import javafx.stage.Stage;
  26. import java.sql.*;
  27.  
  28. public class ZipCodeApp extends Application {
  29.     private PreparedStatement preparedStatement;
  30.     private PreparedStatement preparedStatement1;
  31.     private TextField tfZipCode = new TextField();
  32.     private TextField tfCITY = new TextField();
  33.     private TextField tfSTATE = new TextField();
  34.     private TextField tfCity = new TextField();
  35.     private TextField tfState = new TextField();
  36.     TextArea taZipCode = new TextArea();
  37.     TextField tfStatus = new TextField();
  38.  
  39.     @Override // Override the start method in the Application class
  40.     public void start(Stage primaryStage) {
  41.         // Initialize database connection and create a Statement object
  42.         initializeDB();
  43.  
  44.         // Create pane to hold the label and text field
  45.         HBox hBox = new HBox(10);
  46.  
  47.         tfZipCode.setAlignment(Pos.CENTER_LEFT);
  48.         tfCity.setAlignment(Pos.CENTER_LEFT);
  49.         tfState.setAlignment(Pos.CENTER_LEFT);
  50.         Button bnToCity = new Button("Zip to City");
  51.         hBox.setPadding(new Insets(5, 15, 25, 5));
  52.         hBox.getChildren().addAll(new Label("Zip Code:"), tfZipCode, bnToCity, new Label("City:"), tfCity,
  53.                 new Label("State:"), tfState);
  54.  
  55.         HBox hBox2 = new HBox(10);
  56.         hBox2.setPadding(new Insets(5, 15, 25, 5));
  57.         tfCITY.setAlignment(Pos.CENTER_LEFT);
  58.         tfSTATE.setAlignment(Pos.CENTER_LEFT);
  59.         Button bnToZip = new Button("City to Zip");
  60.  
  61.         taZipCode.setMaxSize(130, 130);
  62.         taZipCode.setText("Zip Codes");
  63.         hBox2.getChildren().addAll(new Label("City:"), tfCITY, new Label("State:"), tfSTATE, bnToZip, taZipCode);
  64.         Line line1 = new Line(10, 10, 750, 10);
  65.         line1.setStrokeWidth(3);
  66.         line1.setStroke(Color.WHITE);
  67.         BorderPane centerPane = new BorderPane();
  68.         centerPane.setTop(line1);
  69.         centerPane.setCenter(hBox2);
  70.  
  71.         HBox hBox3 = new HBox(10);
  72.         hBox3.setPadding(new Insets(5, 15, 25, 80));
  73.         tfStatus.setMinSize(510, 20);
  74.         tfStatus.setAlignment(Pos.CENTER);
  75.         hBox3.getChildren().addAll(new Label("Status:"), tfStatus);
  76.         Line line2 = new Line(10, 200, 750, 200);
  77.         line2.setStrokeWidth(3);
  78.         line2.setStroke(Color.WHITE);
  79.         BorderPane bottomPane = new BorderPane();
  80.         bottomPane.setCenter(hBox3);
  81.         bottomPane.setTop(line2);
  82.  
  83.         // Create pane to hold all
  84.         BorderPane mainPane = new BorderPane();
  85.         mainPane.setPadding(new Insets(25, 5, 5, 5));
  86.         mainPane.setStyle("-fx-background-color: #D3D3D3;");
  87.         mainPane.setTop(hBox);
  88.         mainPane.setCenter(centerPane);
  89.         mainPane.setBottom(bottomPane);
  90.  
  91.         // catch the invalid input
  92.         bnToCity.setOnAction(e -> {
  93.             try {
  94.                 int zipCode = Integer.parseInt(tfZipCode.getText());
  95.                 if (tfZipCode.getText().length() == 5) {
  96.                     tfStatus.setText("Valid Zip Code");
  97.                     showAddress();
  98.                 } else {
  99.                     tfStatus.setText("The ZIP Code must be 5 digits integer");
  100.                 }
  101.             } catch (Exception ex) {
  102.                 tfStatus.setText("The ZIP Code must be 5 digits integer");
  103.             }
  104.         });
  105.  
  106.         bnToZip.setOnAction(e -> showZipCodes());
  107.  
  108.         // Create a scene and place it in the stage
  109.         Scene scene = new Scene(mainPane, 760, 300);
  110.         primaryStage.setTitle("Zip Code Translation System"); // Set the stage
  111.         primaryStage.setScene(scene); // Place the scene in the stage
  112.         primaryStage.show(); // Display the stage
  113.  
  114.     }
  115.  
  116.     // Method initializeDB
  117.     private void initializeDB() {
  118.         final String DATABASE = "silvestri";
  119.         final String USERNAME = "readonly";
  120.         final String PASSWORD = "readonly";
  121.         String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE + "?user=" + USERNAME + "&password=" + PASSWORD;
  122.         try {
  123.             // Load the JDBC driver
  124.             Class.forName("com.mysql.jdbc.Driver");
  125.  
  126.             // Establish a connection
  127.             Connection connection = DriverManager.getConnection(url);
  128.  
  129.             // Create a statement
  130.             String queryString = "select state, city from Zipcodes " + "where zipCode  = ? ";
  131.             String queryString1 = "select zipcode from Zipcodes " + "where city = ?" + " and state = ?";
  132.             preparedStatement = connection.prepareStatement(queryString);
  133.             preparedStatement1 = connection.prepareStatement(queryString1);
  134.         } catch (Exception ex) {
  135.             ex.printStackTrace();
  136.         }
  137.     }
  138.  
  139.     // Method showAddress
  140.     private void showAddress() {
  141.         String ZipCode = tfZipCode.getText();
  142.         // String City = tfCITY.getText();
  143.         // String State = tfSTATE.getText();
  144.         try {
  145.             preparedStatement.setString(1, ZipCode);
  146.             // preparedStatement.setString(2, City);
  147.             // preparedStatement.setString(3, State);
  148.             ResultSet rset = preparedStatement.executeQuery();
  149.             if (rset.next()) {
  150.                 String city = rset.getString(2);
  151.                 String state = rset.getString(1);
  152.                 // Display result
  153.                 tfCity.setText(city);
  154.                 tfState.setText(state);
  155.             } else {
  156.                 tfStatus.setText("Not found");
  157.             }
  158.         } catch (SQLException ex) {
  159.             ex.printStackTrace();
  160.         }
  161.     }
  162.  
  163.     // Method showZipCodes
  164.     private void showZipCodes() {
  165.         String City = tfCITY.getText();
  166.         String State = tfSTATE.getText();
  167.         try {
  168.             preparedStatement1.setString(1, City);
  169.             preparedStatement1.setString(2, State);
  170.             ResultSet rset = preparedStatement1.executeQuery();
  171.  
  172.             if (rset.next()) {
  173.                 String zipCode = rset.getString("zipcode");
  174.                 taZipCode.setText(zipCode + "" + "\n");
  175.                 while (rset.next()) {
  176.                     zipCode = rset.getString("zipcode");
  177.                     taZipCode.setText(taZipCode.getText() + zipCode + "" + "\n");
  178.                 }
  179.             } else {
  180.                 tfStatus.setText("Not found");
  181.             }
  182.         } catch (SQLException ex) {
  183.             ex.printStackTrace();
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * The main method is only needed for the IDE with limited JavaFX support.
  189.      * Not needed for running from the command line.
  190.      */
  191.     public static void main(String[] args) {
  192.         launch(args);
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement