Advertisement
Guest User

IP Geolocation Client

a guest
May 14th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. import java.sql.*;
  2. import javafx.geometry.Pos;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.control.Label;
  5. import javafx.scene.control.TextArea;
  6. import javafx.scene.control.TextField;
  7. import javafx.scene.layout.GridPane;
  8. import javafx.scene.layout.HBox;
  9. import javafx.scene.text.Text;
  10.  
  11. /*
  12.  * Name: Christopher Watkins
  13.  * Date: 5/14/2017
  14.  * Course: Intermediate Topics in Java Programming (CSC-112)
  15.  * Final Project
  16.  * SQL DB Client for Ip Geolocation
  17.  *
  18.  * ******************************************************
  19.  * * On my honor as a student, I pledge that I have     *
  20.  * * not received or given any unauthorized assistance  *
  21.  * * in this exam.                                      *
  22.  * ******************************************************
  23.  */
  24.  
  25.  
  26. public class SQLDBPane extends GridPane{
  27.    
  28.     public TextField txt_ip1;
  29.     public TextField txt_ip2;
  30.     public TextField txt_ip3;
  31.     public TextField txt_ip4;
  32.     public TextArea msgBox;
  33.    
  34.     private void paint(){
  35.     //-------------------------GridPane Setup---------------------------
  36.        
  37.         this.setAlignment(Pos.CENTER);
  38.         this.setVgap(15);
  39.    
  40.     //-------------------------Ip HBox Setup---------------------------
  41.    
  42.         HBox ipInput = new HBox(5);
  43.         ipInput.setAlignment(Pos.CENTER);
  44.         ipInput.setStyle("-fx-padding: 10;"       + "-fx-border-style: solid inside;"
  45.                        + "-fx-border-width: 2;"   + "-fx-border-insets: 5;"
  46.                        + "-fx-border-radius: 50;" + "-fx-border-color: blue;");
  47.         //-------------------------Starting Label---------------------------
  48.         Label lbl_ip = new Label("Enter IP Address");
  49.         //-------------------------Each Text Field Box---------------------------
  50.         txt_ip1 = new TextField();
  51.         txt_ip1.setMaxWidth(50);
  52.         txt_ip1.setOnKeyTyped(e -> {maxKeyEvent(txt_ip1);});
  53.         Text dot1 = new Text(".");
  54.         //----------------------------------------------------
  55.         txt_ip2 = new TextField();
  56.         txt_ip2.setMaxWidth(50);
  57.         txt_ip2.setOnKeyTyped(e -> {maxKeyEvent(txt_ip2);});
  58.         Text dot2 = new Text(".");
  59.         //----------------------------------------------------
  60.         txt_ip3 = new TextField();
  61.         txt_ip3.setMaxWidth(50);
  62.         txt_ip3.setOnKeyTyped(e -> {maxKeyEvent(txt_ip3);});
  63.         Text dot3 = new Text(".");
  64.         //----------------------------------------------------
  65.         txt_ip4 = new TextField();
  66.         txt_ip4.setMaxWidth(50);
  67.         txt_ip4.setOnKeyTyped(e -> {maxKeyEvent(txt_ip4);});
  68.         //-------------------------Added to HBox---------------------------
  69.         ipInput.getChildren().addAll(lbl_ip,txt_ip1,dot1,txt_ip2,dot2,txt_ip3,dot3,txt_ip4);
  70.  
  71.     //------------------------Text Area Setup----------------------------  
  72.    
  73.         msgBox = new TextArea();
  74.         msgBox.setMaxWidth(400);
  75.         msgBox.setEditable(false);
  76.    
  77.     //------------------------Button HBox Setup----------------------------
  78.        
  79.         HBox buttons = new HBox(30);
  80.         buttons.setMaxWidth(180);
  81.         buttons.setAlignment(Pos.CENTER);
  82.         buttons.setStyle("-fx-padding: 7;"        + "-fx-border-style: solid inside;"
  83.                        + "-fx-border-width: 2;"   + "-fx-border-insets: 0;"
  84.                        + "-fx-border-radius: 20;" + "-fx-border-color: blue;");
  85.         //-------------------------Locate Button---------------------------
  86.         Button connectDB = new Button("Locate");
  87.         connectDB.setOnAction(e -> {connectEvent();});
  88.         //-------------------------Clear Text Field + Area Button---------------------------
  89.         Button clear = new Button("Clear");
  90.         clear.setOnAction(e -> {txt_ip1.setText("");
  91.                                 txt_ip2.setText("");
  92.                                 txt_ip3.setText("");
  93.                                 txt_ip4.setText("");
  94.                                 msgBox.setText("Cleared");});
  95.         //-------------------------Added to HBox---------------------------
  96.         buttons.getChildren().addAll(connectDB, clear);
  97.    
  98.     //------------------------GridPane Children Setup and Add----------------------------
  99.        
  100.         GridPane.setConstraints(ipInput, 0, 0);
  101.         GridPane.setConstraints(msgBox, 0, 1);
  102.         GridPane.setConstraints(buttons, 0, 2);
  103.         this.getChildren().addAll(ipInput, msgBox, buttons);
  104.     }
  105. //----------------------Key Limit Event (Ip Text Fields)------------------------------ 
  106.     private void maxKeyEvent(TextField ipBox){
  107.         if (ipBox.getLength() >= 3)
  108.             ipBox.deletePreviousChar();
  109.     }
  110. //----------------------DB Connection Event (Locate Button)------------------------------  
  111.     private void connectEvent(){
  112.        
  113.         try {
  114.         //-------------------------Checking For Integers---------------------------
  115.             int int_ip1 = Integer.parseInt(txt_ip1.getText());
  116.             int int_ip2 = Integer.parseInt(txt_ip2.getText());
  117.             int int_ip3 = Integer.parseInt(txt_ip3.getText());
  118.             int int_ip4 = Integer.parseInt(txt_ip4.getText());
  119.         //-------------------------Checking Number Range---------------------------
  120.             if (int_ip1 <= 255 && int_ip1 > 0 && int_ip2 <= 255 && int_ip2 >= 0 && int_ip3 <= 255 && int_ip3 >= 0 && int_ip4 <= 255 && int_ip4 >= 0){
  121.                 String ipMsg = "IP = " + int_ip1 + "." + int_ip2 + "." + int_ip3 + "." + int_ip4;
  122.                 String result = dBConnection(int_ip1, int_ip2, int_ip3);
  123.                 msgBox.setText(ipMsg + "\n" + result);
  124.             }else{
  125.                 msgBox.setText("Error!\nInput Values Between 0 and 255 For Each Box!");
  126.             }
  127.         //------------------------Catches Non-Numeric Chars----------------------------
  128.         }catch(Exception ex){
  129.             msgBox.setText("Error!\nInput Numbers In Each Box!");
  130.         }  
  131.     }
  132. //----------------------The DB Connection------------------------------
  133.     private String dBConnection(int a, int b, int c){
  134.         String result = "Failed to connect!\n";
  135.         String sqltxt = "select C.name, CITY.name From ip4_" + a + " I, countries C, cityByCountry CITY WHERE I.country = C.ID AND I.city = CITY.city AND b = " + b + " AND c = " + c;
  136.         Statement sql = null;
  137.         ResultSet rs = null;
  138.     //-----------------------Try Block for the Connection and Result-----------------------------
  139.         try {
  140.             Class.forName("com.mysql.jdbc.Driver");
  141.             Connection connDB = DriverManager.getConnection("jdbc:mysql://cs.stcc.edu/silvestri?user=readonly&password=readonly");
  142.             result = "Connected to database\n\n";
  143.         //------------------------Statement, ResultSet Command Execution----------------------------       
  144.             sql = connDB.createStatement();
  145.             rs = sql.executeQuery(sqltxt);
  146.         //------------------------Grabs the Location Information of the First Returned Line----------------------------
  147.             if (rs.next()){
  148.                 result = result + "Country: " + rs.getString("C.name") + "\n" + "City: " + java.net.URLDecoder.decode(rs.getString("CITY.name"), "UTF-8") + "\n\n";
  149.             }else{
  150.                 result = result + "No Data Found!\nPress \"Clear\" and Try Again With a New ip\n\n";
  151.             }
  152.         //------------------------Close DB Connections---------------------------- 
  153.             connDB.close();
  154.             sql.close();
  155.             rs.close();
  156.         //------------------------Result Always Kept as If a Log to See All Returns----------------------------
  157.             result = result + "Connection Closed\n";
  158.         }
  159.         catch (Exception e) {
  160.             result = result + "Error!\n" + e.getMessage();
  161.         }
  162.     //-----------------------Return The Result-----------------------------
  163.         return result;
  164.     }
  165. //------------------------Paint----------------------------
  166.     @Override
  167.     public void setWidth(double width){
  168.         super.setWidth(width);
  169.         paint();
  170.     }
  171. //----------------------------------------------------
  172.     @Override
  173.     public void setHeight(double hieght){
  174.         super.setHeight(hieght);
  175.         paint();
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement