Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Michael Sienkiewicz
- * 5/5/17
- * HomeWork final
- */
- import java.sql.*;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.stage.Stage;
- public class IpReaderGUI extends Application{
- TextField ipv4_A = new TextField ();
- TextField ipv4_B = new TextField ();
- TextField ipv4_C = new TextField ();
- TextField ipv4_D = new TextField ();
- TextArea outPut = new TextArea();
- public void start(Stage primaryStage){
- ipv4_A.setPrefWidth(50);
- ipv4_B.setPrefWidth(50);
- ipv4_C.setPrefWidth(50);
- ipv4_D.setPrefWidth(50);
- BorderPane pane = new BorderPane();
- pane.setPrefSize(600, 400);
- Scene scene = new Scene(pane);
- HBox box1 = new HBox();
- box1.setAlignment(Pos.CENTER);
- primaryStage.setTitle("Ip Tracer");
- primaryStage.setScene(scene);
- primaryStage.setResizable(false);
- primaryStage.show();
- Label info = new Label("Enter Ip Adress ");
- Label periodA = new Label(".");
- Label periodB = new Label(".");
- Label periodC = new Label(".");
- Label padding = new Label(" ");
- Button locate = new Button("Locate");
- locate.setOnAction(e -> locateIp());
- box1.getChildren().addAll(info, ipv4_A, periodA, ipv4_B, periodB, ipv4_C, periodC, ipv4_D, padding, locate);
- pane.setTop(box1);
- pane.setCenter(outPut);
- outPut.setDisable(true);
- outPut.setStyle("-fx-opacity: 1.0;");
- }
- private void locateIp() {
- int ipv4_IA;
- int ipv4_IB;
- int ipv4_IC;
- int ipv4_ID;
- try{
- ipv4_IA = Integer.parseInt(ipv4_A.getText());
- ipv4_IB = Integer.parseInt(ipv4_B.getText());
- ipv4_IC = Integer.parseInt(ipv4_C.getText());
- ipv4_ID = Integer.parseInt(ipv4_D.getText());
- }catch(Exception e){
- ipv4_IA = -1;
- ipv4_IB = -1;
- ipv4_IC = -1;
- ipv4_ID = -1;
- }
- 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)){
- callServer(ipv4_IA, ipv4_IB, ipv4_IC);
- }
- else {
- outPut.setText("error invalid IP");
- }
- }
- private void callServer(int ipv4_IA, int ipv4_IB, int ipv4_IC) {
- try {
- final String DATABASE = "silvestri";
- final String USERNAME = "readonly";
- final String PASSWORD = "readonly";
- String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE +
- "?user=" + USERNAME + "&password=" + PASSWORD;
- // TODO Auto-generated method stub
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver Loaded");
- Connection conn = DriverManager.getConnection(url);
- System.out.println("connected");
- Statement state = conn.createStatement();
- 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;
- ResultSet resultSet = state.executeQuery(sql);
- System.out.println("sql recieved");
- while (resultSet.next()){
- String country = resultSet.getString("C.name");
- String city = resultSet.getString("CITY.name");
- city = city.replaceAll("%20", " ");
- city = city.replaceAll("%2C", ",");
- outPut.setText(country + " " + city);
- }
- } catch (Exception e) {
- System.out.println("Fail");
- }
- }
- static void pStr(String p) {
- System.out.println(p);
- }
- public static void main(String args[]){
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement