author | František Kučera <franta-hg@frantovo.cz> |
Thu, 24 Oct 2019 21:43:08 +0200 | |
branch | v_0 |
changeset 250 | aae5009bd0af |
parent 238 | 4a1864c3e867 |
child 252 | a9d4a8d5c57f |
permissions | -rw-r--r-- |
26 | 1 |
/** |
2 |
* SQL-DK |
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
250
aae5009bd0af
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
238
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
26 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
17 |
package info.globalcode.sql.dk; |
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
18 |
|
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
19 |
import static info.globalcode.sql.dk.jmx.ConnectionManagement.incrementCounter; |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
20 |
import static info.globalcode.sql.dk.jmx.ConnectionManagement.resetCounter; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
21 |
import info.globalcode.sql.dk.batch.Batch; |
146
4f4f515df807
BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
22 |
import info.globalcode.sql.dk.batch.BatchException; |
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
23 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
192
a32bfcbdee51
jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents:
179
diff
changeset
|
24 |
import info.globalcode.sql.dk.configuration.Loader; |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
25 |
import info.globalcode.sql.dk.configuration.Properties; |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
26 |
import info.globalcode.sql.dk.formatting.ColumnsHeader; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
27 |
import info.globalcode.sql.dk.formatting.Formatter; |
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
28 |
import info.globalcode.sql.dk.jmx.ConnectionManagement; |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
29 |
import info.globalcode.sql.dk.jmx.ConnectionManagement.COUNTER; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
30 |
import java.sql.Connection; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
31 |
import java.sql.PreparedStatement; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
32 |
import java.sql.ResultSet; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
33 |
import java.sql.SQLException; |
86 | 34 |
import java.sql.SQLWarning; |
55 | 35 |
import java.util.logging.Level; |
36 |
import java.util.logging.Logger; |
|
26 | 37 |
|
38 |
/** |
|
155 | 39 |
* Represents connected database. Is derived from {@linkplain DatabaseDefinition}. |
40 |
* Wraps {@linkplain Connection}. |
|
41 |
* |
|
42 |
* Is responsible for executing {@linkplain SQLCommand} and passing results to the |
|
43 |
* {@linkplain Formatter}. |
|
26 | 44 |
* |
45 |
* @author Ing. František Kučera (frantovo.cz) |
|
46 |
*/ |
|
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
47 |
public class DatabaseConnection implements AutoCloseable { |
26 | 48 |
|
55 | 49 |
private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName()); |
192
a32bfcbdee51
jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents:
179
diff
changeset
|
50 |
public static final String JDBC_PROPERTY_USER = "user"; |
108
d06d90b28217
DB credentials can be CLI options + log warning: insecure
František Kučera <franta-hg@frantovo.cz>
parents:
107
diff
changeset
|
51 |
public static final String JDBC_PROPERTY_PASSWORD = "password"; |
178 | 52 |
private final DatabaseDefinition databaseDefinition; |
53 |
private final Connection connection; |
|
54 |
private final Properties properties; |
|
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
55 |
/** |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
56 |
* Could be null = JMX is disabled → must check, see functions in |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
57 |
* {@linkplain ConnectionManagement} |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
58 |
*/ |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
59 |
private final ConnectionManagement connectionMBean; |
26 | 60 |
|
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
61 |
/** |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
62 |
* |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
63 |
* @param databaseDefinition DB url, name, password etc. |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
64 |
* @param properties additional properties from CLI |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
65 |
* @param connectionMBean JMX management bean | null = disabled JMX reporting |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
66 |
* @throws SQLException |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
67 |
*/ |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
68 |
public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties, ConnectionManagement connectionMBean) throws SQLException { |
26 | 69 |
this.databaseDefinition = databaseDefinition; |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
70 |
this.properties = properties; |
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
71 |
this.connectionMBean = connectionMBean; |
192
a32bfcbdee51
jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents:
179
diff
changeset
|
72 |
this.connection = Loader.jdbcConnect(databaseDefinition, properties); |
26 | 73 |
} |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
74 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
75 |
public void executeQuery(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
91
43e8d52091d5
Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents:
86
diff
changeset
|
76 |
formatter.writeStartBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
77 |
formatter.writeStartDatabase(databaseDefinition); |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
78 |
formatter.writeStartStatement(); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
79 |
formatter.writeQuery(sqlCommand.getQuery()); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
80 |
formatter.writeParameters(sqlCommand.getParameters()); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
81 |
processCommand(sqlCommand, formatter); |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
82 |
formatter.writeEndStatement(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
83 |
formatter.writeEndDatabase(); |
91
43e8d52091d5
Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents:
86
diff
changeset
|
84 |
formatter.writeEndBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
85 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
86 |
|
146
4f4f515df807
BatchDecoder: basic decoder
František Kučera <franta-hg@frantovo.cz>
parents:
142
diff
changeset
|
87 |
public void executeBatch(Batch batch, Formatter formatter) throws SQLException, BatchException { |
91
43e8d52091d5
Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents:
86
diff
changeset
|
88 |
formatter.writeStartBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
89 |
formatter.writeStartDatabase(databaseDefinition); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
90 |
while (batch.hasNext()) { |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
91 |
SQLCommand sqlCommand = batch.next(); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
92 |
formatter.writeStartStatement(); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
93 |
formatter.writeQuery(sqlCommand.getQuery()); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
94 |
formatter.writeParameters(sqlCommand.getParameters()); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
95 |
processCommand(sqlCommand, formatter); |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
96 |
formatter.writeEndStatement(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
97 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
98 |
formatter.writeEndDatabase(); |
91
43e8d52091d5
Formatter: one more level: writeStartBatch() + writeEndBatch() which allows multiple databases on output
František Kučera <franta-hg@frantovo.cz>
parents:
86
diff
changeset
|
99 |
formatter.writeEndBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
100 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
101 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
102 |
private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
103 |
incrementCounter(connectionMBean, COUNTER.COMMAND); |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
104 |
resetCounter(connectionMBean, COUNTER.RECORD_CURRENT); |
192
a32bfcbdee51
jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents:
179
diff
changeset
|
105 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
106 |
try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) { |
55 | 107 |
log.log(Level.FINE, "Statement prepared"); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
108 |
sqlCommand.parametrize(ps); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
109 |
|
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
110 |
boolean isRS = ps.execute(); |
55 | 111 |
log.log(Level.FINE, "Statement executed"); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
112 |
if (isRS) { |
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
113 |
try (ResultSet rs = ps.getResultSet()) { |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
114 |
processResultSet(rs, formatter); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
115 |
} |
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
116 |
} else { |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
117 |
processUpdateResult(ps, formatter); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
118 |
} |
86 | 119 |
logWarnings(ps); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
120 |
|
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
121 |
while (ps.getMoreResults() || ps.getUpdateCount() > -1) { |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
122 |
ResultSet rs = ps.getResultSet(); |
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
123 |
if (rs == null) { |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
124 |
processUpdateResult(ps, formatter); |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
125 |
} else { |
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
126 |
processResultSet(rs, formatter); |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
127 |
rs.close(); |
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
128 |
} |
86 | 129 |
logWarnings(ps); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
130 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
131 |
} |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
132 |
} |
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
133 |
|
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
134 |
private void processUpdateResult(PreparedStatement ps, Formatter formatter) throws SQLException { |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
135 |
formatter.writeUpdatesResult(ps.getUpdateCount()); |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
136 |
} |
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
137 |
|
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
138 |
private void processResultSet(ResultSet rs, Formatter formatter) throws SQLException { |
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
139 |
formatter.writeStartResultSet(new ColumnsHeader(rs.getMetaData())); |
37
9e6f8e5d5f98
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents:
35
diff
changeset
|
140 |
|
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
141 |
int columnCount = rs.getMetaData().getColumnCount(); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
142 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
143 |
while (rs.next()) { |
179
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
144 |
incrementCounter(connectionMBean, COUNTER.RECORD_CURRENT); |
236332caeb29
Basic JMX management/reporting – counters for commands and records
František Kučera <franta-hg@frantovo.cz>
parents:
178
diff
changeset
|
145 |
incrementCounter(connectionMBean, COUNTER.RECORD_TOTAL); |
192
a32bfcbdee51
jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents:
179
diff
changeset
|
146 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
147 |
formatter.writeStartRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
148 |
|
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
149 |
for (int i = 1; i <= columnCount; i++) { |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
150 |
formatter.writeColumnValue(rs.getObject(i)); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
151 |
} |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
152 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
153 |
formatter.writeEndRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
154 |
} |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
155 |
|
142
da1e38386d84
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
František Kučera <franta-hg@frantovo.cz>
parents:
108
diff
changeset
|
156 |
formatter.writeEndResultSet(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
157 |
} |
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
158 |
|
86 | 159 |
private void logWarnings(PreparedStatement ps) throws SQLException { |
160 |
SQLWarning w = ps.getWarnings(); |
|
161 |
while (w != null) { |
|
162 |
log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage()); |
|
163 |
w = w.getNextWarning(); |
|
164 |
} |
|
165 |
ps.clearWarnings(); |
|
166 |
} |
|
167 |
||
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
168 |
/** |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
169 |
* Tests if this connection is live. |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
170 |
* |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
171 |
* @return true if test was successful |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
172 |
* @throws SQLException if test fails |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
173 |
*/ |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
174 |
public boolean test() throws SQLException { |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
175 |
connection.getAutoCommit(); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
176 |
return true; |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
177 |
} |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
178 |
|
229
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
179 |
public String getProductName() throws SQLException { |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
180 |
return connection.getMetaData().getDatabaseProductName(); |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
181 |
} |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
182 |
|
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
183 |
public String getProductVersion() throws SQLException { |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
184 |
return connection.getMetaData().getDatabaseProductVersion(); |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
185 |
} |
7699133f5a01
InfoLister/test: show also database server name and version
František Kučera <franta-hg@frantovo.cz>
parents:
192
diff
changeset
|
186 |
|
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
187 |
@Override |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
188 |
public void close() throws SQLException { |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
189 |
connection.close(); |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
190 |
} |
26 | 191 |
} |