網站首頁 編程語言 正文
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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-06-16 Python中ArcPy柵格裁剪柵格(批量對齊柵格圖像范圍并統一行數與列數)_python
- 2023-01-04 Opencv實現計算兩條直線或線段角度方法詳解_python
- 2023-03-26 詳解C/C++中低耦合代碼的設計實現_C 語言
- 2022-03-15 nginx 請求的時候 500錯誤 failed (13: Permission denied)
- 2022-10-03 Tomcat安裝使用及部署Web項目的3種方法匯總_Tomcat
- 2022-07-30 Python開發時報TypeError:?‘int‘?object?is?not?iterable錯
- 2022-08-27 Qt實現一個簡單的word文檔編輯器_C 語言
- 2022-04-25 Entity?Framework?Core實現軟刪除與查詢過濾器_實用技巧
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支