Change exception handling
This commit is contained in:
@@ -38,23 +38,29 @@ public class Main {
|
|||||||
|
|
||||||
private static ExecutionMode executionMode = ExecutionMode.PLUGIN_BACKEND;
|
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...");
|
log.debug("Establishing conneciton...");
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
|
|
||||||
String jdbcConnection = String.format("jdbc:oracle:thin:@%s:%d:%s", dbIp, port, sid);
|
String jdbcConnection = String.format("jdbc:oracle:thin:@%s:%d:%s", dbIp, port, sid);
|
||||||
|
|
||||||
try {
|
|
||||||
conn = DriverManager.getConnection(jdbcConnection, username, password);
|
conn = DriverManager.getConnection(jdbcConnection, username, password);
|
||||||
conn.setAutoCommit(false);
|
conn.setAutoCommit(false);
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException("Couldn't connect to database", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("Established conneciton!");
|
log.debug("Established conneciton!");
|
||||||
return conn;
|
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 {
|
private static QueryResult executeExampleQueryWithException(Connection conn) throws SQLException, JsonProcessingException {
|
||||||
String sql = "SELECT id, version, workspac FROM s_settings";
|
String sql = "SELECT id, version, workspac FROM s_settings";
|
||||||
return SqlExecutor.executeQuery(conn, sql, 10L);
|
return SqlExecutor.executeQuery(conn, sql, 10L);
|
||||||
@@ -127,7 +133,51 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
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);
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
} else {
|
||||||
|
String query = commandBuffer.toString().trim();
|
||||||
|
String result;
|
||||||
|
if (query.toLowerCase().startsWith("select")) {
|
||||||
|
result = formatJsonObject(SqlExecutor.executeQuery(conn, query, null));
|
||||||
|
} else if (query.toLowerCase().startsWith("update") || query.toLowerCase().startsWith("insert")) {
|
||||||
|
result = formatJsonObject(SqlExecutor.executeUpdate(conn, query));
|
||||||
|
} else {
|
||||||
|
result = formatJsonObject(SqlExecutor.execute(conn, query));
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
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");
|
String frogRunnerMode = System.getenv("FROG_RUNNER_MODE");
|
||||||
if (frogRunnerMode != null) {
|
if (frogRunnerMode != null) {
|
||||||
if (frogRunnerMode.equals("TERMINAL")) {
|
if (frogRunnerMode.equals("TERMINAL")) {
|
||||||
@@ -152,31 +202,8 @@ public class Main {
|
|||||||
String username = args[3];
|
String username = args[3];
|
||||||
String password = args[4];
|
String password = args[4];
|
||||||
|
|
||||||
@Cleanup
|
serveForever(host, port, sid, username, password);
|
||||||
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();
|
|
||||||
|
|
||||||
while ((line = br.readLine()) != null) {
|
|
||||||
if (!"--- END ---".equals(line)) {
|
|
||||||
commandBuffer.append(line).append("\n");
|
|
||||||
} else {
|
|
||||||
String query = commandBuffer.toString().trim();
|
|
||||||
String result;
|
|
||||||
if (query.toLowerCase().startsWith("select")) {
|
|
||||||
result = formatJsonObject(SqlExecutor.executeQuery(conn, query, null));
|
|
||||||
} else if (query.toLowerCase().startsWith("update") || query.toLowerCase().startsWith("insert")) {
|
|
||||||
result = formatJsonObject(SqlExecutor.executeUpdate(conn, query));
|
|
||||||
} else {
|
|
||||||
result = formatJsonObject(SqlExecutor.execute(conn, query));
|
|
||||||
}
|
|
||||||
System.out.println(result);
|
|
||||||
commandBuffer.setLength(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user