uk.org.retep.util.jdbc.JDBCUtils Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2007, Peter T Mount
* 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 the retep.org.uk 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 THE COPYRIGHT OWNER
* OR CONTRIBUTORS 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 uk.org.retep.util.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
/**
*
* @author peter
*/
public class JDBCUtils
{
private final DataSource dataSource;
public JDBCUtils( final DataSource dataSource )
{
this.dataSource = dataSource;
}
public DataSource getDataSource()
{
return dataSource;
}
public Connection getConnection()
throws SQLException
{
final Connection con = dataSource.getConnection();
con.setAutoCommit( false );
return con;
}
public List processResults( final ResultSetProcessor processor,
final Statement st,
final boolean firstResult )
throws SQLException
{
if( processor != null )
{
processor.init( st );
}
List result = null;
// Parse any result sets
int uc = 0;
boolean irs = firstResult;
do
{
if( irs )
{
final ResultSet rs = st.getResultSet();
try
{
if( processor != null )
{
processor.startResultSet( rs );
while( rs.next() )
{
T rec = processor.process( rs );
if( rec != null )
{
if( result == null )
{
result = new ArrayList();
}
result.add( rec );
}
}
processor.endResultSet( rs );
}
}
finally
{
rs.close();
}
}
else
{
uc = st.getUpdateCount();
}
irs = st.getMoreResults();
} while( !(irs == false && uc == -1) );
return result;
}
public void setParameters( final PreparedStatement ps,
final Object... parameters )
throws SQLException
{
if( parameters.length > 0 )
{
for( int i = 0; i <
parameters.length; i++ )
{
ps.setObject( i + 1, parameters[i] );
}
}
}
public void execute( final String sql,
final Object... parameters )
throws SQLException
{
final Connection con = dataSource.getConnection();
try
{
execute( con, null, sql, parameters );
con.commit();
}
finally
{
con.close();
}
}
public List execute( final ResultSetProcessor processor,
final String sql,
final Object... parameters )
throws SQLException
{
final Connection con = dataSource.getConnection();
try
{
final List result = execute( con, processor, sql, parameters );
con.commit();
return result;
}
finally
{
con.close();
}
}
public void execute( final Connection con,
final String sql,
final Object... parameters )
throws SQLException
{
execute( con, null, sql, parameters );
}
public List execute( final Connection con,
final ResultSetProcessor processor,
final String sql,
final Object... parameters )
throws SQLException
{
final PreparedStatement ps = con.prepareStatement( sql );
try
{
setParameters( ps, parameters );
return processResults( processor, ps, ps.execute() );
}
finally
{
ps.close();
}
}
private static final ResultSetProcessor INTEGER_PROCESSOR = new ResultSetProcessorAdaptor()
{
public Integer process( final ResultSet rs )
throws SQLException
{
return rs.getInt( 1 );
}
};
private static final ResultSetProcessor STRING_PROCESSOR = new ResultSetProcessorAdaptor()
{
public String process( final ResultSet rs )
throws SQLException
{
return rs.getString( 1 );
}
};
private static final ResultSetProcessor BOOLEAN_PROCESSOR = new ResultSetProcessorAdaptor()
{
public Boolean process( final ResultSet rs )
throws SQLException
{
return rs.getBoolean( 1 );
}
};
public static ResultSetProcessor getBooleanResultSetProcessor()
{
return BOOLEAN_PROCESSOR;
}
public static ResultSetProcessor getIntegerResultSetProcessor()
{
return INTEGER_PROCESSOR;
}
public static ResultSetProcessor getStringResultSetProcessor()
{
return STRING_PROCESSOR;
}
public static interface ResultSetProcessor
{
void init( final Statement st )
throws SQLException;
void startResultSet( ResultSet rs )
throws SQLException;
T process( ResultSet rs )
throws SQLException;
void endResultSet( ResultSet rs )
throws SQLException;
}
public static abstract class ResultSetProcessorAdaptor
implements ResultSetProcessor
{
public void init( final Statement st )
throws SQLException
{
}
public void startResultSet( final ResultSet rs )
throws SQLException
{
}
public void endResultSet( final ResultSet rs )
throws SQLException
{
}
}
}