|
10 | 10 | public class Database {
|
11 | 11 | public static ConfigDto configDto;
|
12 | 12 | private static Connection conn;
|
| 13 | + private static Statement stmt; |
13 | 14 |
|
14 | 15 | public static void init(ConfigDto dto) throws SQLException {
|
15 | 16 | configDto = dto;
|
16 | 17 | conn = DriverManager.getConnection(dto.getDbUrl(), dto.getDbUser(), dto.getDbPass());
|
| 18 | + stmt = conn.createStatement(); |
17 | 19 |
|
18 |
| - // inicia OWA |
19 |
| - owaInit(); |
20 |
| - } |
21 |
| - |
22 |
| - private static void owaInit() throws SQLException { |
23 |
| - Statement stmt = conn.createStatement(); |
24 |
| - stmt.executeQuery("declare\n" + |
| 20 | + // init OWA |
| 21 | + stmt.executeUpdate("declare\n" + |
25 | 22 | " nm owa.vc_arr;\n" +
|
26 | 23 | " vl owa.vc_arr;\n" +
|
27 | 24 | "begin\n" +
|
28 | 25 | " nm(1) := 'SERVER_PORT';\n" +
|
29 |
| - " vl(1) := '80';\n" + |
| 26 | + " vl(1) := '8000';\n" + |
30 | 27 | " owa.init_cgi_env( 1, nm, vl );\n" +
|
31 | 28 | "end;");
|
32 |
| - stmt.close(); |
| 29 | + |
| 30 | + // init DBMS |
| 31 | + stmt.executeUpdate("begin dbms_output.enable(); end;"); |
33 | 32 | }
|
34 | 33 |
|
35 | 34 | public static String runPl(String plName, Map<String, String> param) throws SQLException {
|
@@ -64,19 +63,26 @@ public static String runPl(String plName, Map<String, String> param) throws SQLE
|
64 | 63 |
|
65 | 64 | log.info("QUERY:\n{}", sql.toString());
|
66 | 65 |
|
67 |
| - Statement stmt = conn.createStatement(); |
68 |
| - stmt.executeQuery(sql.toString()); |
| 66 | + stmt.executeUpdate(sql.toString()); |
69 | 67 |
|
70 | 68 | log.info("GET RESULT");
|
71 | 69 | return getResult();
|
72 | 70 | }
|
73 | 71 |
|
74 | 72 | private static String getResult() throws SQLException {
|
75 | 73 | StringBuilder result = new StringBuilder();
|
76 |
| - String buff; |
77 | 74 |
|
78 |
| - while ((buff = getChunkResult()) != null){ |
79 |
| - result.append(buff); |
| 75 | + try { |
| 76 | + // get buffer |
| 77 | + String buff; |
| 78 | + |
| 79 | + while ((buff = getChunkResult()) != null) { |
| 80 | + result.append(buff); |
| 81 | + } |
| 82 | + }catch (SQLException e){ |
| 83 | + log.error(e.getMessage()); |
| 84 | + }finally { |
| 85 | + printDbmsOutput(); |
80 | 86 | }
|
81 | 87 |
|
82 | 88 | return result.toString();
|
@@ -104,9 +110,30 @@ private static String getChunkResult() throws SQLException {
|
104 | 110 | cs.setInt(1, 127);
|
105 | 111 | cs.registerOutParameter(2, Types.VARCHAR);
|
106 | 112 | cs.registerOutParameter(3, Types.BIGINT);
|
| 113 | + cs.execute(); |
| 114 | + |
| 115 | + String result = cs.getString(2); |
| 116 | + |
| 117 | + cs.close(); |
107 | 118 |
|
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + private static void printDbmsOutput() throws SQLException { |
| 123 | + String query = "declare\n" + |
| 124 | + " num integer := 1000;\n" + |
| 125 | + "begin\n" + |
| 126 | + " dbms_output.get_lines(?, num);\n" + |
| 127 | + "end;"; |
| 128 | + CallableStatement cs = conn.prepareCall(query); |
| 129 | + cs.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY"); |
108 | 130 | cs.execute();
|
109 | 131 |
|
110 |
| - return cs.getString(2); |
| 132 | + String[] listLog = (String[]) cs.getArray(1).getArray(); |
| 133 | + cs.close(); |
| 134 | + |
| 135 | + for(String item : listLog){ |
| 136 | + log.info("DBMSOUTPUT: {}", item); |
| 137 | + } |
111 | 138 | }
|
112 | 139 | }
|
0 commit comments