Java MySQL batch processing and javamysql Batch Processing

Source: Internet
Author: User

Java MySQL batch processing and javamysql Batch Processing

In essence, it is to use the addBatch () method of Statement and PreStatement.

Code

import java.sql.*;public class GetConnection{public static void main(String[] args){Access2Database adb=new Access2Database();Connection conn=adb.getConn();//transaction dealingPreparedStatement pstam=null;try{conn.setAutoCommit(false);String sql="insert into student(name,major,score) values(?,?,?);";pstam=conn.prepareStatement(sql);pstam.setString(1, "f");pstam.setString(2,"History");pstam.setInt(3, 67);pstam.addBatch();pstam.setString(1, "h");pstam.setString(2, "Biology");pstam.setInt(3, 85);pstam.addBatch();pstam.executeBatch();conn.commit();}catch(SQLException e){try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}e.printStackTrace();}finally{try {conn.setAutoCommit(true);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//release the resource of the programtry{pstam.close();conn.close();}catch(SQLException e){e.printStackTrace();}}}

Related Article

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.