author | František Kučera <franta-hg@frantovo.cz> |
Wed, 01 Jan 2014 02:44:29 +0100 | |
branch | v_0 |
changeset 107 | 8189a4a28cd8 |
parent 106 | e9c3583580c8 |
child 108 | d06d90b28217 |
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 |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
18 |
package info.globalcode.sql.dk; |
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
19 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
20 |
import info.globalcode.sql.dk.batch.Batch; |
27
24aa5199bfd6
DatabaseConnection: just change package
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
21 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
22 |
import info.globalcode.sql.dk.configuration.Properties; |
107
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
23 |
import info.globalcode.sql.dk.configuration.Property; |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
24 |
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
|
25 |
import info.globalcode.sql.dk.formatting.Formatter; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
26 |
import java.sql.Connection; |
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
27 |
import java.sql.DriverManager; |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
28 |
import java.sql.PreparedStatement; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
29 |
import java.sql.ResultSet; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
30 |
import java.sql.SQLException; |
86 | 31 |
import java.sql.SQLWarning; |
55 | 32 |
import java.util.logging.Level; |
33 |
import java.util.logging.Logger; |
|
26 | 34 |
|
35 |
/** |
|
36 |
* |
|
37 |
* @author Ing. František Kučera (frantovo.cz) |
|
38 |
*/ |
|
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
39 |
public class DatabaseConnection implements AutoCloseable { |
26 | 40 |
|
55 | 41 |
private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName()); |
26 | 42 |
private DatabaseDefinition databaseDefinition; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
43 |
private Connection connection; |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
44 |
private Properties properties; |
26 | 45 |
|
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
46 |
public DatabaseConnection(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException { |
26 | 47 |
this.databaseDefinition = databaseDefinition; |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
48 |
this.properties = properties; |
28
57c44a6baedb
DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
49 |
|
107
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
50 |
Properties credentials = new Properties(); |
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
51 |
credentials.add(new Property("user", databaseDefinition.getUserName())); |
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
52 |
credentials.add(new Property("password", databaseDefinition.getPassword())); |
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
53 |
credentials.setDefaults(databaseDefinition.getProperties()); |
8189a4a28cd8
database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents:
106
diff
changeset
|
54 |
properties.setDefaults(credentials); |
106
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
55 |
java.util.Properties javaProperties = properties.getJavaProperties(); |
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
56 |
|
e9c3583580c8
use database properties
František Kučera <franta-hg@frantovo.cz>
parents:
91
diff
changeset
|
57 |
connection = DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties); |
26 | 58 |
} |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
59 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
60 |
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
|
61 |
formatter.writeStartBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
62 |
formatter.writeStartDatabase(databaseDefinition); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
63 |
processCommand(sqlCommand, formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
64 |
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
|
65 |
formatter.writeEndBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
66 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
67 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
68 |
public void executeBatch(Batch batch, 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
|
69 |
formatter.writeStartBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
70 |
formatter.writeStartDatabase(databaseDefinition); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
71 |
while (batch.hasNext()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
72 |
processCommand(batch.next(), formatter); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
73 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
74 |
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
|
75 |
formatter.writeEndBatch(); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
76 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
77 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
78 |
private void processCommand(SQLCommand sqlCommand, Formatter formatter) throws SQLException { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
79 |
try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) { |
55 | 80 |
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
|
81 |
sqlCommand.parametrize(ps); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
82 |
|
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
83 |
boolean isRS = ps.execute(); |
55 | 84 |
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
|
85 |
if (isRS) { |
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
86 |
try (ResultSet rs = ps.getResultSet()) { |
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
|
87 |
processResultSet(sqlCommand, rs, formatter); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
88 |
} |
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
89 |
} else { |
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
|
90 |
processUpdateResult(sqlCommand, ps, formatter); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
91 |
} |
86 | 92 |
logWarnings(ps); |
35
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
93 |
|
b2ff3b2d58b2
accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
if (rs == null) { |
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
|
97 |
processUpdateResult(sqlCommand, ps, formatter); |
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
|
98 |
} else { |
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
|
99 |
processResultSet(sqlCommand, rs, formatter); |
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
|
100 |
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
|
101 |
} |
86 | 102 |
logWarnings(ps); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
103 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
104 |
} |
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
|
105 |
} |
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
|
106 |
|
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
|
107 |
private void processUpdateResult(SQLCommand sqlCommand, PreparedStatement ps, Formatter formatter) throws SQLException { |
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
|
108 |
formatter.writeStartUpdatesResult(); |
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
|
109 |
formatter.writeQuery(sqlCommand.getQuery()); |
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
|
110 |
formatter.writeParameters(sqlCommand.getParameters()); |
41
514df5061f59
No special formatting for generated keys. Use 'INSERT … RETURNING id' or 'INSERT … RETURNING *' to get generated keys.
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
111 |
formatter.writeUpdatedRowsCount(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
|
112 |
formatter.writeEndUpdatesResult(); |
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
|
113 |
} |
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
|
114 |
|
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
|
115 |
private void processResultSet(SQLCommand sqlCommand, ResultSet rs, Formatter formatter) throws SQLException { |
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
|
116 |
formatter.writeStartResultSet(); |
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
|
117 |
formatter.writeQuery(sqlCommand.getQuery()); |
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
|
118 |
formatter.writeParameters(sqlCommand.getParameters()); |
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
|
119 |
|
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
|
120 |
processResultSetRows(rs, formatter); |
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
121 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
122 |
formatter.writeEndResultSet(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
123 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
124 |
|
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 |
private void processResultSetRows(ResultSet rs, Formatter formatter) throws SQLException { |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
126 |
formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData())); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
127 |
int columnCount = rs.getMetaData().getColumnCount(); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
128 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
129 |
while (rs.next()) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
130 |
formatter.writeStartRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
131 |
|
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
132 |
for (int i = 1; i <= columnCount; i++) { |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
133 |
formatter.writeColumnValue(rs.getObject(i)); |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
134 |
} |
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
135 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
136 |
formatter.writeEndRow(); |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
137 |
} |
34
9335cf31c0f2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
138 |
|
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
139 |
} |
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
140 |
|
86 | 141 |
private void logWarnings(PreparedStatement ps) throws SQLException { |
142 |
SQLWarning w = ps.getWarnings(); |
|
143 |
while (w != null) { |
|
144 |
log.log(Level.WARNING, "SQL: {0}", w.getLocalizedMessage()); |
|
145 |
w = w.getNextWarning(); |
|
146 |
} |
|
147 |
ps.clearWarnings(); |
|
148 |
} |
|
149 |
||
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
150 |
/** |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
151 |
* 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
|
152 |
* |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
153 |
* @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
|
154 |
* @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
|
155 |
*/ |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
156 |
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
|
157 |
connection.getAutoCommit(); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
158 |
return true; |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
159 |
} |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
55
diff
changeset
|
160 |
|
42
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
161 |
@Override |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
162 |
public void close() throws SQLException { |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
163 |
connection.close(); |
6fdaa4db3943
DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents:
41
diff
changeset
|
164 |
} |
26 | 165 |
} |