Example usage for java.sql Types NUMERIC

List of usage examples for java.sql Types NUMERIC

Introduction

In this page you can find the example usage for java.sql Types NUMERIC.

Prototype

int NUMERIC

To view the source code for java.sql Types NUMERIC.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type NUMERIC.

Usage

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

public Page getPageByTitle(int objectType, long objectId, String title) {
    Long resutls[] = getExtendedJdbcTemplate().queryForObject(
            getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_BY_OBJECT_TYPE_AND_OBJECT_ID_AND_TITLE").getSql(),
            new RowMapper<Long[]>() {
                public Long[] mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return new Long[] { rs.getLong("PAGE_ID"), rs.getLong("VERSION_ID") };
                }/*from  www. ja va  2  s  .  c om*/
            }, new SqlParameterValue(Types.NUMERIC, objectType), new SqlParameterValue(Types.NUMERIC, objectId),
            new SqlParameterValue(Types.VARCHAR, title));
    if (resutls == null || resutls.length == 0) {
        return null;
    }
    return load(resutls[0].longValue(), resutls[1].intValue());
}

From source file:com.glaf.core.jdbc.QueryHelper.java

/**
 * @param conn/*from   w  w  w.java 2 s. c  om*/
 *            ?
 * @param start
 *            0
 * @param pageSize
 *            ?
 * @param sql
 *            ?
 * @param paramMap
 *            ?
 * @return
 */
@SuppressWarnings("unchecked")
public ResultModel getResultList(Connection conn, String sql, Map<String, Object> paramMap, int start,
        int pageSize) {
    if (!DBUtils.isLegalQuerySql(sql)) {
        throw new RuntimeException(" SQL statement illegal ");
    }
    ResultModel resultModel = new ResultModel();
    boolean supportsPhysicalPage = false;
    PreparedStatement psmt = null;
    ResultSetMetaData rsmd = null;
    ResultSet rs = null;
    Dialect dialect = null;
    try {
        dialect = DBConfiguration.getDatabaseDialect(conn);
        if (dialect != null && dialect.supportsPhysicalPage()) {
            logger.debug("sql=" + sql);
            supportsPhysicalPage = dialect.supportsPhysicalPage();
            sql = dialect.getLimitString(sql, start, pageSize);
            logger.debug(">>sql=" + sql);
        }

        List<Object> values = null;
        if (paramMap != null) {
            SqlExecutor sqlExecutor = DBUtils.replaceSQL(sql, paramMap);
            sql = sqlExecutor.getSql();
            values = (List<Object>) sqlExecutor.getParameter();
        }

        logger.debug("sql:\n" + sql);
        logger.debug("values:" + values);

        psmt = conn.prepareStatement(sql);

        if (values != null && !values.isEmpty()) {
            JdbcUtils.fillStatement(psmt, values);
        }

        List<ColumnDefinition> columns = new ArrayList<ColumnDefinition>();
        rs = psmt.executeQuery();
        rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        for (int i = 1; i <= count; i++) {
            int sqlType = rsmd.getColumnType(i);
            ColumnDefinition column = new ColumnDefinition();
            column.setIndex(i);
            column.setColumnName(rsmd.getColumnName(i));
            column.setColumnLabel(rsmd.getColumnLabel(i));
            column.setJavaType(FieldType.getJavaType(sqlType));
            column.setPrecision(rsmd.getPrecision(i));
            column.setScale(rsmd.getScale(i));
            if (column.getScale() == 0 && sqlType == Types.NUMERIC) {
                column.setJavaType("Long");
            }
            column.setName(StringTools.lower(StringTools.camelStyle(column.getColumnLabel())));
            columns.add(column);
        }

        resultModel.setHeaders(columns);

        if (!supportsPhysicalPage) {
            this.skipRows(rs, start);
        }

        int k = 0;
        while (rs.next() && k++ < pageSize) {
            int index = 0;
            RowModel rowModel = new RowModel();
            Iterator<ColumnDefinition> iterator = columns.iterator();
            while (iterator.hasNext()) {
                ColumnDefinition column = iterator.next();
                ColumnDefinition c = new ColumnDefinition();
                c.setColumnName(column.getColumnName());
                c.setColumnLabel(column.getColumnLabel());
                c.setName(column.getName());
                c.setJavaType(column.getJavaType());
                c.setPrecision(column.getPrecision());
                c.setScale(column.getScale());
                String javaType = column.getJavaType();
                index = index + 1;
                if ("String".equals(javaType)) {
                    String value = rs.getString(column.getIndex());
                    c.setValue(value);
                } else if ("Integer".equals(javaType)) {
                    try {
                        Integer value = rs.getInt(column.getIndex());
                        c.setValue(value);
                    } catch (Exception e) {
                        String str = rs.getString(column.getIndex());
                        str = StringTools.replace(str, "$", "");
                        str = StringTools.replace(str, "", "");
                        str = StringTools.replace(str, ",", "");
                        NumberFormat fmt = NumberFormat.getInstance();
                        Number num = fmt.parse(str);
                        c.setValue(num.intValue());
                    }
                } else if ("Long".equals(javaType)) {
                    try {
                        Long value = rs.getLong(column.getIndex());
                        c.setValue(value);
                    } catch (Exception e) {
                        String str = rs.getString(column.getIndex());
                        str = StringTools.replace(str, "$", "");
                        str = StringTools.replace(str, "", "");
                        str = StringTools.replace(str, ",", "");
                        NumberFormat fmt = NumberFormat.getInstance();
                        Number num = fmt.parse(str);
                        c.setValue(num.longValue());
                    }
                } else if ("Double".equals(javaType)) {
                    try {
                        Double value = rs.getDouble(column.getIndex());
                        c.setValue(value);
                    } catch (Exception e) {
                        String str = rs.getString(column.getIndex());
                        str = StringTools.replace(str, "$", "");
                        str = StringTools.replace(str, "", "");
                        str = StringTools.replace(str, ",", "");
                        NumberFormat fmt = NumberFormat.getInstance();
                        Number num = fmt.parse(str);
                        c.setValue(num.doubleValue());
                    }
                } else if ("Boolean".equals(javaType)) {
                    Boolean value = rs.getBoolean(column.getIndex());
                    c.setValue(value);
                } else if ("Date".equals(javaType)) {
                    Timestamp value = rs.getTimestamp(column.getIndex());
                    c.setValue(value);
                } else {
                    c.setValue(rs.getObject(column.getIndex()));
                }
                rowModel.addColumn(c);
            }
            resultModel.addRow(rowModel);
        }
    } catch (Exception ex) {
        logger.error(ex);
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        JdbcUtils.close(psmt);
        JdbcUtils.close(rs);
    }
    return resultModel;
}

