edu.northwestern.at.utils.db.mysql
Class TableExporterImporter

java.lang.Object
  extended by edu.northwestern.at.utils.db.mysql.TableExporterImporter

public class TableExporterImporter
extends java.lang.Object

MySQL Table Exporter/Importer.

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 Summary
TableExporterImporter(java.lang.String tableName, java.lang.String columnNames, java.lang.String path, boolean append)
          Creates a new MySQL table exporter/importer.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableExporterImporter

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
Creates a new MySQL table exporter/importer.

Parameters:
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.
Throws:
java.io.FileNotFoundException
java.io.UnsupportedEncodingException
java.io.IOException
Method Detail

print

public void print(java.lang.String val)
Prints a string field value.

Parameters:
val - Field value.

print

public void print(long val)
Prints a long field value.

Parameters:
val - Field value.

print

public void print(int val)
Prints an integer field value.

Parameters:
val - Field value.

print

public void print(byte val)
Prints a byte field value.

Parameters:
val - Field value.

printNull

public void printNull()
Prints a null field value.


print

public void print(java.lang.Integer val)
Prints an Integer field value.

Parameters:
val - Field value.

print

public void print(java.lang.Long val)
Prints a Long field value.

Parameters:
val - Field value.

println

public void println()
Starts a new row.


close

public void close()
Closes the data file.


fixPathSeparators

protected java.lang.String fixPathSeparators(java.lang.String path)
Fix path separators in path.

Parameters:
path - The file path to fix.
Returns:
The file path with backslashes replaced by forward slashes.

importData

public int importData(java.sql.Connection c)
               throws java.sql.SQLException
Imports the data into the MySQL table.

The data file is deleted after the data has been imported.

Parameters:
c - JDBC connection to MySQL database.
Returns:
Number of rows imported.
Throws:
java.sql.SQLException