
org.apache.ctakes.jdl.data.base.JdlConnection Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ctakes.jdl.data.base;
import org.apache.ctakes.jdl.schema.xdl.JdbcType;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Connection with db.
*
* @author mas
*/
public class JdlConnection {
private String driver;
private String url;
private String user;
private String password;
private Connection connection;
/**
* @param jdbc
* the jdbc
*/
public JdlConnection(final JdbcType jdbc) {
driver = defaultIfEmpty(jdbc.getDriver(), driver);
url = defaultIfEmpty(jdbc.getUrl(), url);
user = defaultIfEmpty(jdbc.getUsername(), user);
password = defaultIfEmpty(jdbc.getPassword(), password);
}
static private String defaultIfEmpty( final String attempted, final String defaulted ) {
if ( attempted != null && !attempted.isEmpty() ) {
return attempted;
}
return defaulted;
}
/**
* @return the connected
* @throws SQLException
* exception
*/
public final boolean isConnected() throws SQLException {
return (connection != null && !connection.isClosed());
}
/**
* Attempts to establish a connection.
*
* @throws InstantiationException
* exception
* @throws IllegalAccessException
* exception
* @throws ClassNotFoundException
* exception
* @throws SQLException
* exception
*/
private void openConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
// Class.forName(driver);
try {
DriverManager.registerDriver( (Driver)Class.forName( driver ).getDeclaredConstructor().newInstance() );
} catch ( NoSuchMethodException | InvocationTargetException multE ) {
throw new SQLException( multE );
}
connection = DriverManager.getConnection(url, user, password);
}
/**
* @return the openConnection
* @throws InstantiationException
* exception
* @throws IllegalAccessException
* exception
* @throws ClassNotFoundException
* exception
* @throws SQLException
* exception
*/
public final Connection getOpenConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
if (!isConnected()) {
openConnection();
}
return connection;
}
/**
* Makes all changes made since the previous commit/rollback permanent and
* releases any database locks currently held by this Connection object.
*
* @throws SQLException
* exception
*/
public final void commitConnection() throws SQLException {
if (isConnected() && !connection.getAutoCommit()) {
connection.commit();
}
}
/**
* Undoes all changes made in the current transaction and releases any
* database locks currently held by this Connection object.
*
* @throws SQLException
* exception
*/
public final void rollbackConnection() throws SQLException {
if (isConnected() && !connection.getAutoCommit()) {
connection.rollback();
}
}
/**
* Releases this Connection object's database.
*
* @throws SQLException
* exception
*/
public final void closeConnection() throws SQLException {
if (isConnected()) {
connection.close();
}
}
/**
* @return the autoCommit
* @throws SQLException
* exception
*/
public final boolean isAutoCommit() throws SQLException {
return (isConnected()) ? connection.getAutoCommit() : false;
}
/**
* @param autoCommit
* the autoCommit to set
* @throws SQLException
* exception
*/
public final void setAutoCommit(final boolean autoCommit) throws SQLException {
if (isConnected()) {
connection.setAutoCommit(autoCommit);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy