java數據庫(MySQL)之增刪改查


1.查詢數據

先救從簡單的來吧,之前我們實現了將數據庫表格信息讀取到一個List集合中,數據庫的查詢,實

際上就是對這個集合的查詢;

 1 public class Show {
 2     public static void main(String[] args) {
 3         //獲取之前的數據集合
 4         List<User> list = Do.userlist();
 5         //foreach遍歷這個集合
 6         for (User user : list) {
 7             //查詢條件,查找user為tom的用戶名和密碼
 8             if(user.user.equals("tom")) {
 9                 //這里要用equals()方法,不能用==,切記
10                 System.out.println("用戶名:"+user.user+";密碼:"+user.password);
11             }
12         }
13     }
14 }

控制台打印如下:

1 用戶名:tom;密碼:243523563

其實我們也可以將這個方法封裝起來,並傳入一個字符串參數,從而實現對用戶信息的輸出,在這里不做深究。

2.增加數據

在實現這個功能之前我們要事先了解一下,SQL語句是如何實現數據的添加的;

先來看一下表有哪些數據項

我們可以看到,這個表中有4個屬性,由於設置的時候,設置成了必填,所以,添加的時候也得全填

SQL語句:

INSERT INTO table1(id,user,password,age) VALUES ('5','cindy','35675467','23') ;

 1 public class Insert {
 2     // 利用含參構造器新建一個User對象,傳入數據
 3     static User newuser = new User(5, "cindy", "123452", 23);
 4     // 定義含參方法將這個數據(對象)添加到表格中
 5     public static void add(User newuser) {
 6         try {
 7             // 獲取數據庫的連接
 8             Connection conn = MySQL.getconnect();
 9             // 設置SQL規則,數據由於還不知道先用?代替
10             String sql = "INSERT INTO table1(id,user,password,age) VALUES (?,?,?,?)";
11             // 預處理sql語句
12             PreparedStatement presta = conn.prepareStatement(sql);
13             // 設置sql語句中的values值
14             presta.setInt(1, newuser.id);
15             presta.setString(2, newuser.user);
16             presta.setString(3, newuser.password);
17             presta.setInt(4, newuser.age);
18             // 執行SQL語句,實現數據添加
19             presta.execute();
20         } catch (SQLException e) {
21             e.printStackTrace();
22         }
23     }
24     public static void main(String[] args) {
25         // 執行add(newuser)方法
26         add(newuser);
27     }
28 }

我們看看結果:

添加成功,Bingo!

3.刪除數據

其實增刪改查,實際上就是對sql語句的運用,這里的刪除也不例外

 1 public static void delete(Integer id) {
 2         try {
 3             // 獲取數據庫的連接
 4             Connection conn = MySQL.getconnect();
 5             // 設置SQL規則,數據由於還不知道先用?代替
 6             //String sql = "INSERT INTO table1(id,user,password,age) VALUES (?,?,?,?)";
 7             //String sql = "Update table1 set user=?,password=?,age=? where id=?";
 8             String sql = "delete from table1 where id=?";
 9             // 預處理sql語句
10             PreparedStatement presta = conn.prepareStatement(sql);
11             // 設置sql語句中的values值
12             presta.setInt(1,id);
13             // 執行SQL語句,實現數據添加
14             presta.execute();
15         } catch (SQLException e) {
16             e.printStackTrace();
17         }
18     }
19     public static void main(String[] args) {
20         // 執行add(newuser)方法
21         delete(2);
22     }

delete()方法傳入了一個參數id,用於刪除指定id的數據,如下:

id為2的信息被刪除了,beautiful!

4.更新數據

 1 public class Insert {
 2     // 利用含參構造器新建一個User對象,傳入數據
 3     static User newuser = new User(5, "Cindy", "1234567890", 23);
 4     // 定義含參方法將這個數據(對象)添加到表格中
 5     public static void update(User newuser) {
 6         try {
 7             // 獲取數據庫的連接
 8             Connection conn = MySQL.getconnect();
 9             // 設置SQL規則,數據由於還不知道先用?代替
10             //String sql = "INSERT INTO table1(id,user,password,age) VALUES (?,?,?,?)";
11             String sql = "Update table1 set user=?,password=?,age=? where id=?";
12             // 預處理sql語句
13             PreparedStatement presta = conn.prepareStatement(sql);
14             // 設置sql語句中的values值
15             presta.setString(1, newuser.user);
16             presta.setString(2, newuser.password);
17             presta.setInt(3, newuser.age);
18             presta.setInt(4, newuser.id);
19             // 執行SQL語句,實現數據添加
20             presta.execute();
21         } catch (SQLException e) {
22             e.printStackTrace();
23         }
24     }
25     public static void main(String[] args) {
26         // 執行add(newuser)方法
27         update(newuser);
28     }
29 }

這里是根據ID號來更新信息,結果如下

姓名、密碼被成功更改,nice!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM