Skip to content

Instantly share code, notes, and snippets.

@arockwell
Created February 12, 2010 21:08
Show Gist options
  • Select an option

  • Save arockwell/302984 to your computer and use it in GitHub Desktop.

Select an option

Save arockwell/302984 to your computer and use it in GitHub Desktop.
package template;
import java.util.ArrayList;
import java.util.List;
public class InsertStatementGenerator implements DelimitedStringParser {
private int lineCounter = 0;
private String tableName = "";
private List<String> columns = new ArrayList<String>();
public String doParseLine(String rawLine) {
String result;
String[] tempCols = rawLine.split(",");
if (lineCounter == 0) {
tableName = tempCols[0];
for (int i = 1; i < tempCols.length; i++) {
columns.add(tempCols[i]);
}
result = "";
} else {
result = "insert into " + tableName + "(";
for (int i = 0; i < columns.size(); i++) {
if (i != columns.size() - 1) {
result += columns.get(i) + ",";
} else {
result += columns.get(i) + ")";
}
}
result += "VALUES (";
for (int i = 0; i < tempCols.length; i++) {
if (i != tempCols.length - 1) {
result += tempCols[i] + ",";
} else {
result += tempCols[i] + ")";
}
}
}
lineCounter++;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment