Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2011, 2013, Jonathan Giles, Johan Vos, Hendrik Ebbers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of DataFX, the website javafxdata.org, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.datafx.reader;
import org.datafx.reader.converter.JdbcConverter;
import org.datafx.reader.converter.JdbcDataSourceUtil;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author johan
*/
public class JdbcSource extends AbstractDataReader implements WritableDataReader {
private QueryHandler handler;
private JdbcConnectionFactory connectionFactory;
public JdbcSource(String jdbcUrl, JdbcConverter converter, String table, String... cols) {
this(jdbcUrl, JdbcDataSourceUtil.createSelectStatement(table, cols), converter);
}
public JdbcSource(final String jdbcUrl, String selectStatement,
JdbcConverter converter) {
this(new JdbcConnectionFactory() {
@Override
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(jdbcUrl);
}
}, selectStatement, converter);
}
public JdbcSource(Connection connection, JdbcConverter converter, String table, String... cols) {
this(connection, JdbcDataSourceUtil.createSelectStatement(table, cols), converter);
}
public JdbcSource(final Connection connection, String selectStatement,
JdbcConverter converter) {
this(new JdbcConnectionFactory() {
@Override
public Connection getConnection() throws SQLException {
return connection;
}
}, selectStatement, converter);
}
private JdbcSource(JdbcConnectionFactory connectionFactory, String selectStatement,
JdbcConverter converter) {
this(connectionFactory, selectStatement, converter, false);
}
private JdbcSource(JdbcConnectionFactory connectionFactory, final String selectStatement,
final JdbcConverter converter, final boolean updateQuery) {
this(connectionFactory, new QueryHandler() {
boolean connectionCreated;
private JdbcConverter converter;
@Override
public JdbcConverter handleQuery(JdbcConnectionFactory jdbcConnectionFactory) throws SQLException {
if (converter == null) {
Connection connection = jdbcConnectionFactory.getConnection();
connectionCreated = true;
if (updateQuery) {
Statement query = connection.createStatement();
query.executeUpdate(selectStatement);
} else {
Statement query = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = query.executeQuery(selectStatement);
boolean lastResult = resultSet.next();
converter.initialize(resultSet);
}
}
return converter;
}
@Override
public boolean isConnectionCreated() {
return connectionCreated;
}
});
}
private JdbcSource(JdbcConnectionFactory connectionFactory, QueryHandler handler) {
this.connectionFactory = connectionFactory;
this.handler = handler;
}
private synchronized JdbcConverter execute() {
try {
return handler.handleQuery(connectionFactory);
} catch (Exception e) {
Logger.getLogger(JdbcSource.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@Override
public void writeBack() {
execute();
}
@Override
public T get() {
JdbcConverter converter = execute();
if (converter != null) {
return converter.get();
} else {
return null;
}
}
@Override
public boolean next() {
JdbcConverter converter = execute();
boolean answer = converter.next();
return answer;
}
private interface QueryHandler {
JdbcConverter handleQuery(JdbcConnectionFactory jdbcConnectionFactory) throws SQLException;
boolean isConnectionCreated();
}
private interface JdbcConnectionFactory {
Connection getConnection() throws SQLException;
}
}