I need to connect to databse. But not sure where can i add my database connected
ID: 3719849 • Letter: I
Question
I need to connect to databse. But not sure where can i add my database connected code on the following code?
import java.util.ArrayList;
import java.util.Arrays;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Project9 extends Application {
ArrayList<CourseEntry> enrolled = new ArrayList<>();
String courses[] = {"CS1T1", "CS1T2", "CS1T3", "CS1T4", "CS1T5" };
public static void main(String... args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// create buttons
Button btnRegister = new Button("Register");
Button btnExit = new Button("Exit");
// create SSN label and text box
Label lbSSN = new Label("SSN");
TextField tfSSN = new TextField();
// create Course label and combo box
Label lbCourses = new Label("Course");
ComboBox cbCourses = new ComboBox(); // create a combo box
// Add courses to the combobox
cbCourses.getItems().addAll(Arrays.asList(courses));
// add an action event handle to the button register
btnRegister.setOnAction((ActionEvent t) -> {
String ssn = tfSSN.getText(); // read the ssn
String course= cbCourses.getValue().toString();// get selected course
register(ssn, course);
tfSSN.setText("");
});
// add an action event handle to the button exit
btnExit.setOnAction((ActionEvent t) -> {
System.out.println("Exiting the application");
System.exit(0);
});
// add all components to a grid layout
GridPane layout = new GridPane();
layout.add(lbSSN, 0, 0, 1, 1);
layout.add(tfSSN, 1, 0, 1, 1);
layout.add(lbCourses, 0, 1, 1, 1);
layout.add(cbCourses, 1, 1, 1, 1);
layout.add(btnRegister, 1, 4, 4, 4);
layout.add(btnExit, 2, 4, 4, 4);
// set a gap between components
layout.setHgap(10);
layout.setVgap(10);
Scene scene = new Scene(layout, 290, 140);
primaryStage.setTitle("Enrollment");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
private void register(String ssn, String course) {
CourseEntry ce = new CourseEntry(ssn, course);
enrolled.add(ce);
System.out.println("Entrolled : "+ce);
}
}
// a class to hold one registration
class CourseEntry {
private String SSN, Course;
public CourseEntry(String SSN, String Course) {
this.SSN = SSN;
this.Course = Course;
}
public String getSSN() {
return SSN;
}
public void setSSN(String SSN) {
this.SSN = SSN;
}
public String getCourse() {
return Course;
}
public void setCourse(String Course) {
this.Course = Course;
}
@Override
public String toString() {
return "CourseEntry[" + "SSN : " + SSN + ", Course : " + Course + ']';
}
}
Explanation / Answer
package squarecalculator;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SquareCalculator extends Application {
private TextField inputField;
private TextField outputField;
private Button btnSquare;
@Override
public void start(Stage primaryStage) {
inputField =new TextField();
outputField = new TextField();
btnSquare = new Button();
//btnSquare.prefHeight(100);
//btnSquare.prefWidth(100);
btnSquare.setText("Get Square");
VBox box=new VBox(10);
box.autosize();
box.setAlignment(Pos.CENTER);
btnSquare.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double in=Double.parseDouble(inputField.getText());
outputField.setText(Double.toString(in*in));
}
});
// StackPane root = new StackPane();
box.getChildren().addAll(inputField,outputField,btnSquare);
Scene scene = new Scene(box, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.