JAVA JDBC mySql Prepared statement Update query(JAVA JDBC mySql Prepared statement 更新查询)
问题描述
我正在尝试执行以下代码
I am trying to execute the following code
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();
}
}
但出现以下错误
线程main"中的异常com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个SQL 语法错误;检查与您对应的手册MySQL 服务器版本,以便在 '? 附近使用正确的语法在哪里用户 ID=?在第 1 行
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 列用户 ID 和用户名以及 10 行我缺少什么
My database has only 1 table student with 2 columns userid and username and 10 rows what m i missing
推荐答案
试试:
int rs = stmt.executeUpdate();
代替:
int rs = stmt.executeUpdate(query);
executeUpdate() 运行准备好的语句的查询,这是您想要的.executeUpdate(query) 运行传递给该方法的查询.您收到错误是因为您传递了带有错误的 SQL(包含?).
executeUpdate() runs the query of the prepared statement, which is what you want. executeUpdate(query) runs the query passed to the method. You were getting the error because you were passing an SQL with errors (contains ?).
这篇关于JAVA JDBC mySql Prepared statement 更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAVA JDBC mySql Prepared statement 更新查询
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01