--- title: JAVA程式筆記-JDBC-增刪改查 tags: JAVA程式筆記 --- 完成了JDBC-連接的connect (重點)MySQL 8的URL要serverTimezone=UTC參數。+我有去看我的MYSQL在INTELLIJ是否有下載~https://hackmd.io/@sSA6NW2ZToCxgSaVETWNCg/BJLolkys8 來到~重要Connection 1.要先看看自己的資料庫裡面的表裏面有甚麼 ![](https://i.imgur.com/MdA9jw3.jpg) 2.新增數據庫連接的程式碼-新增語句對象 ResultSet resultSet=statement.executeQuery("")<-裡面的sql是INTELLIJ自己跳出來的,只有打""(看別人的資料時) ![](https://i.imgur.com/NgO0flD.jpg) String sql="select userid,name from user";這個打的方法是從底打回來的 ![](https://i.imgur.com/XKdbqLR.jpg) 變數結果集->為TRUE找到數據 ![](https://i.imgur.com/lKCtrdK.jpg) 程式碼: https://gist.github.com/jzs2home/0d955c5f91eb8e45675af06dfff7b520 --- 回到CMD的資料庫select*from user;-要在裡面加人名,不然會沒東西 ![](https://i.imgur.com/wUIk7om.jpg) 這裡建的方法我要找WORKBENCH來幫忙(跟CMD之間不會衝突都可以用) https://hackmd.io/8PJeTM7WQAW8GYv-xp5JXQ ![](https://i.imgur.com/TYIy4uB.jpg) 想要建的表 ![](https://i.imgur.com/LR8dU5C.jpg) 找WORKBENCH來幫忙新增 如果要改TYPE裡的varchart(20)=限定20個字+int(11),NULL是YES 和NULL的內容只能刪除重建-not null 是不能空.沒打就能空(沒有名子也可以) ![](https://i.imgur.com/WuT6b28.jpg) 語法參考https://ithelp.ithome.com.tw/articles/10216290 還有我的上一篇https://hackmd.io/qpamKhSlQmqFwcSpGf9ARA ![](https://i.imgur.com/9Sfqaku.jpg) 要在裡面加入人名要用insert語句 ![](https://i.imgur.com/kKr7Kro.jpg) 再回到intellij 跑一下MAIN 就有資料了喔~ ![](https://i.imgur.com/x5BpNhN.jpg) 程式碼:https://gist.github.com/jzs2home/c847e60fa6290dc2061cf65165402c52 --- CRUD=增刪改查 先用CMD來查(有7步驟,程式碼排的順序也要改變,不然會無限迴圈=找不到=失敗)-改到前面=獲得數據庫連接+加上靜態代碼塊static static String url="jdbc:mysql://localhost:3306/MyDB?serverTimezone=UTC"; static String user="root"; static String password="密碼"; ---加上靜態代碼塊static{}也是往上移 try { Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("加載驅動程式成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("加載驅動程式失敗"); return; } ---原來的 Statement statement=connection.createStatement();要改掉 Connection connection = DriverManager.getConnection(url, user, password); PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) 無條件查詢-數字最大的userid(可以確認自己的資料庫是沒有問題的) ![](https://i.imgur.com/lZzN9Fl.jpg) 然後再拿到JAVA代碼中測試,還要給一個變數maxId ![](https://i.imgur.com/ehbx6jd.jpg) 程式碼:https://gist.github.com/jzs2home/4697bd621e8a9b6e557acf5edea95647 --- 有條件查詢-查userid>0(可以確認自己的資料庫是沒有問題的) ![](https://i.imgur.com/nOilpkq.jpg) select name,userid from user where userid>0 order by userid; 的0要改成用英文輸入法的? 然後再拿到JAVA代碼中測試,還要變換語法=語法不固定....QQ preparedStatement.setInt(1,0);綁定參數順序=從1開始, 要有read();才會有RUN出東西~不然只會在無條件查詢結果 ![](https://i.imgur.com/bzgSo5R.jpg) 程式碼:https://gist.github.com/jzs2home/ea28a4406f8b0358a620d48d7fe1df7a --- 數據修改操作 ![](https://i.imgur.com/dJ2pia6.jpg) 插入(可以確認自己的資料庫是沒有問題的)=create(); ![](https://i.imgur.com/ZgBaPf5.jpg) insert into user(userid,name) values (3,'huangtzutzu');的(3,'huangtzutzu')在JAVA代碼中變(?,?)然後語法也跟樓上不同~QQ ![](https://i.imgur.com/flaGLNG.jpg) 然後ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.printf("name:%s id:%d \n",resultSet. getString("name"), resultSet.getInt("userid")); } 通通不需要~@@ ![](https://i.imgur.com/BYPNRFI.jpg) 改後的程式碼:https://gist.github.com/jzs2home/5b95c3444c77a3d516d1bd2699a3627d 給一個變數maxUserId除了可以查出原來的數值也加入了~ 然後再用CMD看一下 ![](https://i.imgur.com/I9PBK7M.jpg) --- 更新 update(); ![](https://i.imgur.com/HpmxSsA.jpg) 確認自己的資料庫是沒有問題的= update(); ![](https://i.imgur.com/xPMARMB.jpg) 再拿到JAVA代碼中測試 update user set name='littletzu' where userid>3;又要改成? update user set name=? where userid>?; ![](https://i.imgur.com/z0bGhdz.jpg) 成功更新2數據.=2個 ![](https://i.imgur.com/eRqawK9.jpg) 程式碼:https://gist.github.com/jzs2home/7fab1d02fab41bfcea13f70135f12c2f --- 刪除-要刪除最大的userid 確認自己的資料庫是沒有問題的=delete ![](https://i.imgur.com/v25JjpN.jpg) 再拿到JAVA代碼中測試delete from user where userid=5;的5也是改成delete from user where userid=?; ![](https://i.imgur.com/4zBc02x.jpg) 程式碼:https://gist.github.com/jzs2home/f259161a92400484bcd0c2a21da4dd31 --- 看起來沒差別 ![](https://i.imgur.com/fNYMTrC.jpg) 是因為read(); create(); update(); delete(); 全開~把 create(); update();關了=//就會發現不同 ![](https://i.imgur.com/HqMjUBy.jpg) 聽說JDBC還有模板設計模式:https://hackmd.io/@sSA6NW2ZToCxgSaVETWNCg/HJ3V79q38