Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果其中一列是使用prepared语句自动递增的,如何在mysql数据库中插入值_Java_Mysql_Jdbc_Prepared Statement_Sqlexception - Fatal编程技术网

Java 如果其中一列是使用prepared语句自动递增的,如何在mysql数据库中插入值

Java 如果其中一列是使用prepared语句自动递增的,如何在mysql数据库中插入值,java,mysql,jdbc,prepared-statement,sqlexception,Java,Mysql,Jdbc,Prepared Statement,Sqlexception,我得到一个例外: public String put(){ this.query = "INSERT INTO user_registration VALUES('default', '?' , '?' , '?' , '?' , '?' );"; try { PreparedStatement stmt= Main.connection.prepareStatement(query); stmt.setString

我得到一个例外:

public String put(){
         this.query = "INSERT INTO user_registration VALUES('default', '?' , '?' , '?' , '?' , '?' );";

    try {
        PreparedStatement stmt= Main.connection.prepareStatement(query);
        stmt.setString(1,this.fullname);
        stmt.setString(2,this.username);
        stmt.setString(3,this.password);
        stmt.setString(4,this.email);
        stmt.setString(5,this.contact);
        stmt.executeUpdate();
        return String.valueOf(SignupStatus.SUCCESS);
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println(e);
        return String.valueOf(SignupStatus.FAILED);
    }
}

请帮我更正代码。

占位符不能用单引号括起来。
此外,最好在语句中包含将接收每个提供值的列名。
如果第1列是自动递增列,则应将其从列表中忽略,因为其值将由MySql提供:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    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.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1372)
    at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1385)
    at com.mysql.cj.jdbc.ClientPreparedStatement.setString(ClientPreparedStatement.java:1752)
    at client_sharenow.Signup.put(Signup.java:29)
    at client_sharenow.Client_Request.run(Client_Request.java:40)
    at java.base/java.lang.Thread.run(Thread.java:832)
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

将列名更改为实际列名。

还应注意,添加
'
会混淆PreparedStatement,这会使它认为您实际上打算在该列中插入“?”,但现在我得到了一个不同的例外:(该用户名不能为空。你能帮我吗?如果你将列用户名定义为
非空
,你必须为此
提供一个非空值。用户名
@alupi07如果问题解决了,请不要忘了点击复选标记接受答案。我相信问号(参数)在SQL字符串中,不应在其旁边加引号。“?”将被视为一个值而不是占位符。因此请尝试删除“”。
this.query = "INSERT INTO user_registration(fullname, username, password, email, contact) VALUES (?, ?, ?, ?, ?);";