Batch JDBC statement to improve processing speed

zhaozj2021-02-16  78

Sometimes JDBC is not ideal, which prompted us to write some stored procedures related to a particular database. As a replacement, try the batch characteristics of Statement to see if all SQL statements will increase. The simplest form of stored procedures is that the entire process contains only a set of SQL statements. Placing these statements together can easily manage can also increase the running speed. The Statement class has the ability to include a string SQL statement, so it allows all SQL statements to be executed in a database session, thereby avoiding a series of execution calls to the database. Using batch features involves two methods: addbatch (string) method Executebatch method addbatch method can accept a standard SQL (if you use a statement) as a parameter, you can use a parameter without bringing (if you use a preparedStatement). The executebatch method then executes the SQL statement and returns an INT type array. This array includes the number of rows affects each statement. If you use a SELECT or other returned statement in a batch, you will generate a SQLEXCEPTION exception. Here is an example of a simple java.sql.Statement: statement stmt = conn.createstatement (); Stmt.insert ("delete from users"); Stmt.insert ("Insert Into Uses Values" ("Rod ', 37,' Circle ') "); Stmt.Insert (" INSERT INTO USERS VALUES (' Jane ', 33,' Triangle '); Stmt.insert ("Insert Into Users Values ​​(' Freddy ', 29,' Square ') ); int [] counts = stmt.executebatch (); use preparedStatement slightly different. It can only handle a SQL statement, but you can bring a lot of parameters. The following is an example of using preparedStatement rewritten: // Note We don't do any delete action pre previoustement stmt = conn.preparestatement (_ "INSERT INTO USERS VALUES (?,?,?)"); User [] users = ...; for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-14839.html

New Post(0)