網站首頁 編程語言 正文
01 異常發生場景
-
當我使用PreparedStatement 類進行數據庫連接時,彈出錯誤
Cause: java.sql.SQLSyntaxErrorException: 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 '? , studentNo=? , age=? , classId=? , phone=? , remark=? , sex=?' at line 1 ? //以下是代碼部分 try { // ? ? ? ? ? 1.注冊驅動 ? ? ? ? ? ?Class.forName("com.mysql.cj.jdbc.Driver"); // ? ? ? ? ? 2.獲取連接對象,連接數據庫 ? ? ? ? ? ?connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/text012?userSSL=false&serverTimezone=Asia/Shanghai", "root", "1234"); // ? ? ? ? ? 4.執行sql語句,返回對象 ? ? ? ? ? ?String sql="insert into student (name, studentNo , age , classId , phone , remark , sex) values (?,?,?,?,?,?,?);"; ? ? ? ? ? ?stmt=connection.prepareStatement(sql); ? ? ? ? ? ?stmt.setString(1,name); ? ? ? ? ? ?stmt.setString(2,studentNo); ? ? ? ? ? ?stmt.setInt(3,age); ? ? ? ? ? ?stmt.setInt(4,classId); ? ? ? ? ? ?stmt.setInt(5,phone); ? ? ? ? ? ?stmt.setString(6,remark); ? ? ? ? ? ?stmt.setString(7,sex); ? ? ? ? ? ? ?System.out.println(sql); // ? ? ? ? ? 3.獲得查詢執行者 ? ? ? ? ? ?int num=0; ? ? ? ? ? ?num=stmt.executeUpdate(sql); ? ? ? ? ? ?System.out.println(num); // ? ? ? ? ? 5.解析對象,獲得數據庫的數據 ? ? ? ? ? ?if (num!=0){ ? ? ? ? ? ? ? ?flag=true; ? ? ? ? ? } ? ? ? ? }catch (Exception e){ ? ? ? ? ? ?e.printStackTrace(); ? ? ? }finally { ? ? ? ? ? ?if (connection!=null){ ? ? ? ? ? ? ? ?connection.close(); ? ? ? ? ? } ? ? ? ? ? ?if (rs!=null){ ? ? ? ? ? ? ? ?rs.close(); ? ? ? ? ? } ? ? ? ? ? ?if (stmt!=null){ ? ? ? ? ? ? ? ?stmt.close(); ? ? ? ? ? } ? ?
02 異常的產生原因
-
num=stmt.executeUpdate(sql);部分不需要再次傳入sql
-
executeUpdate:執行數據庫的更新、插入和刪除操作 ,返回改變記錄的行數,但是使用PreparedStatement 類對傳入數據進行加密時,在執行到這個語句的時候數據庫系統會對()中sql 的語句進行預編譯的處理
03 解決方式
-
放棄使用PreparedStatement 類,或者更簡單的
-
num=stmt.executeUpdate();部分不傳入字符串
原文鏈接:https://blog.csdn.net/2302_77182979/article/details/132650047
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-06 go語言限制協程并發數的方案詳情_Golang
- 2022-12-05 TensorFlow中關于tf.app.flags命令行參數解析模塊_python
- 2022-09-27 Android開發EditText實現密碼顯示隱藏_Android
- 2022-08-18 C/C++?extern和static的使用詳解_C 語言
- 2021-12-14 常用時間處理方法
- 2022-12-05 一文教你如何優雅處理Golang中的異常_Golang
- 2023-03-13 React中如何設置多個className_React
- 2023-01-30 Python中String模塊示例詳解_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支