Example usage for javax.sql.rowset CachedRowSet populate

List of usage examples for javax.sql.rowset CachedRowSet populate

Introduction

In this page you can find the example usage for javax.sql.rowset CachedRowSet populate.

Prototype

public void populate(ResultSet data) throws SQLException;

Source Link

Document

Populates this CachedRowSet object with data from the given ResultSet object.

Usage

From source file:uk.ac.ox.it.ords.api.database.structure.services.impl.hibernate.StructureServiceImpl.java

protected CachedRowSet runJDBCQuery(String query, List<Object> parameters, String server, int port,
        String databaseName, String userName, String password) throws Exception {
    Connection connection = null;
    Properties connectionProperties = new Properties();
    PreparedStatement preparedStatement = null;

    connectionProperties.put("user", userName);
    connectionProperties.put("password", password);

    String connectionURL = "jdbc:postgresql://" + server + ":" + port + "/" + databaseName;

    try {/* w  w w.j  a v  a 2 s.c o  m*/
        connection = DriverManager.getConnection(connectionURL, connectionProperties);
        preparedStatement = connection.prepareStatement(query);
        if (parameters != null) {
            int paramCount = 1;
            for (Object parameter : parameters) {
                @SuppressWarnings("rawtypes")
                Class type = parameter.getClass();
                if (type.equals(String.class)) {
                    preparedStatement.setString(paramCount, (String) parameter);
                }
                if (type.equals(Integer.class)) {
                    preparedStatement.setInt(paramCount, (Integer) parameter);
                }
                paramCount++;
            }

        }
        if (query.toLowerCase().startsWith("select")) {
            ResultSet result = preparedStatement.executeQuery();
            CachedRowSet rowSet = RowSetProvider.newFactory().createCachedRowSet();
            rowSet.populate(result);
            log.debug("prepareAndExecuteStatement:return result");
            return rowSet;
        } else {
            preparedStatement.execute();
            log.debug("prepareAndExecuteStatement:return null");
            return null;
        }

    } catch (SQLException e) {
        log.error("Error with this command", e);
        log.error("Query:" + query);
        throw e;
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

}