电竞比分网-中国电竞赛事及体育赛事平台

分享

jdbc常用接口介紹

 liang1234_ 2019-05-19

1.什么是JDBC

  -1.JDBC(Java Database Connection)為Java開發(fā)者使用數(shù)據(jù)庫(kù)提供了統(tǒng)一的編程接口,它由一組Java類和接口組成。是Java程序與數(shù)據(jù)庫(kù)通信的標(biāo)準(zhǔn)API。

JDBC API使得開發(fā)人員可以使用純Java的方式來(lái)連接數(shù) 據(jù)庫(kù),并執(zhí)行操作。

  -2.sun公司由于不知道各個(gè)主流商用數(shù)據(jù)庫(kù)的程序代碼,因此無(wú)法自己寫代碼連接各個(gè)數(shù)據(jù)庫(kù),因此,sun公司自己提供了一套API,只要數(shù)據(jù)庫(kù)想要和Java連接的,

數(shù)據(jù)庫(kù)廠商必須自己實(shí)現(xiàn)JDBC這套接口。而數(shù)據(jù)庫(kù)廠商的JDBC實(shí)現(xiàn),我們就叫它此數(shù)據(jù)庫(kù)的驅(qū)動(dòng)。

2.JDBC訪問(wèn)數(shù)據(jù)庫(kù)流程

  1.加載JDBC驅(qū)動(dòng)程序

  2.建立與數(shù)據(jù)庫(kù)的連接

  3.發(fā)送SQL語(yǔ)句

  4.得到SQL執(zhí)行結(jié)果

3.Java程序和數(shù)據(jù)庫(kù)連接的時(shí)候,其實(shí)就是Socket連接。

4.JDBC常用接口

  Driver接口

    - Driver接口由數(shù)據(jù)庫(kù)廠家提供,對(duì)于Java開發(fā)者而言,只需要使用Driver接口就可以了。

    - 在編程中要連接數(shù)據(jù)庫(kù),必須先裝載特定廠商的數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序。不同的數(shù)據(jù)庫(kù)有不同的裝載方法。

    - 驅(qū)動(dòng):就是各個(gè)廠商實(shí)現(xiàn)Sun公司提出的JDBC接口。即對(duì)Connection等接口的實(shí)現(xiàn)類的jar文件。

    - 裝載mysql驅(qū)動(dòng):Class.forName("com.mysql.jdbc.Driver");

    - 裝載oracle驅(qū)動(dòng):Class.forName("com.jdbc.driver.OracleDriver");

  DriverManager接口

    - DriverManager接口是JDBC的管理層,作用于用戶和驅(qū)動(dòng)程序之間。

    - DriverManager跟蹤可用的驅(qū)動(dòng)程序,并在數(shù)據(jù)庫(kù)和相應(yīng)的驅(qū)動(dòng)之間建立連接。

  Connection接口

    - Connection與特定數(shù)據(jù)庫(kù)的連接(會(huì)話),在連接上下文中執(zhí)行SQL語(yǔ)句并返回結(jié)果。

    - DriverManager的getConnection()方法建立在JDBC URL中定義的數(shù)據(jù)庫(kù)Connection連接上

      -Mysql

        Connection con = DriverManager.getConnection("jdbc:mysql://host:port/database", "user", "password");

      -Oracle

        Connection con = DriverManager.getConnection("jdbc:oracle:thin@host:port/database", "user", "password");

   Statement接口

    - 用于執(zhí)行靜態(tài)SQL語(yǔ)句并返回它所生成結(jié)果的對(duì)象。

    - 三種Statement類:

      - Statement:

        由createStatement創(chuàng)建,用于發(fā)送簡(jiǎn)單的SQL語(yǔ)句(不帶參數(shù))

      - PreparedStatement:

        - 繼承自Statement接口,有preparedStatement創(chuàng)建,用于發(fā)送含有一個(gè)或者多個(gè)輸入?yún)?shù)的SQL語(yǔ)句。PreparedStatement對(duì)象

         比Statement對(duì)象效率更高,并且是防止SQL注入。我們一般都使用PreparedStatement。

      - CallableStatement:

        - 繼承自preparedStatement。由方法prePareCall創(chuàng)建,用于調(diào)用存儲(chǔ)過(guò)程。

    - 常用的Statement方法

        - execute():運(yùn)行語(yǔ)句,返回是否有結(jié)果集。

        - executeQuery():運(yùn)行select語(yǔ)句,返回ResultSet結(jié)果節(jié)

        - executeUpdate():運(yùn)行insert/update/delete操作,返回更新的行數(shù)。

    - 例子:

        

