Created
April 27, 2021 18:37
-
-
Save jananpatel2002/9cffd415c5a252760213989b8169ff02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.root{ | |
-fx-background-color: #00ffff; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///* | |
// * Name: Janan Patel | |
// * Date: 4/22/2020 | |
// * Course Number: CSC 112 | |
// * Course Name: Intermediate topics in Java Programming | |
// * Problem Number: 11 | |
// * Email: jkpatel2001@student.stcc.edu | |
// * GUI for Database part 1 | |
// * Add JavaFX Library to your project | |
// * Add VM Argument to Run Configuration: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml | |
import java.sql.Connection; | |
import java.sql.*; | |
import java.sql.DriverManager; | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.Labeled; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class DatabaseGUI extends Application { | |
private static final int APPWIDTH = 700; | |
private static final int APPHEIGHT = 325; | |
private static final String TITLE = "The Email Program"; | |
// I include the textFields here because they need to be accessed from other | |
// classes. | |
private TextField tfipAddress2 = new TextField(); | |
private TextField tfipAddress3 = new TextField(); | |
private TextField tfipAddress1 = new TextField(); | |
private TextField tfipAddress4 = new TextField(); | |
private TextArea textArea = new TextArea(); | |
// All the HBoxes/VBoxes and extra components that will be needed through the | |
// program. | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
AppGUI scene = new AppGUI(); | |
Scene scene1 = new Scene(scene, APPWIDTH, APPHEIGHT); | |
primaryStage.setTitle(TITLE); | |
primaryStage.setScene(scene1); | |
primaryStage.show(); | |
primaryStage.setResizable(false); | |
scene.getStylesheets().addAll("Database.css"); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
private class AppGUI extends BorderPane { | |
public AppGUI() { | |
topBox topBox = new topBox(); | |
this.setTop(topBox); | |
textAreaBox centerBox = new textAreaBox(); | |
this.setCenter(centerBox); | |
} | |
public class topBox extends VBox { | |
public topBox() { | |
HBox ipAddress = new HBox(); | |
Labeled lbipAddress = new Label("Enter IP Address: "); | |
Label period = new Label("."); | |
Label period2 = new Label("."); | |
Label period3 = new Label("."); | |
Button locate = new Button("Locate"); | |
lbipAddress.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period2.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period3.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
tfipAddress1.setPrefColumnCount(5); | |
tfipAddress1.setAlignment(Pos.CENTER); | |
tfipAddress1.setPrefHeight(50); | |
tfipAddress2.setPrefColumnCount(5); | |
tfipAddress2.setAlignment(Pos.CENTER); | |
tfipAddress2.setPrefHeight(50); | |
tfipAddress3.setPrefColumnCount(5); | |
tfipAddress3.setAlignment(Pos.CENTER); | |
tfipAddress3.setPrefHeight(50); | |
tfipAddress4.setPrefColumnCount(5); | |
tfipAddress4.setAlignment(Pos.CENTER); | |
tfipAddress4.setPrefHeight(50); | |
locate.setStyle("-fx-font-weight: bold; -fx-font-size: 17pt; -fx-background-color: grey"); | |
locate.setOnAction(e -> { | |
calculate(); | |
}); | |
ipAddress.setSpacing(3); | |
ipAddress.getChildren().addAll(lbipAddress, tfipAddress1, period, tfipAddress2, period2, tfipAddress3, | |
period3, tfipAddress4, locate); | |
this.setAlignment(Pos.BASELINE_LEFT); | |
this.setPadding(new Insets(20)); | |
this.setSpacing(10); | |
this.getChildren().addAll(ipAddress); | |
} | |
} | |
} | |
private class textAreaBox extends VBox { | |
public textAreaBox() { | |
textArea.setPrefColumnCount(5); | |
textArea.setWrapText(true); | |
textArea.setStyle("-fx-font-size: 12pt;"); | |
textArea.setPadding(new Insets(15)); | |
textArea.setPrefRowCount(10); | |
textArea.setEditable(false); | |
this.setAlignment(Pos.CENTER); | |
this.getChildren().addAll(textArea); | |
} | |
} | |
private void calculate() { | |
int a = Integer.parseInt(tfipAddress1.getText()); | |
int b = Integer.parseInt(tfipAddress2.getText()); | |
int c = Integer.parseInt(tfipAddress3.getText()); | |
int d = Integer.parseInt(tfipAddress4.getText()); | |
if ((tfipAddress1.getText() != "") && (tfipAddress2.getText() != "") && (tfipAddress3.getText() != "") | |
&& (tfipAddress4.getText() != "")) { | |
if ((a > 0 && a < 255) | |
&& (b > 0 && b < 255) | |
&& (c > 0 && c < 255) | |
&& (d > 0 && d < 255)) { | |
textArea.clear(); | |
var displayIP = String.format("%s.%s.%s.%s", a, b, c, d); | |
try { | |
var geolocator = new GeoIPClient(); | |
String location = ""; | |
try { | |
location = geolocator.lookupIp(a, b, c, d); | |
} catch (Exception ex) { | |
location = "Unexpected Error: " + ex.getMessage(); | |
} | |
var result = String.format("%s => %s\n", displayIP, location); | |
textArea.appendText(result); | |
} catch (Exception e) { | |
textArea.setText(tfipAddress1.getText() + "." + tfipAddress2.getText() + "." | |
+ tfipAddress3.getText() + "." + tfipAddress4.getText() + "\n" + e.getMessage()); | |
} | |
} else { | |
textArea.setText( | |
"ERROR: Looks like you either have a number too big or too small \nPlease make sure each field is between 0 and" | |
+ " 255 and please don't leave either one blank"); | |
} | |
} else { | |
textArea.appendText("ERROR: Please don't leave any blanks"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class GeoIPClient { | |
// MySql connection constants | |
private static final String DATABASE = "silvestri"; | |
private static final String USERNAME = "readonly"; | |
private static final String PASSWORD = "readonly"; | |
private static String driver = "com.mysql.jdbc.Driver"; | |
private static String url = "jdbc:mysql://localhost:6000/" + DATABASE; | |
private static String driverState = ""; | |
static { | |
try { | |
Class.forName(driver); | |
} catch (Exception ex) { | |
driverState = "Cannot load SQL Driver: " + ex; | |
} | |
}; | |
public String lookupIp(int octet1, int octet2, int octet3, int octet4) throws SQLException { | |
if (!driverState.isEmpty()) { | |
return driverState; | |
} | |
long bit; | |
bit = (long) (octet1 * (Math.pow(256, 3)) + octet2 * (Math.pow(256, 2)) + octet3 * (Math.pow(256, 1)) | |
+ octet4 * (Math.pow(256, 0))); | |
var location = "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure\r\n" | |
+ "\r\n" | |
+ "The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.\r\n" | |
+ ""; | |
try (Connection conn = DriverManager.getConnection(url, USERNAME, PASSWORD); | |
Statement statement = conn.createStatement();) { | |
ResultSet resultSet = statement | |
.executeQuery("select country_name, region_name, city_name from ip2location_db3 where " + bit | |
+ ">=ip_from and " + bit + "<=ip_to"); | |
if (resultSet.next()) { | |
location = ((resultSet.getString("country_name")) + ", " + (resultSet.getString("region_name")) + ", " | |
+ (resultSet.getString("city_name"))); | |
if (location.equals("-, -, -")) { | |
location = "Cannot perform Geolocation with this ip address"; | |
} | |
} | |
} catch (Exception ex) { | |
System.out.println(ex); | |
} | |
return location; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment