[JavaSE] JDBC batch processing, javasejdbc Batch Processing

Source: Internet
Author: User

[JavaSE] JDBC batch processing, javasejdbc Batch Processing

Send Multiple SQL statements to the database

Create database batch

Use batch

Create table batch_table (

Id int primary key auto_increment,

Name varchar (20)

)

Insert into batch_table values (null, "aaa ")

Insert into batch_table values (null, "bbb ")

Insert into batch_table values (null, "ccc ")

 

Execute batch processing in Statement mode:

Advantage: You can execute multiple SQL statements with different structures.

Disadvantage: the pre-compilation mechanism is not used and the efficiency is low.

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. statement; public class JDBCBatchTest {public static void main (String [] args) {try {// register the database driver // DriverManager. registerDriver (new Driver (); // Class Using Reflection. forName ("com. mysql. jdbc. driver "); // get the database connection. When importing a package, note that you must import java. in SQL, Connection conn = DriverManager for interface programming. getConnection ("jdbc: mysql: // localhost: 3306/java", "root", "root"); // get the transmitter object Statement = conn. createStatement (); statement. addBatch ("create database batch"); statement. addBatch ("use batch"); statement. addBatch ("create table batch_table (id int primary key auto_increment, name varchar (20)"); statement. addBatch ("insert into batch_table values (null, 'aaa')"); statement. addBatch ("insert into batch_table values (null, 'bbb ')"); statement. addBatch ("insert into batch_table values (null, 'ccc ')"); statement.exe cuteBatch ();} catch (Exception 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.