Question: Create a java program that performs a database lookup. Edit it so the four octets taken in the GUI are converted to a single number.

Create a java program that performs a database lookup. Edit it so the four octets taken in the GUI are converted to a single number. The program is supposed to take this number then search for it the database record and return the geolocation of it,country, region, and city if they are there (if not it leaves it blank or if the number doesnt exist display a message saying it cant).The code I connected a mysql connector v5.1.49.First code is starter (dont touch it), second is what to edit.
____________________
import java.io.IOException;
import java.sql.SQLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class GeoLocApp extends Application {
private TextField txtA, txtB, txtC, txtD;
private TextArea taLocation;
public void start(Stage primaryStage) throws IOException {
Scene scene = new Scene(buildStagePane(),650,250);
primaryStage.setScene(scene);
primaryStage.setTitle("IP Address Locator");
primaryStage.setResizable(false);
primaryStage.show();
}
private BorderPane buildStagePane(){
final String PANESTYLE ="-fx-font-weight: bold; -fx-font-size: 12pt";
BorderPane bp = new BorderPane();
bp.setStyle(PANESTYLE);
bp.setTop(buildInputBox());
bp.setCenter(buildOutputBox());
return bp;
}
private HBox buildInputBox(){
HBox inputBox = new HBox(10);
inputBox.setPadding(new Insets(10,10,0,10));
inputBox.setSpacing(10);
inputBox.setAlignment(Pos.CENTER);
inputBox.setPrefWidth(600);
txtA = new TextField();
txtA.setPrefColumnCount(3);
txtB = new TextField();
txtB.setPrefColumnCount(3);
txtC = new TextField();
txtC.setPrefColumnCount(3);
txtD = new TextField();
txtD.setPrefColumnCount(3);
Label lblEnter = new Label("IP Address:");
Label lblDot = new Label(".");
lblDot.setTranslateY(8);
Label lblDot1= new Label(".");
lblDot1.setTranslateY(8);
Label lblDot2= new Label(".");
lblDot2.setTranslateY(8);
var btnLocate = generateButton("Locate", e -> locate());
var btnClear = generateButton("Clear", e -> clear());
inputBox.getChildren().addAll(lblEnter, txtA, lblDot, txtB, lblDot1, txtC, lblDot2, txtD,
btnLocate, btnClear);
return inputBox;
}
private HBox buildOutputBox(){
HBox outputBox = new HBox();
taLocation = new TextArea();
taLocation.setPrefWidth(600);
taLocation.setEditable(false);
outputBox.setAlignment(Pos.TOP_CENTER);
outputBox.setPadding(new Insets(10));
outputBox.setSpacing(10);
outputBox.getChildren().add(taLocation);
return outputBox;
}
private Button generateButton(String caption, EventHandler e){
var btn = new Button();
btn.setText(caption);
btn.setOnAction(e);
return btn;
}
private void locate(){
var displayIP = String.format("%s.%s.%s.%s", txtA.getText(), txtB.getText(), txtC.getText(), txtD.getText());
String location;
try {
location = GeoIPClient.lookupIp(txtA, txtB, txtC, txtD);
} catch (SQLException ex){
location ="SQL Exception: "+ ex;
}
taLocation.appendText(displayIP +": "+ location);
}
private void clear(){
txtA.clear();
txtB.clear();
txtC.clear();
txtD.clear();
taLocation.clear();
txtA.requestFocus();
}
public static void main(String[] args){
launch(args);
}
}
__________________________________
import java.sql.*;
import javafx.scene.control.TextField;
public class GeoIPClient {
private static final String DATABASE = "change";
private static final String USERNAME = "change";
private static final String PASSWORD = "change";
private static String driver = "com.mysql.jdbc.Driver";
private static String url ="jdbc:mysql://change/"+ DATABASE;
private static String driverState ="";
static {
try {
Class.forName(driver);
}
catch (Exception ex){
driverState = "Cannot load SQL Driver: "+ ex;
}
};
public static String lookupIp(TextField octet1, TextField octet2, TextField octet3, TextField octet4) throws SQLException {
if (!driverState.isEmpty()){
return driverState;
}
var location = "Somewhere";
return location;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!