这就是错误。
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml@19-ea/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml@19-ea/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base@19-ea/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base@19-ea/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@19-ea/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@19-ea/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@19-ea/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base@19-ea/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@19-ea/javafx.scene.Node.fireEvent(Node.java:8797)
at javafx.controls@19-ea/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls@19-ea/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls@19-ea/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base@19-ea/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base@19-ea/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base@19-ea/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base@19-ea/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base@19-ea/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@19-ea/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@19-ea/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@19-ea/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base@19-ea/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@19-ea/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
at javafx.graphics@19-ea/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics@19-ea/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
at javafx.graphics@19-ea/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics@19-ea/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@19-ea/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics@19-ea/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics@19-ea/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics@19-ea/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics@19-ea/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics@19-ea/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@19-ea/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at javafx.base@19-ea/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml@19-ea/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml@19-ea/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
... 46 more
Caused by: java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:675)
at java.base/java.lang.Integer.valueOf(Integer.java:992)
at com.example.studentdatabase/com.example.studentdatabase.Register.register(Register.java:99)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 53 more
这些是我的类和FXMLMain
类-
package com.example.studentdatabase;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Main extends Application {
InputStream stream = new FileInputStream("C:\\Users\\dinira francisco\\Desktop\\DDSlogo.jpg");
Image image = new Image(stream);
public Main() throws FileNotFoundException {
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("Register.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Student Database 2023");
stage.getIcons().add(image);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
// type here
Register
类-
package com.example.studentdatabase;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;
public class Register {
private int studentID=0;
private boolean hasFirstName = false;
private boolean hasLastName = false;
private boolean hasAge = false;
private boolean hasGrade = false;
@FXML
private TextField GradeTF;
@FXML
private Button ageSubmit;
@FXML
private TextField ageTF;
@FXML
private TextField firstNameTF;
@FXML
private Button firstSubmit;
@FXML
private Button gradeSubmit;
@FXML
private TextField lastNameTF;
@FXML
private Button lastsubmit;
@FXML
private Button registerButton;
@FXML
private Label warning;
public void getFirstName(ActionEvent event){
if(firstNameTF.getText().length()>=1){
hasFirstName=true;
firstNameTF.setDisable(true);
}
}
public void getLastName(ActionEvent event){
if(lastNameTF.getText().length()>=1){
hasLastName=true;
lastNameTF.setDisable(true);
}
}
public void getAge(ActionEvent event){
if(ageTF.getText().length()>=1){
hasAge=true;
ageTF.setDisable(true);
}
}
public void getGrade(ActionEvent event){
if(GradeTF.getText().length()>=1){
hasGrade=true;
GradeTF.setDisable(true);
}
}
public void register(ActionEvent event){
if(hasFirstName && hasLastName && hasAge && hasGrade){
studentID++;
warning.setText("Registration Completed!");
warning.setVisible(true);
firstNameTF.setText("");
lastNameTF.setText("");
ageTF.setText("");
GradeTF.setText("");
firstNameTF.setDisable(false);
lastNameTF.setDisable(false);
ageTF.setDisable(false);
GradeTF.setDisable(false);
hasFirstName=false;
hasLastName = false;
hasAge = false;
hasGrade = false;
DatabaseConnection databaseConnection = new DatabaseConnection();
Connection connection = databaseConnection.getConnection();
String registerString = "INSERT INTO student_data(student_id,first_name,last_name,age,grade) VALUES('"+studentID+"','"+firstNameTF.getText()+"','"+lastNameTF.getText()+"','"+Integer.valueOf(ageTF.getText())+"','"+Integer.valueOf(GradeTF.getText())+"')";
try{
Statement statement = connection.createStatement();
statement.executeUpdate(registerString);
}catch(Exception e){
e.printStackTrace();
e.getCause();
}
}else{
warning.setText("Fields are Empty!");
warning.setVisible(true);
}
}
}
数据库连接类-
package com.example.studentdatabase;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
public Connection databaseLink;
public Connection getConnection(){
String databaseName = "student_db";
String databaseUser = "root";
String databasePassword = "***";
String url = "jdbc:mysql://localhost/"+databaseName;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
databaseLink = DriverManager.getConnection(url,databaseUser,databasePassword);
}catch (Exception e){
e.printStackTrace();
e.getCause();
}
return databaseLink;
}
}
和FXML-
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="443.0" prefWidth="458.0" style="-fx-background-color: #f0db24;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.studentdatabase.Register">
<children>
<Label layoutX="161.0" layoutY="25.0" text="Student Database">
<font>
<Font size="17.0" />
</font>
</Label>
<Label layoutX="95.0" layoutY="52.0" text="Sinhala Oratory & Debating Society">
<font>
<Font size="17.0" />
</font>
</Label>
<Label layoutX="106.0" layoutY="78.0" text="D.S.Senanayake College - Col. 08">
<font>
<Font size="17.0" />
</font>
</Label>
<Label layoutX="161.0" layoutY="125.0" text="Registration Form">
<font>
<Font size="17.0" />
</font>
</Label>
<Label layoutX="66.0" layoutY="177.0" text="First Name ">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="67.0" layoutY="226.0" text="Last Name ">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="66.0" layoutY="269.0" text="Age">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="66.0" layoutY="315.0" text="Grade">
<font>
<Font size="15.0" />
</font>
</Label>
<TextField fx:id="firstNameTF" layoutX="154.0" layoutY="175.0" />
<TextField fx:id="lastNameTF" layoutX="154.0" layoutY="224.0" />
<TextField fx:id="ageTF" layoutX="154.0" layoutY="267.0" />
<TextField fx:id="GradeTF" layoutX="154.0" layoutY="313.0" />
<Button fx:id="firstSubmit" layoutX="318.0" layoutY="175.0" mnemonicParsing="false" onAction="#getFirstName" text="Submit" />
<Button fx:id="gradeSubmit" layoutX="318.0" layoutY="313.0" mnemonicParsing="false" onAction="#getGrade" text="Submit" />
<Button fx:id="ageSubmit" layoutX="318.0" layoutY="267.0" mnemonicParsing="false" onAction="#getAge" text="Submit" />
<Button fx:id="lastsubmit" layoutX="318.0" layoutY="224.0" mnemonicParsing="false" onAction="#getLastName" text="Submit" />
<Button fx:id="registerButton" layoutX="192.0" layoutY="358.0" mnemonicParsing="false" onAction="#register" text="Register" />
<Label fx:id="warning" layoutX="175.0" layoutY="399.0" text="Fields are Empty!" textFill="#f70909" visible="false" />
</children>
</AnchorPane>
我在stackoverflow中搜索了一些解决方案,但无法解决这个问题。
1条答案
按热度按时间8hhllhi21#
首先是关于代码的一些注解。
类
DatabaseConnection
这是no longer needed。
因为方法
getConnection
中的catch
块,该方法可能返回null,这意味着调用方法getConnection
的其他方法需要检查是否返回null。在我看来,最好声明方法getConnection
抛出SQLException
。此外,类DatabaseConnection
可能应该是singleton,这意味着方法getConnection
应该是静态方法。类
Register
我认为您应该使用PreparedStatement而不是
Statement
。您还应该使用try-with-resources
我看不出这句台词的必要性:
方法
printStackTrace
还将打印原因-如果有原因的话。不要使用变量
hasFirstName
、hasLastName
、hasAge
和hasGrade
,而是使用可以在方法initialize
中设置的绑定,因为类Register
是FXML控制器。这样,注册表单中的每个TextField
都不需要一个 * Submit * 按钮。数据输入表单通常不需要单独提交每个字段。与其显式设置每个
Node
的大小和位置,不如使用layouts。关于顶部的
Label
,您可以使用单个Label
,而不是每行文本都使用单独的Label
。您可以插入line breaks,也可以居中对齐文本。现在来看看你的问题,你需要在更新数据库之后"清除"注册表(而不是之前,就像你现在在代码中所做的那样)。
这是我的申请版本。
等级
DatabaseConnection
等级
Register
注意,它使用text blocks,也使用[JavaFX] alerts。
FXML文件
类
Main
未更改,因此被省略。