SignUp Form JavaFX with MySQL fails in intellij

huangapple go评论51阅读模式
英文:

SignUp Form JavaFX with MySQL fails in intellij

问题

这是您的JavaFX注册表单代码,以及您的MySQL表格和错误信息:

ea = new Stage();
AnchorPane pane = new AnchorPane();

TextField n = new TextField();
n.setPromptText("输入您的名字");
n.setLayoutY(150);
n.setLayoutX(140);
pane.getChildren().add(n);

TextField s = new TextField();
s.setPromptText("输入您的姓氏");
s.setLayoutY(200);
s.setLayoutX(140);
pane.getChildren().add(s);

TextField e = new TextField();
e.setPromptText("输入您的电子邮件");
e.setLayoutY(250);
e.setLayoutX(140);
pane.getChildren().add(e);

TextField u = new TextField();
u.setPromptText("输入您的用户名");
u.setLayoutY(300);
u.setLayoutX(140);
pane.getChildren().add(u);

TextField a = new TextField();
a.setPromptText("输入您的密码");
a.setLayoutY(350);
a.setLayoutX(140);
pane.getChildren().add(a);

Button b = new Button();
b.setText("保存");
b.setLayoutY(400);
b.setLayoutX(180);
b.setOnAction((ee)->{
    try
    {
        String fn = n.getText();
        String ln = s.getText();
        String em = e.getText();
        String un = u.getText();
        String ps = s.getText();
        PreparedStatement stmt = con.prepareStatement("INSERT INTO userdatabasetable VALUES (?,?,?,?,?,?)");
        stmt.setString(2, fn);
        stmt.setString(3, ln);
        stmt.setString(4, em);
        stmt.setString(5, un);
        stmt.setString(6, ps);
        stmt.execute();
        System.out.println("插入完成");
        stmt.close();
    }
    catch (SQLException er)
    {
        er.printStackTrace();
    }
});
pane.getChildren().add(b);

scene = new Scene(pane,1280,720);
ea.setScene(scene);
ea.setResizable(false);
ea.setTitle("汽车租赁系统");
ea.show();

这是您的SQL表格:

----------------------------------------------------------------------------------------------------------------------------------------
|     ID     |     FirstName     |     LastName     |                 Email                  |      UserName    |     PassWord     |
----------------------------------------------------------------------------------------------------------------------------------------
|     1       |         ahmed    |        ali       |            ahmed@gmail.com             |        ahmed99   |          1234       |
----------------------------------------------------------------------------------------------------------------------------------------

这是您收到的错误信息:

java.sql.SQLException: 参数1未指定值

如果您需要更多帮助来解决这个问题,请提供额外的信息。

英文:

I've made a javaFx Signup form that is connected with MySQL when I compile the code connection works fine but when I try to create a new user I get an error that does not allow me to import the details into the table my the error that I get is that No value specified for parameter 1 even though I already make the Id column in MySQL auto-increment bellow I wrote my code and share how my table looks like and finally the error that I get so can someone please help me fix this problem with all respect

        ea = new Stage();
AnchorPane pane = new AnchorPane();
TextField n = new TextField();
n.setPromptText("ENTER YOUR FIRST NAME");
n.setLayoutY(150);
n.setLayoutX(140);
pane.getChildren().add(n);
TextField s = new TextField();
s.setPromptText("ENTER YOU LAST NAME");
s.setLayoutY(200);
s.setLayoutX(140);
pane.getChildren().add(s);
TextField e = new TextField();
e.setPromptText("ENTER YOUR EMAIL");
e.setLayoutY(250);
e.setLayoutX(140);
pane.getChildren().add(e);
TextField u = new TextField();
u.setPromptText("ENTER YOUR USERNAME");
u.setLayoutY(300);
u.setLayoutX(140);
pane.getChildren().add(u);
TextField a = new TextField();
a.setPromptText("ENTER YOU PASSWORD");
a.setLayoutY(350);
a.setLayoutX(140);
pane.getChildren().add(a);
Button b = new Button();
b.setText("save");
b.setLayoutY(400);
b.setLayoutX(180);
b.setOnAction((ee)->{
// name = ErrorDetection.name(n.getText());
// age  = ErrorDetection.inNumber(a.getText());
try
{
String fn = n.getText();
String ln = s.getText();
String em = e.getText();
String un = u.getText();
String ps = s.getText();
PreparedStatement stmt = con.prepareStatement("INSERT INTO userdatabasetable VALUES (?,?,?,?,?,?)");
stmt.setString(2, fn);
stmt.setString(3, ln);
stmt.setString(4, em);
stmt.setString(5, un);
stmt.setString(6, ps);
stmt.execute();
System.out.println("Insertion Complete");
stmt.close();
}
catch (SQLException er)
{
er.printStackTrace();
}
});
pane.getChildren().add(b);
scene = new Scene(pane,1280,720);
//e.setOnCloseRequest(confirmCloseEventHandler);
ea.setScene(scene);
ea.setResizable(false);
//e.getIcons().add(new Image("commonPictures/cra.png"));
ea.setTitle("CAR RENTING SYSTEM");
ea.show();

