Advertisement
Guest User

Untitled

a guest
May 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. /*
  2.  * Michael Sienkiewicz
  3.  * 5/5/17
  4.  * HomeWork final
  5.  * geolocation
  6.  */
  7. //
  8. //On my honor as a student, I pledge that I have not received or given any unauthorized assistance in this exam.
  9.  
  10.  
  11. import java.sql.*;
  12.  
  13. import javafx.application.Application;
  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.stage.Stage;
  23.  
  24. public class IpReaderGUI extends Application{
  25.     TextField ipv4_A = new TextField ();
  26.     TextField ipv4_B = new TextField ();
  27.     TextField ipv4_C = new TextField ();
  28.     TextField ipv4_D = new TextField ();
  29.    
  30.     TextArea outPut = new TextArea();
  31.    
  32.  
  33.     public void start(Stage primaryStage){
  34.         ipv4_A.setPrefWidth(50);
  35.         ipv4_B.setPrefWidth(50);
  36.         ipv4_C.setPrefWidth(50);
  37.         ipv4_D.setPrefWidth(50);
  38.         BorderPane pane = new BorderPane();
  39.  
  40.         pane.setPrefSize(600, 400);
  41.         Scene scene = new Scene(pane);
  42.         HBox box1 = new HBox();
  43.         box1.setAlignment(Pos.CENTER);
  44.         primaryStage.setTitle("Ip Tracer");
  45.         primaryStage.setScene(scene);
  46.         primaryStage.setResizable(false);
  47.         primaryStage.show();
  48.         Label info = new Label("Enter Ip Adress     ");
  49.         Label periodA = new Label(".");
  50.         Label periodB = new Label(".");
  51.         Label periodC = new Label(".");
  52.         Label padding = new Label("   ");
  53.  
  54.        
  55.         Button locate = new Button("Locate");
  56.         locate.setOnAction(e -> locateIp());
  57.        
  58.         box1.getChildren().addAll(info, ipv4_A, periodA, ipv4_B, periodB, ipv4_C, periodC, ipv4_D, padding, locate);
  59.  
  60.        
  61.         pane.setTop(box1);
  62.         pane.setCenter(outPut);
  63.        
  64.         outPut.setDisable(true);
  65.         outPut.setStyle("-fx-opacity: 1.0;");
  66.        
  67.     }
  68.     private void locateIp() {
  69.         int ipv4_IA;
  70.         int ipv4_IB;
  71.         int ipv4_IC;
  72.         int ipv4_ID;
  73.         try{
  74.         ipv4_IA = Integer.parseInt(ipv4_A.getText());
  75.         ipv4_IB = Integer.parseInt(ipv4_B.getText());
  76.         ipv4_IC = Integer.parseInt(ipv4_C.getText());
  77.         ipv4_ID = Integer.parseInt(ipv4_D.getText());
  78.         }catch(Exception e){
  79.             ipv4_IA = -1;
  80.             ipv4_IB = -1;
  81.             ipv4_IC = -1;
  82.             ipv4_ID = -1;
  83.         }
  84.         if ((ipv4_IA <= 255 && ipv4_IA > 0) && (ipv4_IA <= 255 && ipv4_IA > 0) && (ipv4_IA <= 255 && ipv4_IA > 0) && (ipv4_IA <= 255 && ipv4_IA > 0)){
  85.             callServer(ipv4_IA, ipv4_IB, ipv4_IC);
  86.         }
  87.         else {
  88.             outPut.setText("error invalid IP");
  89.         }
  90.     }
  91.    
  92.     private void callServer(int ipv4_IA, int ipv4_IB, int ipv4_IC) {
  93.         try {
  94.          final String DATABASE = "silvestri";
  95.             final String USERNAME = "readonly";
  96.             final String PASSWORD = "readonly";
  97.          
  98.          
  99.             String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE +
  100.                                 "?user=" + USERNAME + "&password=" + PASSWORD;
  101.         // TODO Auto-generated method stub
  102.  
  103.             Class.forName("com.mysql.jdbc.Driver");
  104.             System.out.println("Driver Loaded");
  105.             Connection conn = DriverManager.getConnection(url);
  106.             System.out.println("connected");
  107.             Statement state = conn.createStatement();
  108.             String sql = "select C.name, CITY.name From ip4_" + ipv4_IA + " I, countries C, cityByCountry CITY WHERE I.country = C.ID AND I.city = CITY.city AND b = " + ipv4_IB + " AND c = " + ipv4_IC;
  109.            
  110.             ResultSet resultSet = state.executeQuery(sql);
  111.             System.out.println("sql recieved");
  112.             while (resultSet.next()){
  113.                 String country = resultSet.getString("C.name");
  114.                 String city = resultSet.getString("CITY.name");
  115.                     city = city.replaceAll("%20", " ");
  116.                     city = city.replaceAll("%2C", ",");
  117.                    
  118.  
  119.                 outPut.setText(country + " " + city);
  120.             }
  121.            
  122.            
  123.            
  124.            
  125.            
  126.         } catch (Exception e) {
  127.             System.out.println("Fail");
  128.         }
  129.        
  130.        
  131.     }
  132.     static void pStr(String p) {
  133.         System.out.println(p);
  134.     }
  135.     public static void main(String args[]){
  136.         launch(args);
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement