Advertisement
Guest User

Untitled

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