JDBC的批處理,JDBC批處理

來源:互聯網
上載者:User

JDBC的批處理,JDBC批處理

 

JDBC的批處理:

    1)批處理:一次性處理很多資料。
  解釋:有時候需要向資料庫發送一批sql語句執行,這時應避免向資料庫一條條的發送執行,

   而應採用JDBC的批處理機制,以提升執行效率。
    2)兩種方式:
      Statement :                  //Statement stat=conn.createStatement();建立語句
        stat.addBatch(sql):將sql填入緩衝區,進行編譯。
        stat.executeBatch():將緩衝區中的資料一次性執行。
      PreparedStatement:    //PreparedStatement ps=conn.prepareStatement(sql);
        ps.addBatch():將參數放入預留位置?存入緩衝區。
        ps.executeBatch()::將緩衝區中的資料一次性執行。
    3)案例:
      向表bank_money插入1070條資料。

 

1)@Test    public void testStatement() {        Connection conn=null;        try {            conn= DBUtil2.getconn();            Statement stat=conn.createStatement();            long l1=System.currentTimeMillis();            for(int i=10;i<1080;i++) {                String sql="insert into login_info                            values("+i+",'zs','111111')";                 stat.addBatch(sql);                  //將sql填入緩衝區,進行編譯。                           stat.executeQuery(sql);            }            stat.executeBatch();//:將緩衝區中的資料一次性執行。            long l2=System.currentTimeMillis();            System.out.println(l2-l1);        } catch (Exception e) {            e.printStackTrace();        }    }       2)@Test    public void testPreparedStatement() {        Connection conn=null;        try {            conn=DBUtil2.getconn();            String sql="insert into login_info values(?,?,?)";            PreparedStatement ps=conn.prepareStatement(sql);            long l1=System.currentTimeMillis();            for(int i=10;i<1080;i++) {                ps.setInt(1, i);                ps.setString(2, "zs");                ps.setString(3, "111111");                ps.addBatch();// 將參數放入預留位置?存入緩衝區                if(i%100==0) {                    ps.executeBatch();//每100次將 將緩衝區中的資料一次性執行。,                }            }            ps.executeBatch();//將緩衝區資料一次性執行            long l2=System.currentTimeMillis();            System.out.println(l2-l1);        } catch (Exception e) {            e.printStackTrace();        }finally {            DBUtil2.colseconn(conn);        }    }  3)    public class DBUtil2 {    private static String driver;    private static String url;    private static String user;    private static String pwd;    /**讀取設定檔,只需要載入一次*/    static {        try {//從db檔案讀取出來內容            /*檔案字元流:預設當前檔案路徑在工程名下。                      * "db.properties"             * :通過當前類的類載入器所擷取的流讀取設定檔             */            FileReader fr=new FileReader("db.properties");            Properties prop=new Properties();            prop.load(fr);            driver=prop.getProperty("driver");            url=prop.getProperty("url");            user=prop.getProperty("user");            pwd=prop.getProperty("pwd");            Class.forName(driver);//1載入驅動        } catch (Exception e) {            e.printStackTrace();            /*  "db.properties:                          driver=oracle.jdbc.driver.OracleDriver             #driver=oracle.jdbc.OracleDriver             url=jdbc:oracle:thin:@localhost:1521:orcl             user=scott             pwd=1234             *              */        }    }    public static  Connection getconn() {//2.建立串連        Connection conn=null;        try {            conn = DriverManager.getConnection(                     url, user, pwd);                     } catch (Exception e) {            e.printStackTrace();        }        return conn;    }    public static void colseconn(Connection conn) {        try {            if(conn!=null) {                conn.close();            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        System.out.println(getconn());//返回連線物件    }}    

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.