增刪改查-java(新手)


 PreparedStatement:

方法:

 

 


Connection:

方法:

 

 
        

 實例:


 

1、查詢:

 1 package cn.chuang.JdbcDome;
 2 
 3 import java.sql.*;
 4 
 5 public class JdbcDome3 {
 6     public static void main(String[] args) throws Exception {
 7         PreparedStatement ppst = null;
 8         Connection conn = null;
 9         fun3(ppst,conn);
10     }
11 
12     public static void fun1(PreparedStatement ppst,Connection conn) throws Exception {
13         //查詢表的內容
14         //1 注冊驅動 獲得Connection
15         Class.forName("com.mysql.jdbc.Driver");
16         //2 獲得鏈接
17         conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root");
18         //3 sql語句
19         String sql = "select * from lll";
20         //4 獲得執行sql語句的對象
21         ppst = conn.prepareStatement(sql);
22         ResultSet rs = ppst.executeQuery(sql);
23         //5 讓游標向下移動一行
24         rs.next();
25         int i = rs.getInt(1);
26         String name = rs.getString("ename");
27         //6 獲取數據
28         System.out.println(i+"  "+name);
29     }
30    

 


 

2、添加

 1     public static void fun2(PreparedStatement ppst,Connection conn) throws Exception {
 2         //在表中添加數據,表結構有多少就要寫多少。不能漏寫,會報錯。
 3         try {
 4             //1 注冊驅動 獲得Connection
 5             Class.forName("com.mysql.jdbc.Driver");
 6             //2 獲得鏈接
 7             conn = DriverManager.getConnection("Jdbc:mysql:///semployee", "root", "root");
 8 
 9             //3 sql語句
10             String sql = "insert into lll values (null,'兀立扗'),(null,'吳詩意')";
11             //4 獲得執行sql語句的對象
12             ppst = conn.prepareStatement(sql);
13             int i = ppst.executeUpdate(sql);
14             //5 處理結果
15             System.out.println(i);
16             //6 另創建if語句,做提示用。
17             if (i>0){
18                 System.out.println("添加成功");
19             }else{
20                 System.out.println("添加失敗");
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }finally {
25             if(ppst!=null){
26                 try {
27                     ppst.close();
28                 } catch (SQLException e) {
29                     e.printStackTrace();
30                 }
31             }
32             if (conn!=null){
33                 try {
34                     conn.close();
35                 } catch (SQLException e) {
36                     e.printStackTrace();
37                 }
38             }
39         }
40     }
41    

 


 

3、刪除 

 public static void fun3(PreparedStatement ppst,Connection conn) throws Exception {
        //刪除表內數據。
        //1 注冊驅動 獲得Connection
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root");
        //2 sql語句
        String sql = "delete from lll where uid = 2";
        //3 獲得執行sql語句的對象Statement
        ppst = conn.prepareStatement(sql);
        int i = ppst.executeUpdate(sql);
        //4 處理結果
        System.out.println(i);

        if (i>0){
            System.out.println("刪除成功");
            ppst.close();
            conn.close();
        }else{
            System.out.println("刪除失敗");
        }
    }

 


 4、修改

 1 public static void fun4(PreparedStatement ppst,Connection conn) throws Exception {
      //修改表內數據
2 //1 注冊驅動。 3 Class.forName("com.mysql.jdbc.Driver"); 4 //2 鏈接數據庫。 5 conn = DriverManager.getConnection("jdbc:mysql:///semployee", "root", "root"); 6 7 //3 SQL語句。 8 String sql = "update lll set uname = '吳惆' where uid = 1 "; 9 10 //4 獲得執行SQL的語句。 11 ppst = conn.prepareStatement(sql); 12 //5 處理結果。 13 int ou = ppst.executeUpdate(sql); 14 System.out.println(ou); 15 } 16 }

 


免責聲明!

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



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