Home >Java >javaTutorial >Using Java to implement batch operation of form data
Use Java to implement the batch operation function of form data
In modern software development, the batch operation function of form data has become a common requirement. For example, we may need to batch insert, batch update or batch delete form data. By using Java language, we can easily implement these functions. This article will introduce you to how to use Java to implement batch operations on form data, and attach relevant code examples.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { public static Connection getConnection() { Connection con = null; String url = "jdbc:mysql://localhost:3306/form_data"; String user = "root"; String password = "root"; try { con = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return con; } }
In the above code example, we used the MySQL database and passed in the database URL, username, and password to obtain the database connection.
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class BatchInsert { public static void main(String[] args) { Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = null; String sql = "INSERT INTO form_table (`name`, `age`) VALUES (?, ?)"; try { con.setAutoCommit(false); ps = con.prepareStatement(sql); // 模拟批量插入数据 for (int i = 1; i <= 1000; i++) { ps.setString(1, "Name " + i); ps.setInt(2, i); ps.addBatch(); if (i % 100 == 0) { ps.executeBatch(); con.commit(); } } ps.executeBatch(); con.commit(); } catch (SQLException e) { try { con.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
In the above code example, we use PreparedStatement to pre-compile the insert statement, and use the addBatch() method to combine a series of insert operations Add to batch. When a certain batch size (such as 100) is reached, we use the executeBatch() method to perform the batch operation and call the commit() method to commit the transaction. This improves insertion efficiency.
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class BatchUpdate { public static void main(String[] args) { Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = null; String sql = "UPDATE form_table SET age = ? WHERE id = ?"; try { con.setAutoCommit(false); ps = con.prepareStatement(sql); // 模拟批量更新数据 for (int i = 1; i <= 1000; i++) { ps.setInt(1, i * 2); ps.setInt(2, i); ps.addBatch(); if (i % 100 == 0) { ps.executeBatch(); con.commit(); } } ps.executeBatch(); con.commit(); } catch (SQLException e) { try { con.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
In the above code example, we use PreparedStatement to pre-compile the update statement, and use the addBatch() method to add a series of update operations into batch processing. By setting an appropriate batch size (say 100), we can improve update efficiency.
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class BatchDelete { public static void main(String[] args) { Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = null; String sql = "DELETE FROM form_table WHERE id = ?"; try { con.setAutoCommit(false); ps = con.prepareStatement(sql); // 模拟批量删除数据 for (int i = 1; i <= 1000; i++) { ps.setInt(1, i); ps.addBatch(); if (i % 100 == 0) { ps.executeBatch(); con.commit(); } } ps.executeBatch(); con.commit(); } catch (SQLException e) { try { con.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
In the above code example, we use PreparedStatement to pre-compile the deletion statement, and use the addBatch() method to add a series of deletion operations into batch processing. By setting a suitable batch size (such as 100), we can improve deletion efficiency.
Summary
By using Java language, we can easily implement the batch operation function of form data. In this article, we introduce how to use Java to implement the functions of batch insert, batch update and batch deletion of data, and attach relevant code examples. I hope this article will be helpful for you to use the batch operation function in actual development.
The above is the detailed content of Using Java to implement batch operation of form data. For more information, please follow other related articles on the PHP Chinese website!