Created
March 2, 2023 12:59
-
-
Save realyota/ce0b824ae5b44e56be91ce62a3357578 to your computer and use it in GitHub Desktop.
Batching in jdbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class BatchInsertExample { | |
public static void main(String[] args) { | |
String sourceUrl = "jdbc:mysql://localhost:3306/source_db"; | |
String sourceUsername = "root"; | |
String sourcePassword = "password"; | |
String destUrl = "jdbc:mysql://localhost:3306/dest_db"; | |
String destUsername = "root"; | |
String destPassword = "password"; | |
try { | |
Connection sourceConn = DriverManager.getConnection(sourceUrl, sourceUsername, sourcePassword); | |
Connection destConn = DriverManager.getConnection(destUrl, destUsername, destPassword); | |
Statement stmt = sourceConn.createStatement(); | |
ResultSet rs = stmt.executeQuery("SELECT * FROM source_table"); | |
PreparedStatement pstmt = destConn.prepareStatement("INSERT INTO dest_table (col1, col2, col3) VALUES (?, ?, ?)"); | |
int batchSize = 50000; | |
int count = 0; | |
while (rs.next()) { | |
pstmt.setInt(1, rs.getInt("col1")); | |
pstmt.setString(2, rs.getString("col2")); | |
pstmt.setDouble(3, rs.getDouble("col3")); | |
pstmt.addBatch(); | |
count++; | |
if (count % batchSize == 0) { | |
pstmt.executeBatch(); | |
} | |
} | |
// Execute the remaining batch | |
pstmt.executeBatch(); | |
rs.close(); | |
stmt.close(); | |
pstmt.close(); | |
sourceConn.close(); | |
destConn.close(); | |
System.out.println("Batch insert completed successfully"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment