1. 程式人生 > 實用技巧 >C++入門 -- 迭代器及型別推導

C++入門 -- 迭代器及型別推導

一、JDBC是什麼

JDBC:java database connectivity

二、JDBC本質

JDBC 是sun公司提供的一套介面
介面都有呼叫者和實現者,面向介面呼叫,面向介面寫實現類,都屬於面向介面程式設計

為什麼要面向介面程式設計:
    為了解耦合,降低程式的耦合度,提高程式的擴充套件力。
    比如:多型就是典型的面向抽象程式設計
    Animal a = new Dog();

三、JDBC開發前的準備

先從對應的官網中下載對應的驅動jar包,配置到IDEA中

配置的截圖

四、JDBC開發六步

1.註冊驅動:告訴Java程式將要連線的是哪個品牌的資料庫
2.獲取連線:表示jvm和資料庫之間的程序通道打開了,這屬於程序之間的通訊,使用之後一定要關閉
3.獲取資料庫操作物件(執行sql語句的物件)
4.執行sql語句
5.處理查詢結果集:只有第四步執行的是select語句時才有第五步
6.釋放資源:java程式和資料庫是程序之間的通訊,使用之後一定要關閉

// 示例1:sql語句是insert,所有沒有上述第五步處理查詢結果集
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest01 {
    public static void main(String[] args) {
        

            //1.註冊驅動,這裡目的要把class檔案載入一下,後續還有另外一種方式實現
        java.sql.Driver driver = null;
        // 寫在這裡是因為下邊finally要用到,所以宣告為全域性的
        Connection con = null;
        Statement state = null;
        try {
            driver = new Driver(); //多型,父型別引用指向子類物件;java.sql.Driver是介面,jdbc中的Driver是實現類
            DriverManager.registerDriver(driver);
            //2.獲取連線
            String url = "jdbc:mysql://127.0.0.1/firstbase";
            String userName = "root";
            String password = "123456";
            con = DriverManager.getConnection(url,userName,password);
            System.out.println("資料庫連線物件是:" + con);
            //3.獲取資料庫操作物件(專門執行sql語句的物件)
            state = con.createStatement();
            //4.執行sql語句
            String sql = "insert into dept(deptno, dname, loc) values(60, 'SALES', 'NEWYORK')";
            int count = state.executeUpdate(sql);
            System.out.println(count == 1 ? "儲存成功" : "儲存失敗");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.釋放資源,為了保證資源一定釋放,在finally語句中關閉資源,並且遵循從小到大依次關閉,分別try catch
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null){
                try{
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                }
            }
        }
    }
}

// 示例2:delete 和 update
import java.sql.*;

public class JdbcTest02 {
    public static void main(String[] args) {

        Driver driver = null;
        Connection con = null;
        Statement state = null;

        try {
            //1.註冊驅動
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //2.獲取連線
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/firstbase","root","123456");
            //3.獲取資料庫執行物件
            state = con.createStatement();
            //4.執行sql語句;jdbc中sql語句不需要寫分號
//            String sql = "delete from dept where deptno = 50 ";   //刪除語句
            String sql = "update dept set dname = 'zhangsan' where deptno = 10 "; // 修改語句
            int count = state.executeUpdate(sql);
            System.out.println(count == 1 ? "刪除成功" : "刪除失敗");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

//示例3:註冊驅動的另一種常用的寫法
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcTest03 {
    public static void main(String[] args) {
        //1.註冊驅動的另外一種方式(比較常用)
        try {
            //這裡不需要接收返回值,因為只需要這個載入的動作,這個在原始碼中有靜態程式碼塊,只需要載入就執行了
            //這種方式常用是因為接收的是一個字串,可以寫到配置檔案.properities中
            Class.forName("com.mysql.jdbc.Driver");
        //2.獲取連線
            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/firstbase","root","123456");
            System.out.println(con); //這裡打印出con物件,表示獲取連線成功
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
//例項4:使用資源繫結器繫結屬性檔案
import java.util.ResourceBundle;
import java.sql.*;

public class JdbcTest04 {
    public static void main(String[] args) {
        //使用資源繫結器繫結屬性配置檔案
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String jdbc = bundle.getString("jdc");
        String url = bundle.getString("url");
        String userName = bundle.getString("userName");
        String password = bundle.getString("password");

        Connection con = null;
        Statement state = null;
        try {
            //1.註冊驅動
            Class.forName(jdbc);
            //2.獲取連線
            con = DriverManager.getConnection(url,userName,password);
            //3.獲取資料庫操作物件
            state = con.createStatement();
            //4.執行sql語句
            String sql = "insert into dept(deptno, dname, loc) values(60, 'SALES', 'NEWYORK')";
            int count = state.executeUpdate(sql);
            System.out.println(count == 1 ? "儲存成功" : "儲存失敗");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}