Example usage for java.sql PreparedStatement getClass

List of usage examples for java.sql PreparedStatement getClass

Introduction

In this page you can find the example usage for java.sql PreparedStatement getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.springsource.insight.plugin.jdbc.PoolingConnectionTest.java

@Test
public void testOperationCollection() throws SQLException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(dataSource);
    ObjectPool connPool = new GenericObjectPool();
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, connPool, null, null,
            false, true);//from  www . j  av  a2s  . c  o m
    PoolingDataSource poolDs = new PoolingDataSource(poolFactory.getPool());
    String sql = "select * from appointment where owner = 'Agim'";
    Connection c = poolDs.getConnection();
    try {
        PreparedStatement ps = c.prepareStatement(sql);
        try {
            System.out.println("Prepared statement=" + ps.getClass());

            ResultSet rs = ps.executeQuery();
            rs.close();
        } finally {
            ps.close();
        }
    } finally {
        c.close();
    }

    ArgumentCaptor<Operation> opCaptor = ArgumentCaptor.forClass(Operation.class);
    verify(spiedOperationCollector, times(3)).enter(opCaptor.capture());

    List<Operation> ops = opCaptor.getAllValues();
    assertEquals("Mismatched number of operations", 3, ops.size());
    assertSourceCodeLocation("top-op", ops.get(0),
            "org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper", "prepareStatement");
    assertSourceCodeLocation("mid-op", ops.get(1), "org.apache.commons.dbcp.DelegatingConnection",
            "prepareStatement");
    assertSourceCodeLocation("bottom-op", ops.get(2), "org.hsqldb.jdbc.jdbcConnection", "prepareStatement");
}

From source file:org.apache.ctakes.ytex.kernel.KernelUtilImpl.java

/**
 * this can be very large - avoid loading the entire jdbc ResultSet into
 * memory//from   w  w  w  .  j  a  v a 2 s. c om
 */
@Override
public InstanceData loadInstances(String strQuery) {
    final InstanceData instanceLabel = new InstanceData();
    PreparedStatement s = null;
    Connection conn = null;
    ResultSet rs = null;
    try {
        // jdbcTemplate.query(strQuery, new RowCallbackHandler() {
        RowCallbackHandler ch = new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                String label = "";
                int run = 0;
                int fold = 0;
                boolean train = true;
                long instanceId = rs.getLong(1);
                String className = rs.getString(2);
                if (rs.getMetaData().getColumnCount() >= 3)
                    train = rs.getBoolean(3);
                if (rs.getMetaData().getColumnCount() >= 4) {
                    label = rs.getString(4);
                    if (label == null)
                        label = "";
                }
                if (rs.getMetaData().getColumnCount() >= 5)
                    fold = rs.getInt(5);
                if (rs.getMetaData().getColumnCount() >= 6)
                    run = rs.getInt(6);
                // get runs for label
                SortedMap<Integer, SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>> runToInstanceMap = instanceLabel
                        .getLabelToInstanceMap().get(label);
                if (runToInstanceMap == null) {
                    runToInstanceMap = new TreeMap<Integer, SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>>();
                    instanceLabel.getLabelToInstanceMap().put(label, runToInstanceMap);
                }
                // get folds for run
                SortedMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>> foldToInstanceMap = runToInstanceMap
                        .get(run);
                if (foldToInstanceMap == null) {
                    foldToInstanceMap = new TreeMap<Integer, SortedMap<Boolean, SortedMap<Long, String>>>();
                    runToInstanceMap.put(run, foldToInstanceMap);
                }
                // get train/test set for fold
                SortedMap<Boolean, SortedMap<Long, String>> ttToClassMap = foldToInstanceMap.get(fold);
                if (ttToClassMap == null) {
                    ttToClassMap = new TreeMap<Boolean, SortedMap<Long, String>>();
                    foldToInstanceMap.put(fold, ttToClassMap);
                }
                // get instances for train/test set
                SortedMap<Long, String> instanceToClassMap = ttToClassMap.get(train);
                if (instanceToClassMap == null) {
                    instanceToClassMap = new TreeMap<Long, String>();
                    ttToClassMap.put(train, instanceToClassMap);
                }
                // set the instance class
                instanceToClassMap.put(instanceId, className);
                // add the class to the labelToClassMap
                SortedSet<String> labelClasses = instanceLabel.getLabelToClassMap().get(label);
                if (labelClasses == null) {
                    labelClasses = new TreeSet<String>();
                    instanceLabel.getLabelToClassMap().put(label, labelClasses);
                }
                if (!labelClasses.contains(className))
                    labelClasses.add(className);
            }
        };
        conn = this.jdbcTemplate.getDataSource().getConnection();
        s = conn.prepareStatement(strQuery, java.sql.ResultSet.TYPE_FORWARD_ONLY,
                java.sql.ResultSet.CONCUR_READ_ONLY);
        if ("MySQL".equals(conn.getMetaData().getDatabaseProductName())) {
            s.setFetchSize(Integer.MIN_VALUE);
        } else if (s.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerStatement")) {
            try {
                BeanUtils.setProperty(s, "responseBuffering", "adaptive");
            } catch (IllegalAccessException e) {
                log.warn("error setting responseBuffering", e);
            } catch (InvocationTargetException e) {
                log.warn("error setting responseBuffering", e);
            }
        }
        rs = s.executeQuery();
        while (rs.next()) {
            ch.processRow(rs);
        }
    } catch (SQLException j) {
        log.error("loadInstances failed", j);
        throw new RuntimeException(j);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return instanceLabel;
}

