JDBC的批處理

2021-08-19 02:53:43 字數 1412 閱讀 2920

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()

stat.executebatch();

//:將緩衝區中的資料一次性執行。

long l2=system.currenttimemillis();

system.out.println(l2-l1);

} catch

(exception e)

}

2)@test

public

void

testpreparedstatement()

}ps.executebatch();

//將緩衝區資料一次性執行

long l2=system.currenttimemillis();

system.out.println(l2-l1);

} catch

(exception e)

finally

} 3)

public

class

dbutil2 catch

(exception e)

}public

static connection getconn()

catch

(exception e)

return

conn;

}public

static

void

colseconn(connection conn)

} catch

(exception e)

}public

static

void

main(string args)

}

JDBC的批處理

第一種 採用statement.addbatch sql 方式實現批處理 優點 可以向資料庫傳送多條不同的 語句。缺點 sql語句沒有預編譯。當向資料庫傳送多條語句相同,但僅引數不同的sql語句時,需重複寫上很多條sql語句。例如 insert into user name,password val...

jdbc 批處理操作

jdbc的批處理操作是指把一組sql語句 增刪改操作 一次性提交給資料庫去執行,提高效率。分為statement版和preparedstatement版。size medium 1.例子 size 資料庫軟體 postgresql 資料庫名稱 test 資料庫圖表 intense 資料庫表 mira...

JDBC中的批處理

jdbc中 有兩個批處理函式 addbatch string 和 executebatch 有兩種方法可以進行批處理 第一種是用statement 另外一種是preparedstatement 一 使用statement 1statement sm con.createstatement 2stri...