fj-copy-jdbc-db is a high-performance, robust, and highly dynamic Java utility designed to copy table data from a source database query to a destination database table using standard JDBC metadata APIs.
- Metadata-Driven Execution: Dynamically extracts column names, mappings, and database data types directly from the source query result set. No hardcoded schemas are required.
- High-Performance Batching: Executes inserts using optimized prepared statement batches to minimize database roundtrips.
- Robust LOB & Type Mapping: Gracefully translates standard primitive datatypes, nullable fields, and large objects (
CLOB,BLOB) using native Java streams. - Configurable Control: Supports custom execution modes, including configurable batch sizes and optional target table truncation before copy operations.
mvn clean installmvn clean install -P testCopies all columns returned by a source query directly to the target table using default settings (batch size of 1000, no target truncation):
import java.sql.Connection;
import org.fugerit.java.db.copy.CopyJDBC;
Connection srcConn = ...; // Source DB Connection
Connection destConn = ...; // Destination DB Connection
String srcQuery = "SELECT id, name, created_at, details FROM my_source_table";
String destTable = "my_destination_table";
int rowsCopied = CopyJDBC.copy(srcConn, destConn, srcQuery, destTable);
System.out.println("Copied " + rowsCopied + " rows successfully!");Use the builder options inside CopyConfig to control target truncation and customize the size of your execution batches:
import java.sql.Connection;
import org.fugerit.java.db.copy.CopyJDBC;
import org.fugerit.java.db.copy.CopyConfig;
Connection srcConn = ...;
Connection destConn = ...;
String srcQuery = "SELECT * FROM employees WHERE department = 'IT'";
String destTable = "it_employees_backup";
CopyConfig config = CopyConfig.builder()
.batchSize(5000) // Run prepared statements in batches of 5000
.truncateDest(true) // Delete all existing destination rows before copying
.build();
int rowsCopied = CopyJDBC.copy(srcConn, destConn, srcQuery, destTable, config);
System.out.println("Copied " + rowsCopied + " rows into the backup table!");