java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 01:24:21 +0100
branchv_0
changeset 65 f05be87239ad
parent 55 f5ed7c4efacc
child 86 6b0eb3b22eb8
permissions -rw-r--r--
option --test-connection – tests connection to given database
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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;
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    22
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
    23
import info.globalcode.sql.dk.formatting.Formatter;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    24
import java.sql.Connection;
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    25
import java.sql.DriverManager;
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    26
import java.sql.PreparedStatement;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    27
import java.sql.ResultSet;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    28
import java.sql.SQLException;
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    29
import java.util.logging.Level;
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    30
import java.util.logging.Logger;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
/**
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 *
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 * @author Ing. František Kučera (frantovo.cz)
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 */
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
    36
public class DatabaseConnection implements AutoCloseable {
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    38
	private static final Logger log = Logger.getLogger(DatabaseConnection.class.getName());
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	private DatabaseDefinition databaseDefinition;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    40
	private Connection connection;
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    42
	public DatabaseConnection(DatabaseDefinition databaseDefinition) throws SQLException {
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		this.databaseDefinition = databaseDefinition;
28
57c44a6baedb DatabaseConnection: connect JDBC
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    44
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    45
		connection = DriverManager.getConnection(databaseDefinition.getUrl(), databaseDefinition.getUserName(), databaseDefinition.getPassword());
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    47
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    48
	public void executeQuery(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
    49
		formatter.writeStartDatabase(databaseDefinition);
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    50
		processCommand(sqlCommand, formatter);
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    51
		formatter.writeEndDatabase();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    52
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    53
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    54
	public void executeBatch(Batch batch, Formatter formatter) throws SQLException {
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    55
		formatter.writeStartDatabase(databaseDefinition);
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    56
		while (batch.hasNext()) {
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    57
			processCommand(batch.next(), formatter);
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    58
		}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    59
		formatter.writeEndDatabase();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    60
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    61
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    62
	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
    63
		try (PreparedStatement ps = sqlCommand.prepareStatement(connection)) {
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    64
			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
    65
			sqlCommand.parametrize(ps);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    66
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    67
			boolean isRS = ps.execute();
55
f5ed7c4efacc colorful logging
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    68
			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
    69
			if (isRS) {
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    70
				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
    71
					processResultSet(sqlCommand, rs, formatter);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    72
				}
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    73
			} 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
    74
				processUpdateResult(sqlCommand, ps, formatter);
35
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    75
			}
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    76
b2ff3b2d58b2 accept SQL commands returning more ResultSets
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    77
			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
    78
				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
    79
				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
    80
					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
    81
				} 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
    82
					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
    83
					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
    84
				}
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
		}
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
	}
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
    88
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
    89
	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
    90
		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
    91
		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
    92
		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
    93
		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
    94
		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
    95
	}
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
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
	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
    98
		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
    99
		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
   100
		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
   101
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
   102
		processResultSetRows(rs, formatter);
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
		formatter.writeEndResultSet();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   105
	}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   106
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
   107
	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
   108
		formatter.writeColumnsHeader(new ColumnsHeader(rs.getMetaData()));
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   109
		int columnCount = rs.getMetaData().getColumnCount();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   110
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   111
		while (rs.next()) {
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   112
			formatter.writeStartRow();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   113
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
			for (int i = 1; i <= columnCount; i++) {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   115
				formatter.writeColumnValue(rs.getObject(i));
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   116
			}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   117
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   118
			formatter.writeEndRow();
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   119
		}
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   120
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   121
	}
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   122
65
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   123
	/**
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   124
	 * 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
   125
	 *
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   126
	 * @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
   127
	 * @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
   128
	 */
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   129
	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
   130
		connection.getAutoCommit();
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   131
		return true;
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   132
	}
f05be87239ad option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents: 55
diff changeset
   133
42
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   134
	@Override
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   135
	public void close() throws SQLException {
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   136
		connection.close();
6fdaa4db3943 DatabaseConnection implements AutoCloseable
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   137
	}
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
}