gpt4 book ai didi

JAVA JDBC mySql 预处理语句更新查询

转载 作者:行者123 更新时间:2023-11-29 07:24:27 26 4
gpt4 key购买 nike

<分区>

我正在尝试执行以下代码

package jdbclesson;

import java.sql.*;

public class PreparedQuery {

public static void main(String[] args) throws Exception
{
String url = "jdbc:mysql://localhost:3306/alien?useSSL=false";
String uname = "root";
String pass = "ma123";
String query = "UPDATE student SET username= ? where userid= ? ";
PreparedStatement stmt = null;


Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, uname, pass);
stmt = con.prepareStatement(query);

stmt.setString(1, "tina");
stmt.setInt(2, 6);


int rs = stmt.executeUpdate(query);

System.out.println(rs);
stmt.close();
con.close();

}

}

但出现以下错误

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? where userid=?' at line 1

我的数据库只有 1 个表 student 有 2 列 userid 和 username 以及 10 行我缺少的内容

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com