Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import javafx.geometry.Pos;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.text.Text;
- /*
- * Name: Christopher Watkins
- * Date: 5/14/2017
- * Course: Intermediate Topics in Java Programming (CSC-112)
- * Final Project
- * SQL DB Client for Ip Geolocation
- *
- * ******************************************************
- * * On my honor as a student, I pledge that I have *
- * * not received or given any unauthorized assistance *
- * * in this exam. *
- * ******************************************************
- */
- public class SQLDBPane extends GridPane{
- public TextField txt_ip1;
- public TextField txt_ip2;
- public TextField txt_ip3;
- public TextField txt_ip4;
- public TextArea msgBox;
- private void paint(){
- //-------------------------GridPane Setup---------------------------
- this.setAlignment(Pos.CENTER);
- this.setVgap(15);
- //-------------------------Ip HBox Setup---------------------------
- HBox ipInput = new HBox(5);
- ipInput.setAlignment(Pos.CENTER);
- ipInput.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;"
- + "-fx-border-width: 2;" + "-fx-border-insets: 5;"
- + "-fx-border-radius: 50;" + "-fx-border-color: blue;");
- //-------------------------Starting Label---------------------------
- Label lbl_ip = new Label("Enter IP Address");
- //-------------------------Each Text Field Box---------------------------
- txt_ip1 = new TextField();
- txt_ip1.setMaxWidth(50);
- txt_ip1.setOnKeyTyped(e -> {maxKeyEvent(txt_ip1);});
- Text dot1 = new Text(".");
- //----------------------------------------------------
- txt_ip2 = new TextField();
- txt_ip2.setMaxWidth(50);
- txt_ip2.setOnKeyTyped(e -> {maxKeyEvent(txt_ip2);});
- Text dot2 = new Text(".");
- //----------------------------------------------------
- txt_ip3 = new TextField();
- txt_ip3.setMaxWidth(50);
- txt_ip3.setOnKeyTyped(e -> {maxKeyEvent(txt_ip3);});
- Text dot3 = new Text(".");
- //----------------------------------------------------
- txt_ip4 = new TextField();
- txt_ip4.setMaxWidth(50);
- txt_ip4.setOnKeyTyped(e -> {maxKeyEvent(txt_ip4);});
- //-------------------------Added to HBox---------------------------
- ipInput.getChildren().addAll(lbl_ip,txt_ip1,dot1,txt_ip2,dot2,txt_ip3,dot3,txt_ip4);
- //------------------------Text Area Setup----------------------------
- msgBox = new TextArea();
- msgBox.setMaxWidth(400);
- msgBox.setEditable(false);
- //------------------------Button HBox Setup----------------------------
- HBox buttons = new HBox(30);
- buttons.setMaxWidth(180);
- buttons.setAlignment(Pos.CENTER);
- buttons.setStyle("-fx-padding: 7;" + "-fx-border-style: solid inside;"
- + "-fx-border-width: 2;" + "-fx-border-insets: 0;"
- + "-fx-border-radius: 20;" + "-fx-border-color: blue;");
- //-------------------------Locate Button---------------------------
- Button connectDB = new Button("Locate");
- connectDB.setOnAction(e -> {connectEvent();});
- //-------------------------Clear Text Field + Area Button---------------------------
- Button clear = new Button("Clear");
- clear.setOnAction(e -> {txt_ip1.setText("");
- txt_ip2.setText("");
- txt_ip3.setText("");
- txt_ip4.setText("");
- msgBox.setText("Cleared");});
- //-------------------------Added to HBox---------------------------
- buttons.getChildren().addAll(connectDB, clear);
- //------------------------GridPane Children Setup and Add----------------------------
- GridPane.setConstraints(ipInput, 0, 0);
- GridPane.setConstraints(msgBox, 0, 1);
- GridPane.setConstraints(buttons, 0, 2);
- this.getChildren().addAll(ipInput, msgBox, buttons);
- }
- //----------------------Key Limit Event (Ip Text Fields)------------------------------
- private void maxKeyEvent(TextField ipBox){
- if (ipBox.getLength() >= 3)
- ipBox.deletePreviousChar();
- }
- //----------------------DB Connection Event (Locate Button)------------------------------
- private void connectEvent(){
- try {
- //-------------------------Checking For Integers---------------------------
- int int_ip1 = Integer.parseInt(txt_ip1.getText());
- int int_ip2 = Integer.parseInt(txt_ip2.getText());
- int int_ip3 = Integer.parseInt(txt_ip3.getText());
- int int_ip4 = Integer.parseInt(txt_ip4.getText());
- //-------------------------Checking Number Range---------------------------
- 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){
- String ipMsg = "IP = " + int_ip1 + "." + int_ip2 + "." + int_ip3 + "." + int_ip4;
- String result = dBConnection(int_ip1, int_ip2, int_ip3);
- msgBox.setText(ipMsg + "\n" + result);
- }else{
- msgBox.setText("Error!\nInput Values Between 0 and 255 For Each Box!");
- }
- //------------------------Catches Non-Numeric Chars----------------------------
- }catch(Exception ex){
- msgBox.setText("Error!\nInput Numbers In Each Box!");
- }
- }
- //----------------------The DB Connection------------------------------
- private String dBConnection(int a, int b, int c){
- String result = "Failed to connect!\n";
- 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;
- Statement sql = null;
- ResultSet rs = null;
- //-----------------------Try Block for the Connection and Result-----------------------------
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connDB = DriverManager.getConnection("jdbc:mysql://cs.stcc.edu/silvestri?user=readonly&password=readonly");
- result = "Connected to database\n\n";
- //------------------------Statement, ResultSet Command Execution----------------------------
- sql = connDB.createStatement();
- rs = sql.executeQuery(sqltxt);
- //------------------------Grabs the Location Information of the First Returned Line----------------------------
- if (rs.next()){
- result = result + "Country: " + rs.getString("C.name") + "\n" + "City: " + java.net.URLDecoder.decode(rs.getString("CITY.name"), "UTF-8") + "\n\n";
- }else{
- result = result + "No Data Found!\nPress \"Clear\" and Try Again With a New ip\n\n";
- }
- //------------------------Close DB Connections----------------------------
- connDB.close();
- sql.close();
- rs.close();
- //------------------------Result Always Kept as If a Log to See All Returns----------------------------
- result = result + "Connection Closed\n";
- }
- catch (Exception e) {
- result = result + "Error!\n" + e.getMessage();
- }
- //-----------------------Return The Result-----------------------------
- return result;
- }
- //------------------------Paint----------------------------
- @Override
- public void setWidth(double width){
- super.setWidth(width);
- paint();
- }
- //----------------------------------------------------
- @Override
- public void setHeight(double hieght){
- super.setHeight(hieght);
- paint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement