JDBC batch processing and JDBC Batch Processing

Source: Internet
Author: User

JDBC batch processing and JDBC Batch Processing


JDBC batch processing:

1) Batch Processing: process a large amount of data at a time.
Explanation: Sometimes you need to send a batch of SQL statements to the database for execution. In this case, you should avoid sending and executing SQL statements to the database,

The JDBC batch processing mechanism should be adopted to improve the execution efficiency.
2) two methods:
Statement: // Statement stat = conn. createStatement (); Create Statement
Stat. addBatch (SQL): Fill in the buffer to compile the SQL statement.
Stat.exe cuteBatch (): one-time execution of data in the buffer zone.
PreparedStatement: // PreparedStatement ps = conn. prepareStatement (SQL );
Ps. addBatch (): Put the parameter into the placeholder? Stored in the cache.
Ps.exe cuteBatch (): one-time execution of data in the cache.
3) case:
Insert 1070 data records into the table bank_money.


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 ', '20140901') "; stat. addBatch (SQL); // enter SQL in the buffer for compilation. Stat.exe cuteQuery (SQL);} stat.exe cuteBatch (); //: one-time execution of data in the buffer zone. 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 (); // Add a parameter to a placeholder? If (I % 100 = 0) {ps.exe cuteBatch (); // the data in the cache is executed once every 100 times .,} Ps.exe cuteBatch (); // execute long l2 = System once for the buffer data. 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; /** to read the configuration file, you only need to load the configuration file once */static {try {// read the content from the db file/* The current file path is under the project name by default. * "Db. properties "*: Read the configuration file */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 load driver} 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. establish 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 (); // return the connection object }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.