[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 ();}}}