From source file:org.athenasource.framework.unidbi.Datatypes.java

/**
 * Setting the parameter for the given prepared statement. This method will
 * cast the value to the object expected type (execept BOOLEAN) as
 * {@linkplain #getClass(int)}./*from   w w  w.  java 2  s  .  com*/
 * <p>INSERT, UPDATE should always call this method to set parameters before 'WHERE' keyword.</p>
 * <p><b>Warining</b>: For parameters after 'WHERE' keyword, always remember 'IS NULL' is not equal to '= NULL'.</p>
 *
 * @param index
 *            the parameter index
 * @param value
 *            the value for the parameter, can be <code>null</code>.
 * @param unidbType
 *            the datatype of the parameter
 * @throws SQLException
 *             in case of SQL problems or type conversion fails.
 */
public static void setParameter(PreparedStatement stmt, int index, Object value, int unidbType)
        throws SQLException {
    // Derby needs special handling.
    boolean isDerby = (stmt instanceof DelegatingPreparedStatement)
            ? ((DelegatingPreparedStatement) stmt).getDelegate().getClass().getName().contains("derby")
            : stmt.getClass().getName().contains("derby");
    if (value == null) {
        if (isDerby) {
            if (unidbType == NCHAR) {
                stmt.setNull(index, Datatypes.getSQLType(CHAR));
            } else if (unidbType == NVARCHAR) {
                stmt.setNull(index, Datatypes.getSQLType(VARCHAR));
            } else {
                stmt.setNull(index, Datatypes.getSQLType(unidbType));
            }
        } else {
            stmt.setNull(index, Datatypes.getSQLType(unidbType));
        }
    } else {
        try {
            switch (unidbType) {
            case BOOLEAN:
                stmt.setByte(index, (byte) (((Number) value).intValue() == 1 ? 1 : 0));
                break;
            case TINYINT:
                stmt.setByte(index, ((Number) value).byteValue());
                break;
            case SMALLINT:
                stmt.setShort(index, ((Number) value).shortValue());
                break;
            case INTEGER:
                stmt.setInt(index, ((Number) value).intValue());
                break;
            case BIGINT:
                stmt.setLong(index, ((Number) value).longValue());
                break;
            case DECIMAL:
                stmt.setBigDecimal(index, ((BigDecimal) value));
                break;
            case REAL:
                stmt.setFloat(index, ((Float) value).floatValue());
                break;
            case DOUBLE:
                stmt.setDouble(index, ((Double) value).doubleValue());
                break;
            case CHAR:
                stmt.setString(index, (String) value);
                break;
            case NCHAR:
                if (isDerby) {
                    stmt.setString(index, (String) value);
                } else {
                    stmt.setNString(index, (String) value);
                }
                break;
            case VARCHAR:
                stmt.setString(index, (String) value);
                break;
            case NVARCHAR:
                if (isDerby) {
                    stmt.setString(index, (String) value);
                } else {
                    stmt.setNString(index, (String) value);
                }
                break;
            case CLOB: // Clob/NClob can be represented as String without any problem. - Oct 16, 2008.
                stmt.setString(index, (String) value); // stmt.setClob(index, ((Clob) value));
                break;
            case NCLOB:
                if (isDerby) {
                    stmt.setString(index, (String) value);
                } else {
                    stmt.setNString(index, (String) value); // stmt.setNClob(index, ((NClob) value));
                }
                break;
            case BLOB:
                stmt.setBlob(index, ((Blob) value));
                break;
            case TIMESTAMP:
                stmt.setTimestamp(index, ((Timestamp) value));
                break;
            default:
                throw new IllegalArgumentException("[!NO SUCH UNIDB DATA TYPE: " + unidbType + "]");
            }
        } catch (ClassCastException cce) {
            throw new SQLException(
                    "Failed to convert " + value + " (" + value.getClass() + ") to " + getName(unidbType));
        }
    }
}

From source file:org.jboss.dashboard.database.NonPooledDataSource.java

protected Statement createPreparedStatementProxy(PreparedStatement stmt, String sql) throws SQLException {
    return (Statement) Proxy.newProxyInstance(stmt.getClass().getClassLoader(),
            getClassInterfaces(stmt.getClass()), new PreparedStatementInvocationHandler(stmt, sql));
}