1. 程式人生 > >JDBC-java資料庫連線

JDBC-java資料庫連線

java資料庫連線體系結構是用於Java應用程式連線資料庫的標準方法。JDBC對Java程式設計師而言是API,對實現與資料庫連線的服務提供商而言是介面模型。作為API,JDBC為程式開發提供標準的介面,併為資料庫廠商及第三方中介軟體廠商實現與資料庫的連線提供了標準方法。

  1. 大多數JDBC驅動程式都支援事務;
  2. 與資料庫建立連線的標準方法是呼叫DriverManager.getConnection方法;
  3. JDBC提供了三個類,用於向資料庫傳送SQL語句。Connection介面中的三個方法可用於建立這些類的例項:
    (1)Statement:由方法createStatement所建立。Statement物件用於傳送簡單的SQL語句。
    (2)PreparedStatement:由方法prepareStatement所建立。PreparedStatement物件有可能比Statement物件的效率更高,因為它已被預編譯過並存放在那以供將來使用。
    (3)CallableStatement:由方法prepareCall所建立。

jdbc連線資料庫並執行SQL語句進行crud例項:

涉及到的包:都是SQL的:驅動管理,連線物件、處理物件、結果集
DriverManager:依據資料庫的不同,管理JDBC驅動;
Connection:負責連線資料庫並擔任傳送資料的任務
Statement:負責執行SQL語句;
ResultSet:負責儲存Statement執行後所產生的查詢結果

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

executeUpdate執行insert、delete、update的SQL操作,此處之舉刪除SQL為例,對應插入、修改和刪除一樣,只需將sql改成對應的插入、修改資料庫語言即可

 void delete() {
        // TODO Auto-generated method stub
        String url = "jdbc:mysql://192.168.1.45:3306/employ";
        String uerName = "root";
        String password = "[email protected]";
        // 1.註冊驅動
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Connection conn = null;
        Statement stmt = null;
        try {
            // 2.獲得連線物件
            conn = DriverManager.getConnection(url, uerName, password);
            // 3.獲得處理物件
            stmt = conn.createStatement();

            // 4.進行crud
            String sql = "DELETE FROM  emp WHERE emp_no=99";// 刪除SQL
            /**
             * executeUpdate執行增刪改,操作資料的SQL;executeQuery執行查詢語句
             */
       		  int lines = stmt.executeUpdate(sql);// 返回操作影響的資料庫行數
      		  System.out.println("影響行數:" + lines);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 5.釋放資源
            try {
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

executeQuery執行select查詢語句,返回結果集ResultSet,比executeUpdate多一個結果集處理

 private static void query() {
        // TODO Auto-generated method stub
        String url = "jdbc:mysql://192.168.1.45:3306/employ";
        String uerName = "root";
        String password = "[email protected]";
        // 1.載入驅動
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 2.獲得連線物件
            conn = DriverManager.getConnection(url, uerName, password);
            // 3.獲得處理物件
            stmt = conn.createStatement();

            // 4.進行crud
            String sql = "select * FROM emp";// 查詢SQL
            /**
             * executeUpdate執行增刪改,操作資料的SQL;executeQuery執行查詢語句
             */
            rs = stmt.executeQuery(sql);
            // 5. 解析結果集
            while (rs.next()) {// 判斷遊標下移是否有資料
                int empNo = rs.getInt("emp_no");
                // 資料庫下標從1開始rs.getInt(1)
                String empName = rs.getString("emp_name");
                boolean sex = rs.getBoolean("sex");
                double salary = rs.getDouble("salary");
                String birthDay = rs.getString("birthday");
                Emp emp = new Emp(empNo, empName, salary, sex, birthDay);
                System.out.println(emp);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 6.釋放資源

            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
程式碼涉及到的庫表:在這裡插入圖片描述
emp物件
public class Emp {

    private Integer empNo;

    private String empName;

    private double salary;

    private boolean sex;

    private String birthday;

    public Emp(Integer empNo, String empName, double salary, boolean sex, String birthday) {
        this.empNo = empNo;
        this.empName = empName;
        this.salary = salary;
        this.sex = sex;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Emp [ 姓名:" + empName + ", 員工號:" + empNo + ", 薪資:" + salary + ", 性別:" + (sex ? "男" : "女") + ", 生日:" + birthday + "]";
    }
}