Change exception handling

master
Zoran Zaric 7 years ago
parent da6d849dc2
commit 33cb4d45b7

@ -38,23 +38,29 @@ public class Main {
private static ExecutionMode executionMode = ExecutionMode.PLUGIN_BACKEND;
private static Connection getConnection(String dbIp, long port, String sid, String username, String password) {
private static Connection getConnection(String dbIp, long port, String sid, String username, String password) throws SQLException {
log.debug("Establishing conneciton...");
Connection conn = null;
String jdbcConnection = String.format("jdbc:oracle:thin:@%s:%d:%s", dbIp, port, sid);
try {
conn = DriverManager.getConnection(jdbcConnection, username, password);
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException("Couldn't connect to database", e);
}
log.debug("Established conneciton!");
return conn;
}
private static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.error(e);
}
}
}
private static QueryResult executeExampleQueryWithException(Connection conn) throws SQLException, JsonProcessingException {
String sql = "SELECT id, version, workspac FROM s_settings";
return SqlExecutor.executeQuery(conn, sql, 10L);
@ -127,39 +133,23 @@ public class Main {
}
}
public static void main(String[] args) throws Exception {
String frogRunnerMode = System.getenv("FROG_RUNNER_MODE");
if (frogRunnerMode != null) {
if (frogRunnerMode.equals("TERMINAL")) {
executionMode = ExecutionMode.TERMINAL;
}
private static void serveForever(String host, long port, String sid, String username, String password) {
Connection conn = null;
try {
try {
conn = getConnection(host, port, sid, username, password);
} catch (SQLException e) {
log.error(e.getMessage(), e);
System.err.println("Could not connect to Database: " + e.getMessage());
System.exit(1);
}
BasicConfigurator.configure();
LogManager.getRootLogger().setLevel(Level.OFF);
LogManager.getLogger("at.compax.tools.sql.main").setLevel(executionMode.getLoggingLevel());
LogManager.getLogger("at.compax.tools.sql").setLevel(executionMode.getLoggingLevel());
if (args.length == 0) {
runTests();
} else {
if (args.length < 4)
throw new IllegalArgumentException("Arguments: Host Port sid username password [max rownums]");
String host = args[0];
long port = Long.parseLong(args[1]);
String sid = args[2];
String username = args[3];
String password = args[4];
@Cleanup
Connection conn = getConnection(host, port, sid, username, password);
log.info("Waiting for input...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
String line;
StringBuffer commandBuffer = new StringBuffer();
try {
while ((line = br.readLine()) != null) {
if (!"--- END ---".equals(line)) {
commandBuffer.append(line).append("\n");
@ -177,6 +167,43 @@ public class Main {
commandBuffer.setLength(0);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
} finally {
closeConnection(conn);
}
}
public static void main(String[] args) {
String frogRunnerMode = System.getenv("FROG_RUNNER_MODE");
if (frogRunnerMode != null) {
if (frogRunnerMode.equals("TERMINAL")) {
executionMode = ExecutionMode.TERMINAL;
}
}
BasicConfigurator.configure();
LogManager.getRootLogger().setLevel(Level.OFF);
LogManager.getLogger("at.compax.tools.sql.main").setLevel(executionMode.getLoggingLevel());
LogManager.getLogger("at.compax.tools.sql").setLevel(executionMode.getLoggingLevel());
if (args.length == 0) {
runTests();
} else {
if (args.length < 4)
throw new IllegalArgumentException("Arguments: Host Port sid username password [max rownums]");
String host = args[0];
long port = Long.parseLong(args[1]);
String sid = args[2];
String username = args[3];
String password = args[4];
serveForever(host, port, sid, username, password);
}
}
}

Loading…
Cancel
Save