This is how SQL table looks like

 ----------------------------------------------------------------------------------------------------------------------------------------
|     ID     |     FirstName     |     LastName     |                 Email                  |      UserName    |     PassWord     |
-------------------------------------------------------------------------------------------------------------------------------------
|     1       |         ahmed    |        ali       |            ahmed@gmail.com             |        ahmed99   |          1234       |
--------------------------------------------------------------------------------------------------------

AND this is the error that I get

"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" --module-path "C:\Program Files\Java\javafx-sdk-14.0.1\lib" --add-modules=javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -javaagent:C:\Users\YUSOF\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-1\202.6250.13\lib\idea_rt.jar=49308:C:\Users\YUSOF\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-1\202.6250.13\bin -Dfile.encoding=UTF-8 -classpath "P:\JAVA PROJECTS\CP III\JavaFX DataBase (JavaFX 8 Tutorial)\out\production\JavaFX DataBase (JavaFX 8 Tutorial);C:\Program Files\Java\javafx-sdk-14.0.1\lib\src.zip;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx-swt.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-14.0.1\lib\javafx.graphics.jar;P:\JAVA PROJECTS\CP III\mysql-connector-java-8.0.20.jar" sample.DataBaseProject1
DataBase Connected Successfully
java.sql.SQLException: No value specified for parameter 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:396)
at sample.GUIS.lambda$display$0(GUIS.java:83)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8885)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)

答案1

得分: 1

以下任何一种方法都可以工作:

PreparedStatement stmt = con.prepareStatement(
    "INSERT INTO userdatabasetable " +
    "(FirstName, LastName, Email, UserName, PassWord) " +
    "VALUES (?,?,?,?,?)");
stmt.setString(1, fn);
stmt.setString(2, ln);
stmt.setString(3, em);
stmt.setString(4, un);
stmt.setString(5, ps);

或者

PreparedStatement stmt = con.prepareStatement(
    "INSERT INTO userdatabasetable VALUES (?,?,?,?,?,?)");
stmt.setNull(1, Types.INTEGER);
stmt.setString(2, fn);
stmt.setString(3, ln);
stmt.setString(4, em);
stmt.setString(5, un);
stmt.setString(6, ps);

或者

PreparedStatement stmt = con.prepareStatement(
    "INSERT INTO userdatabasetable VALUES (NULL,?,?,?,?,?)");
stmt.setString(1, fn);
stmt.setString(2, ln);
stmt.setString(3, em);
stmt.setString(4, un);
stmt.setString(5, ps);
英文:

Any of the following should work:

PreparedStatement stmt = con.prepareStatement(
"INSERT INTO userdatabasetable "+
"(FirstName, LastName, Email, UserName, PassWord) "+
"VALUES (?,?,?,?,?)");
stmt.setString(1, fn);
stmt.setString(2, ln);
stmt.setString(3, em);
stmt.setString(4, un);
stmt.setString(5, ps);

or

PreparedStatement stmt = con.prepareStatement(
"INSERT INTO userdatabasetable VALUES (?,?,?,?,?,?)");
stmt.setNull(1, Types.INTEGER);
stmt.setString(2, fn);
stmt.setString(3, ln);
stmt.setString(4, em);
stmt.setString(5, un);
stmt.setString(6, ps);

or

PreparedStatement stmt = con.prepareStatement(
"INSERT INTO userdatabasetable VALUES (NULL,?,?,?,?,?)");
stmt.setString(1, fn);
stmt.setString(2, ln);
stmt.setString(3, em);
stmt.setString(4, un);
stmt.setString(5, ps);

huangapple
  • 本文由 发表于 2020年7月30日 04:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63161592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定