In JDBC Development, the operational database needs to establish a connection with the database, and then the SQL statements to be executed are sent to the database server, and finally the database connection is closed, according to this operation, if you follow this process to execute multiple SQL statements, then you need to establish multiple database connections, Wasting time on a database connection, JDBC gives a good solution------JDBC Batch processing for such a problem.
Sample code
(1) index.jsp
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Batch processing operations</title>8 </Head>9 <Body>Ten <Jsp:usebeanID= "Batch"class= "Com.java.Batch"></Jsp:usebean> One <% A //perform a BULK insert operation - intRow=Batch.savebatch (); - Out.print ("BULK Insert the ""+Row+"article Information ""); the %> - </Body> - </HTML>
View Code
(2) Batch Class
1 PackageCom.java;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 Importjava.sql.SQLException;7 ImportJava.util.Random;8 9 Public classBatch {Ten PublicConnection getconnection () { One //Database Connection AConnection conn =NULL; - Try { - //Load Database driver, register to drive manager theClass.forName ("Com.mysql.jdbc.Driver"); - //Database connection String -String url = "Jdbc:mysql://localhost:3306/test"; - //Database user name +String username = "root"; - //Database Password +String password = "123456"; A //Create a connection connection atconn =drivermanager.getconnection (Url,username,password); - -}Catch(ClassNotFoundException e) { - e.printstacktrace (); -}Catch(SQLException e) { - in e.printstacktrace (); - } to //back to database connection + returnConn; - } the /** * * Bulk Add Data $ * @returnThe number of rows affectedPanax Notoginseng */ - Public intSavebatch () { the //Number of rows + intRow = 0; A //Get database connection theConnection conn =getconnection (); + Try { - //SQL statement to insert data $String sql = "INSERT into book (Id,name,price,bookcount,author) VALUES (?,?,?,?,?)"; $ //Create Preparestatement -PreparedStatement PS =conn.preparestatement (SQL); - //Instantiate Random theRandom random =NewRandom (); - //Looping Add DataWuyi for(inti=10;i<20;i++) { the //Assigning a value to the first parameter in an SQL statement -Ps.setint (1, i+1); Wu //Assigning a value to the second parameter in an SQL statement -Ps.setstring (2, "book" +i); About //Assigning a value to the third parameter in an SQL statement $Ps.setdouble (3, i%2); - //Assigning a value to the fourth parameter in an SQL statement -Ps.setint (4, Random.nextint (5) +10); - //Assigning a value to the fifth parameter in an SQL statement APs.setstring (5, "author" +i); + //Add Batch command the Ps.addbatch (); - } $ //performs a batch operation and returns an array of Count components the int[] rows =Ps.executebatch (); the //assign a value to the number of rows therow =rows.length; the //Close Preparestatement - ps.close (); in //Close Connection the conn.close (); the}Catch(Exception e) { About e.printstacktrace (); the } the the returnRow; + } -}
Program Run Result:
Batch processing of JDBC operations database