我在netbeans中创建了一个简单的JAVAFX应用程序。用户可以登录和搜索员工详细信息。但问题是,在创建JDBC连接并从应用程序单击提交按钮后,我无法获取任何结果。我已经独立测试了JDBC连接并且它工作正常,但它在JAVAFX应用程序中不起作用。 这是我的JDBC连接代码:
private static Connection conn;
private static Statement stmt;
private static String url = "jdbc:mysql://localhost/EmpAttendance";
private static String user = "root";//Username of database
private static String pass = "root";//Password of database
public static Connection connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pass);
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM EmpAttendance.emp_attendance;";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("trans_id"));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(JDBCDemo.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}
这是我的JAVAFX应用程序代码:
Scene scene, scene2;
Connection c;
@Override
public void start(Stage stage) throws Exception {
//login layout
StackPane root1 = new StackPane();
//employee ids addition layout
StackPane root2 = new StackPane();
VBox scene2Box = secondScene();
VBox formLayout = firstScene(stage);
//Adding form layout to the root layout
root1.getChildren().addAll(formLayout);
//Adding second scene to add employees
root2.getChildren().addAll(scene2Box);
//adding login scene to the stage
scene = new Scene(root1, 500, 250);
//adding employee id screen to the stage
scene2 = new Scene(root2, 500, 250);
stage.setTitle("My window");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private VBox secondScene() {
TextField field = new TextField();
HBox box = new HBox();
VBox mainBox = new VBox();
ListView<String> myview = new ListView<>();
Button submit = new Button();
submit.setText("Submit");
Button addBtn = new Button();
addBtn.setText("Add");
addBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (!field.getText().trim().isEmpty()) {
System.out.println(field.getText());
myview.getItems().add(field.getText());
} else {
Alert alert = new Alert(AlertType.WARNING, "Please enter Employee ID ", ButtonType.OK);
alert.showAndWait();
}
}
});
submit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(!myview.getItems().isEmpty()){
c = JDBCDemo.connect();
}else{
Alert alert = new Alert(AlertType.WARNING, "Please enter atleast one Employee ID ", ButtonType.OK);
alert.showAndWait();
}
}
});
box.getChildren().addAll(field, addBtn);
HBox.setHgrow(field, Priority.ALWAYS);
mainBox.getChildren().addAll(box, myview,submit);
return mainBox;
}
private VBox firstScene(Stage stage){
//Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
ReadOnlyStringWrapper empIdText = new ReadOnlyStringWrapper("Employee ID");
ReadOnlyStringProperty readOnlyEmpId = empIdText.getReadOnlyProperty();
ReadOnlyStringWrapper passText = new ReadOnlyStringWrapper("Password");
ReadOnlyStringProperty readOnlyPassword = passText.getReadOnlyProperty();
ReadOnlyStringWrapper submitText = new ReadOnlyStringWrapper("Submit");
ReadOnlyStringProperty readOnlySubmit = submitText.getReadOnlyProperty();
//'employee ID' text
Label empIdfield = new Label();
empIdfield.textProperty().bind(readOnlyEmpId);
//Text Field to insert employee id
TextField employeeid = new TextField();
employeeid.setPromptText("Employee ID");
//'Password' text
Label passwordField = new Label();
passwordField.textProperty().bind(readOnlyPassword);
//Password Field to enter password
PasswordField password = new PasswordField();
password.setPromptText("Password");
//Submit button
Button submitBtn = new Button();
submitBtn.setPrefSize(120, 40);
submitBtn.textProperty().bind(readOnlySubmit);
//HBox for employee id
HBox empLayout = new HBox(5);
empLayout.setPadding(new Insets(10, 20, 10, 20));
empLayout.getChildren().addAll(empIdfield, employeeid);
empLayout.setLayoutX(12);
empLayout.setLayoutY(12);
HBox.setHgrow(empIdfield, Priority.ALWAYS);
HBox.setHgrow(employeeid, Priority.ALWAYS);
empLayout.setAlignment(Pos.CENTER);
//HBox for password field
HBox passwordLayout = new HBox(20);
passwordLayout.setPadding(new Insets(20, 20, 20, 20));
passwordLayout.getChildren().addAll(passwordField, password);
passwordLayout.setLayoutX(12);
passwordLayout.setLayoutY(12);
HBox.setHgrow(passwordField, Priority.ALWAYS);
HBox.setHgrow(password, Priority.ALWAYS);
passwordLayout.setAlignment(Pos.CENTER);
//Layout for the form
VBox formLayout = new VBox();
//Adding form fields to the layout
formLayout.getChildren().addAll(empLayout, passwordLayout, submitBtn);
formLayout.setLayoutX(12);
formLayout.setLayoutY(12);
formLayout.setAlignment(Pos.CENTER);
//login button event listener
submitBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
stage.setScene(scene2);
c = JDBCDemo.connect();
}
});
return formLayout;
}
请告诉我哪里弄错了。我是JAVAFX的新手