Skip to content

Commit afdab2d

Browse files
committed
dbms_output
1 parent 48c87aa commit afdab2d

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CREATE OR REPLACE PROCEDURE HELLO_WORLD (
1717
) IS
1818
BEGIN
1919
HTP.PRINT('Hello World');
20+
DBMS_OUTPUT.PUT_LINE('Test DBMS_OUTPUT');
2021
END;
2122
```
2223

src/main/java/com/github/clagomess/modplsql/jdbc/Database.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,25 @@
1010
public class Database {
1111
public static ConfigDto configDto;
1212
private static Connection conn;
13+
private static Statement stmt;
1314

1415
public static void init(ConfigDto dto) throws SQLException {
1516
configDto = dto;
1617
conn = DriverManager.getConnection(dto.getDbUrl(), dto.getDbUser(), dto.getDbPass());
18+
stmt = conn.createStatement();
1719

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" +
2522
" nm owa.vc_arr;\n" +
2623
" vl owa.vc_arr;\n" +
2724
"begin\n" +
2825
" nm(1) := 'SERVER_PORT';\n" +
29-
" vl(1) := '80';\n" +
26+
" vl(1) := '8000';\n" +
3027
" owa.init_cgi_env( 1, nm, vl );\n" +
3128
"end;");
32-
stmt.close();
29+
30+
// init DBMS
31+
stmt.executeUpdate("begin dbms_output.enable(); end;");
3332
}
3433

3534
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
6463

6564
log.info("QUERY:\n{}", sql.toString());
6665

67-
Statement stmt = conn.createStatement();
68-
stmt.executeQuery(sql.toString());
66+
stmt.executeUpdate(sql.toString());
6967

7068
log.info("GET RESULT");
7169
return getResult();
7270
}
7371

7472
private static String getResult() throws SQLException {
7573
StringBuilder result = new StringBuilder();
76-
String buff;
7774

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();
8086
}
8187

8288
return result.toString();
@@ -104,9 +110,30 @@ private static String getChunkResult() throws SQLException {
104110
cs.setInt(1, 127);
105111
cs.registerOutParameter(2, Types.VARCHAR);
106112
cs.registerOutParameter(3, Types.BIGINT);
113+
cs.execute();
114+
115+
String result = cs.getString(2);
116+
117+
cs.close();
107118

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");
108130
cs.execute();
109131

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+
}
111138
}
112139
}

src/main/resources/log4j.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<appender name="console" class="org.apache.log4j.ConsoleAppender">
77
<param name="Target" value="System.out" />
88
<layout class="org.apache.log4j.PatternLayout">
9-
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n" />
9+
<param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n" />
1010
</layout>
1111
</appender>
1212

0 commit comments

Comments
 (0)