復(fù)制代碼
package com.yf.jdbc.test;

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

/**
 * 測(cè)試執(zhí)行SQL語(yǔ)句和SQL注入問(wèn)題
 * @author yangf
 *
 */
public class Demo02 {
    public static void main(String[] args) {
        try {
            // 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            // 獲得connection對(duì)象 建立與數(shù)據(jù)庫(kù)連接
            /*
             * 連接對(duì)象內(nèi)部其實(shí)包含了Socket對(duì)象,是一個(gè)遠(yuǎn)程連接。比較耗時(shí)!這是Connection對(duì)象管理的一個(gè)要點(diǎn)。
             * 后面通過(guò)連接池來(lái)做
             */
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
            
            Statement stmt = con.createStatement();
            String sql = "insert into t_user (username,pwd,regTime) values ('yyy',123123,now())";
            stmt.execute(sql);
            
            String sql1 = "delete from t_user where id = 4";
            stmt.execute(sql1);
            // 測(cè)試SQL注入
            // 由于是外部傳入?yún)?shù),所以可以任意修改DB,造成SQL注入,所以我們不適用Statement
            String id = "5 or 1 = 1";
            String sql2 = "delete from t_user where id = " + id;
            stmt.execute(sql2);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
復(fù)制代碼
復(fù)制代碼
package com.yf.jdbc.test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * 測(cè)試PreparedStatement的基本用法
 * @author yangf
 *
 */
public class Demo03 {
    public static void main(String[] args) {
        try {
            // 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            // 獲得connection對(duì)象 建立與數(shù)據(jù)庫(kù)連接
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
            String sql = "insert into t_user (username,pwd,regTime) values (?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, "yangf");
            ps.setInt(2, 888888);
            ps.setDate(3, new Date(System.currentTimeMillis()));
            
            ps.execute();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
復(fù)制代碼

 

  ResultSet接口

      - Statement執(zhí)行SQL語(yǔ)句時(shí)返回Result結(jié)果集。

      - ResultSet提供的檢索不同類型字段的方法,常用的有:

        - getString():獲得在數(shù)據(jù)庫(kù)里是varchar,char等數(shù)據(jù)類型的對(duì)象。

        - getFloat():獲得數(shù)據(jù)庫(kù)里是Float類型的對(duì)象

        - getDate():獲得數(shù)據(jù)庫(kù)里是Date類型的對(duì)象

        - getBoolean():獲得數(shù)據(jù)庫(kù)里是Boolean類型的數(shù)據(jù)

      - 依序關(guān)閉使用的對(duì)象和連接

        Result->Statement->Connection

復(fù)制代碼
/**
 * 測(cè)試ResultSet的用法
 * @author ibm
 *
 */
public class Demo04 {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            // 獲得connection對(duì)象 建立與數(shù)據(jù)庫(kù)連接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
            String sql = "select * from t_user where id > ?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, 2);
            // 內(nèi)部就是個(gè)迭代器
            rs = ps.executeQuery();
            
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "--" + rs.getString(2) + "--" + rs.getInt(3) + "--" + rs.getDate(4));
            }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
復(fù)制代碼

 批處理

  - batch

  - 對(duì)于大量的批處理,建議使用Statement,因?yàn)镻reparedStatement的預(yù)編譯空間有限,數(shù)據(jù)量特別大時(shí),會(huì)發(fā)生異常。

  

復(fù)制代碼
package com.yf.jdbc.test;

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

/**
 * 測(cè)試批處理
 * @author yangf
 *
 */
public class Demo05 {
    public static void main(String[] args) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            // 獲得connection對(duì)象 建立與數(shù)據(jù)庫(kù)連接
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
            // jdbc中事物是自動(dòng)提交的,我們?cè)O(shè)為手動(dòng)提交
            con.setAutoCommit(false);
            long start = System.currentTimeMillis();
            stmt = con.createStatement();
            
            for (int i = 0; i < 20000; i++) {
                stmt.addBatch("insert into t_user (username,pwd,regTime) values ('yangf "+ i + "', 6666 , now())");
            }
            
            stmt.executeBatch();
            con.commit();
            long end = System.currentTimeMillis();
            
            System.out.println("插入20000萬(wàn)條數(shù)據(jù)耗時(shí):" + (end - start) + "毫秒");
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
復(fù)制代碼

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多