Skip to content

Commit

Permalink
Small fix to ignore empty lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
sofiageo committed Mar 16, 2016
1 parent fcd4ec1 commit 086a304
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# Chameleon
# Chameleon

Chameleon is a JDBC implementation that uses UcanAccess library in order to execute SQL statements in an MS-Access database. It can be extended to use any JDBC driver.

## Developers notes

If you want to debug the jar file, you can run the application using these parameters:
`java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y -jar chameleon-1.0.jar`
11 changes: 4 additions & 7 deletions src/main/java/eionet/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.Properties;

/**
* @author George Sofianos
Expand All @@ -17,14 +18,10 @@ public class Main {

public static void main(String[] args) {
Converter converter;
//if (args[3] == "old") {
// SjConverter conv = new SjConverter();
// String[] ar = {"net.ucanaccess.jdbc.UcanaccessDriver", "jdbc:ucanaccess://" + args[0], "n", "n", args[1], "--batch", "all", "--ignore-nodata"};
// conv.nonStaticMain(ar);
// }
Properties props = System.getProperties();
int batchSize = Integer.parseInt(props.getProperty("batchSize", "10000"));
if (args.length == 2) {
converter = new CustomConverter();
converter.setDriver("net.ucanaccess.jdbc.UcanaccessDriver");
converter = new CustomConverter(batchSize);
converter.setUrl("jdbc:ucanaccess://" + args[0]);
converter.setInput(args[1]);
try {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/eionet/converters/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface Converter {
void setUrl(String s);

void setInput(String arg);

}
19 changes: 18 additions & 1 deletion src/main/java/eionet/converters/CustomConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

/**
* @author George Sofianos
Expand All @@ -23,11 +24,15 @@ public class CustomConverter implements Converter {
private String driver;
private String url;
private String input;
private final int batchSize = 10000;
private final int batchSize;
private Statement st;
private Connection connection;
private String query;

public CustomConverter(int batchSize) {
this.batchSize = batchSize;
}


private Connection getConnection(String url) throws SQLException {
Connection connection = DriverManager.getConnection(url);
Expand Down Expand Up @@ -56,13 +61,19 @@ private void rollback() throws SQLException {
private void executeStatements() throws SQLException, IOException {
connection = this.getConnection(url);
log.debug("Connected to database");
log.info("Executing SQL statements now...");
long duration = System.currentTimeMillis();
connection.setAutoCommit(false);
st = connection.createStatement();
if (connection != null) {
BufferedReader inputFile = getFile(input);
int count = 0;
while ((query = inputFile.readLine()) != null) {
query = query.trim();
if (query.isEmpty()) {
log.debug("Ignored empty line");
continue;
}
st.addBatch(query);
if (++count % batchSize == 0) {
st.executeBatch();
Expand All @@ -74,6 +85,11 @@ private void executeStatements() throws SQLException, IOException {

connection.commit();
log.debug("Transaction committed successfully");
duration = System.currentTimeMillis() - duration;
log.info("SQL statements executed successfully, time it took to finish: " + String.format("%d minutes, %d seconds",
TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)))
);
}
}

Expand All @@ -83,6 +99,7 @@ private void executeStatements() throws SQLException, IOException {
public void cleanup() {
try {
connection.close();
log.debug("Connection to database closed");
} catch (SQLException ex) {
log.error("An error has occured while closing the connection: " + ex);
}
Expand Down

0 comments on commit 086a304

Please sign in to comment.