Writers permits to write data rendered from csv file where you want.
csvtosql come with three default implementations of writer:
- StandardOutputWrite, write statements to standard output using System.out.println()
- SqlInsertWriter, write statements to a file specified in configuration.
- JdbcWriter, write statements to a jdbc resource, with options specified in configuration.
You can write your own writer extending class net.sf.csv2sql.writers.AbstractWriter,
and this tutorial explain how to extend this class. We using for example SqlFileWriter class.
This is the AbstractWriter class:
public abstract class AbstractWriter {
private Properties properties;
public abstract void write(ArrayList statements);
protected Properties getWriterProperties() {
return this.properties;
}
public void configure(Properties properties) {
this.properties = properties;
}
}
You simply need to override the write() method, the ArrayList passed to this method
contain the statemens already rendered.
This is the extended SqlFileWriter class:
public class SqlFileWriter extends AbstractWriter {
public void write(ArrayList statements) {
try {
File out = new File(getWriterProperties().getProperty("filename"));
BufferedWriter output = new BufferedWriter(new FileWriter(out));
Iterator it = statements.iterator();
while (it.hasNext()) {
output.write( (String)it.next() +"\n" );
}
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The getWriterProperties() method return a java.util.Properties object containing all
option for this writer in configuration, so getWriterProperties().getProperty("filename")
method return the value for the option names "filename".
|