From source file:org.springframework.jdbc.object.SqlQueryTests.java

public void testFancyCustomerQuery() throws SQLException {
    mockResultSet.next();/*  w ww  .j  a  v a  2s.  c o  m*/
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerQuery extends MappingSqlQuery {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id) {
            return (Customer) findObject(id);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    Customer cust = query.findCustomer(1);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

public int getPageCount(int objectType, long objectId) {
    return getExtendedJdbcTemplate().queryForObject(
            getBoundSql("ARCHITECTURE_COMMUNITY.COUNT_PAGE_BY_OBJECT_TYPE_AND_OBJECT_ID").getSql(),
            Integer.class, new SqlParameterValue(Types.NUMERIC, objectType),
            new SqlParameterValue(Types.NUMERIC, objectId));
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

public List<Long> getPageIds(int objectType, long objectId) {
    return getExtendedJdbcTemplate().queryForList(
            getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_IDS_BY_OBJECT_TYPE_AND_OBJECT_ID").getSql(),
            Long.class, new SqlParameterValue(Types.NUMERIC, objectType),
            new SqlParameterValue(Types.NUMERIC, objectId));
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

public List<Long> getPageIds(int objectType, long objectId, int startIndex, int maxResults) {
    return getExtendedJdbcTemplate().queryScrollable(
            getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_IDS_BY_OBJECT_TYPE_AND_OBJECT_ID").getSql(),
            startIndex, maxResults, new Object[] { objectType, objectId },
            new int[] { Types.NUMERIC, Types.NUMERIC }, Long.class);
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

@Override
public List<Long> getPageIds(int objectType, PageState state) {
    return getExtendedJdbcTemplate().queryForList(
            getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_IDS_BY_OBJECT_TYPE_AND_STATE").getSql(), Long.class,
            new SqlParameterValue(Types.NUMERIC, objectType),
            new SqlParameterValue(Types.VARCHAR, state.name().toLowerCase()));
}

From source file:org.springframework.jdbc.object.SqlQueryTests.java

public void testUnnamedParameterDeclarationWithNamedParameterQuery() throws SQLException {
    replay();//  w w w  .ja v  a  2 s . c  om

    class CustomerQuery extends MappingSqlQuery {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id) {
            Map params = new HashMap();
            params.put("id", new Integer(id));
            return (Customer) executeByNamedParam(params).get(0);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    try {
        Customer cust = query.findCustomer(1);
        fail("Query should not succeed since parameter declaration did not specify parameter name");
    } catch (InvalidDataAccessApiUsageException ex) {
        // OK - it worked
    }
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

@Override
public List<Long> getPageIds(int objectType, PageState state, int startIndex, int maxResults) {
    return getExtendedJdbcTemplate().queryScrollable(
            getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_IDS_BY_OBJECT_TYPE_AND_STATE").getSql(), startIndex,
            maxResults, new Object[] { objectType, state.name().toLowerCase() },
            new int[] { Types.NUMERIC, Types.VARCHAR }, Long.class);
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

@Override
public int getPageCount(int objectType, PageState state) {
    return getExtendedJdbcTemplate().queryForObject(
            getBoundSql("ARCHITECTURE_COMMUNITY.COUNT_PAGE_BY_OBJECT_TYPE_AND_STATE").getSql(), Integer.class,
            new SqlParameterValue(Types.NUMERIC, objectType),
            new SqlParameterValue(Types.VARCHAR, state.name().toLowerCase()));
}