public class TableExporterImporter
extends java.lang.Object
When creating large MySQL tables, it is much more efficient to export the data to a file, then import the file using the MySQL "load data infile" command, than it is to directly insert each individual row into the table. This class facilitates this optimization.
Constructor and Description |
---|
TableExporterImporter(java.lang.String tableName,
java.lang.String columnNames,
java.lang.String path,
boolean append)
Creates a new MySQL table exporter/importer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the data file.
|
protected java.lang.String |
fixPathSeparators(java.lang.String path)
Fix path separators in path.
|
int |
importData(java.sql.Connection c)
Imports the data into the MySQL table.
|
void |
print(byte val)
Prints a byte field value.
|
void |
print(int val)
Prints an integer field value.
|
void |
print(java.lang.Integer val)
Prints an Integer field value.
|
void |
print(long val)
Prints a long field value.
|
void |
print(java.lang.Long val)
Prints a Long field value.
|
void |
print(java.lang.String val)
Prints a string field value.
|
void |
println()
Starts a new row.
|
void |
printNull()
Prints a null field value.
|
public TableExporterImporter(java.lang.String tableName, java.lang.String columnNames, java.lang.String path, boolean append) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException, java.io.IOException
tableName
- MySQL table name.columnNames
- MySQL column names, comma-delimited, in the
order in which their values will be printed.
May be null, in which case the MySQL-defined
order must be used.path
- Path to data file, or null to use a
temporary scratch file.append
- True to append to data file, false to
overwrite any existing data file.java.io.FileNotFoundException
java.io.UnsupportedEncodingException
java.io.IOException
public void print(java.lang.String val)
val
- Field value.public void print(long val)
val
- Field value.public void print(int val)
val
- Field value.public void print(byte val)
val
- Field value.public void printNull()
public void print(java.lang.Integer val)
val
- Field value.public void print(java.lang.Long val)
val
- Field value.public void println()
public void close()
protected java.lang.String fixPathSeparators(java.lang.String path)
path
- The file path to fix.public int importData(java.sql.Connection c) throws java.sql.SQLException
The data file is deleted after the data has been imported.
c
- JDBC connection to MySQL database.java.sql.SQLException