Example usage for java.sql Types TIMESTAMP

List of usage examples for java.sql Types TIMESTAMP

Introduction

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

Prototype

int TIMESTAMP

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

Click Source Link

Document

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

Usage

From source file:org.apache.hive.jdbc.TestJdbcDriver2.java

@Test
public void testResultSetMetaData() throws SQLException {
    Statement stmt = con.createStatement();

    ResultSet res = stmt.executeQuery("select c1, c2, c3, c4, c5 as a, c6, c7, c8, c9, c10, c11, c12, "
            + "c1*2, sentences(null, null, null) as b, c17, c18, c20, c21, c22, c23 from " + dataTypeTableName
            + " limit 1");
    ResultSetMetaData meta = res.getMetaData();

    ResultSet colRS = con.getMetaData().getColumns(null, null, dataTypeTableName.toLowerCase(), null);

    assertEquals(20, meta.getColumnCount());

    assertTrue(colRS.next());//  www . j a  va 2s. com

    assertEquals("c1", meta.getColumnName(1));
    assertEquals(Types.INTEGER, meta.getColumnType(1));
    assertEquals("int", meta.getColumnTypeName(1));
    assertEquals(11, meta.getColumnDisplaySize(1));
    assertEquals(10, meta.getPrecision(1));
    assertEquals(0, meta.getScale(1));

    assertEquals("c1", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.INTEGER, colRS.getInt("DATA_TYPE"));
    assertEquals("int", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(1), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(1), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c2", meta.getColumnName(2));
    assertEquals("boolean", meta.getColumnTypeName(2));
    assertEquals(Types.BOOLEAN, meta.getColumnType(2));
    assertEquals(1, meta.getColumnDisplaySize(2));
    assertEquals(1, meta.getPrecision(2));
    assertEquals(0, meta.getScale(2));

    assertEquals("c2", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.BOOLEAN, colRS.getInt("DATA_TYPE"));
    assertEquals("boolean", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getScale(2), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c3", meta.getColumnName(3));
    assertEquals(Types.DOUBLE, meta.getColumnType(3));
    assertEquals("double", meta.getColumnTypeName(3));
    assertEquals(25, meta.getColumnDisplaySize(3));
    assertEquals(15, meta.getPrecision(3));
    assertEquals(15, meta.getScale(3));

    assertEquals("c3", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.DOUBLE, colRS.getInt("DATA_TYPE"));
    assertEquals("double", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(3), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(3), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c4", meta.getColumnName(4));
    assertEquals(Types.VARCHAR, meta.getColumnType(4));
    assertEquals("string", meta.getColumnTypeName(4));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(4));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(4));
    assertEquals(0, meta.getScale(4));

    assertEquals("c4", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.VARCHAR, colRS.getInt("DATA_TYPE"));
    assertEquals("string", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(4), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(4), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("a", meta.getColumnName(5));
    assertEquals(Types.ARRAY, meta.getColumnType(5));
    assertEquals("array", meta.getColumnTypeName(5));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(5));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(5));
    assertEquals(0, meta.getScale(5));

    assertEquals("c5", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.ARRAY, colRS.getInt("DATA_TYPE"));
    assertEquals("array<int>", colRS.getString("TYPE_NAME").toLowerCase());

    assertTrue(colRS.next());

    assertEquals("c6", meta.getColumnName(6));
    assertEquals(Types.JAVA_OBJECT, meta.getColumnType(6));
    assertEquals("map", meta.getColumnTypeName(6));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(6));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(6));
    assertEquals(0, meta.getScale(6));

    assertEquals("c6", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.JAVA_OBJECT, colRS.getInt("DATA_TYPE"));
    assertEquals("map<int,string>", colRS.getString("TYPE_NAME").toLowerCase());

    assertTrue(colRS.next());

    assertEquals("c7", meta.getColumnName(7));
    assertEquals(Types.JAVA_OBJECT, meta.getColumnType(7));
    assertEquals("map", meta.getColumnTypeName(7));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(7));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(7));
    assertEquals(0, meta.getScale(7));

    assertEquals("c7", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.JAVA_OBJECT, colRS.getInt("DATA_TYPE"));
    assertEquals("map<string,string>", colRS.getString("TYPE_NAME").toLowerCase());

    assertTrue(colRS.next());

    assertEquals("c8", meta.getColumnName(8));
    assertEquals(Types.STRUCT, meta.getColumnType(8));
    assertEquals("struct", meta.getColumnTypeName(8));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(8));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(8));
    assertEquals(0, meta.getScale(8));

    assertEquals("c8", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.STRUCT, colRS.getInt("DATA_TYPE"));
    assertEquals("struct<r:string,s:int,t:double>", colRS.getString("TYPE_NAME").toLowerCase());

    assertTrue(colRS.next());

    assertEquals("c9", meta.getColumnName(9));
    assertEquals(Types.TINYINT, meta.getColumnType(9));
    assertEquals("tinyint", meta.getColumnTypeName(9));
    assertEquals(4, meta.getColumnDisplaySize(9));
    assertEquals(3, meta.getPrecision(9));
    assertEquals(0, meta.getScale(9));

    assertEquals("c9", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.TINYINT, colRS.getInt("DATA_TYPE"));
    assertEquals("tinyint", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(9), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(9), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c10", meta.getColumnName(10));
    assertEquals(Types.SMALLINT, meta.getColumnType(10));
    assertEquals("smallint", meta.getColumnTypeName(10));
    assertEquals(6, meta.getColumnDisplaySize(10));
    assertEquals(5, meta.getPrecision(10));
    assertEquals(0, meta.getScale(10));

    assertEquals("c10", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.SMALLINT, colRS.getInt("DATA_TYPE"));
    assertEquals("smallint", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(10), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(10), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c11", meta.getColumnName(11));
    assertEquals(Types.FLOAT, meta.getColumnType(11));
    assertEquals("float", meta.getColumnTypeName(11));
    assertEquals(24, meta.getColumnDisplaySize(11));
    assertEquals(7, meta.getPrecision(11));
    assertEquals(7, meta.getScale(11));

    assertEquals("c11", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.FLOAT, colRS.getInt("DATA_TYPE"));
    assertEquals("float", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(11), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(11), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c12", meta.getColumnName(12));
    assertEquals(Types.BIGINT, meta.getColumnType(12));
    assertEquals("bigint", meta.getColumnTypeName(12));
    assertEquals(20, meta.getColumnDisplaySize(12));
    assertEquals(19, meta.getPrecision(12));
    assertEquals(0, meta.getScale(12));

    assertEquals("c12", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.BIGINT, colRS.getInt("DATA_TYPE"));
    assertEquals("bigint", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(12), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(12), colRS.getInt("DECIMAL_DIGITS"));

    assertEquals("_c12", meta.getColumnName(13));
    assertEquals(Types.INTEGER, meta.getColumnType(13));
    assertEquals("int", meta.getColumnTypeName(13));
    assertEquals(11, meta.getColumnDisplaySize(13));
    assertEquals(10, meta.getPrecision(13));
    assertEquals(0, meta.getScale(13));

    assertEquals("b", meta.getColumnName(14));
    assertEquals(Types.ARRAY, meta.getColumnType(14));
    assertEquals("array", meta.getColumnTypeName(14));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(14));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(14));
    assertEquals(0, meta.getScale(14));

    // Move the result of getColumns() forward to match the columns of the query
    assertTrue(colRS.next()); // c13
    assertTrue(colRS.next()); // c14
    assertTrue(colRS.next()); // c15
    assertTrue(colRS.next()); // c16
    assertTrue(colRS.next()); // c17

    assertEquals("c17", meta.getColumnName(15));
    assertEquals(Types.TIMESTAMP, meta.getColumnType(15));
    assertEquals("timestamp", meta.getColumnTypeName(15));
    assertEquals(29, meta.getColumnDisplaySize(15));
    assertEquals(29, meta.getPrecision(15));
    assertEquals(9, meta.getScale(15));

    assertEquals("c17", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.TIMESTAMP, colRS.getInt("DATA_TYPE"));
    assertEquals("timestamp", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(15), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(15), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c18", meta.getColumnName(16));
    assertEquals(Types.DECIMAL, meta.getColumnType(16));
    assertEquals("decimal", meta.getColumnTypeName(16));
    assertEquals(18, meta.getColumnDisplaySize(16));
    assertEquals(16, meta.getPrecision(16));
    assertEquals(7, meta.getScale(16));

    assertEquals("c18", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.DECIMAL, colRS.getInt("DATA_TYPE"));
    assertEquals("decimal", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(16), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(16), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next()); // skip c19, since not selected by query
    assertTrue(colRS.next());

    assertEquals("c20", meta.getColumnName(17));
    assertEquals(Types.DATE, meta.getColumnType(17));
    assertEquals("date", meta.getColumnTypeName(17));
    assertEquals(10, meta.getColumnDisplaySize(17));
    assertEquals(10, meta.getPrecision(17));
    assertEquals(0, meta.getScale(17));

    assertEquals("c20", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.DATE, colRS.getInt("DATA_TYPE"));
    assertEquals("date", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(17), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(17), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c21", meta.getColumnName(18));
    assertEquals(Types.VARCHAR, meta.getColumnType(18));
    assertEquals("varchar", meta.getColumnTypeName(18));
    // varchar columns should have correct display size/precision
    assertEquals(20, meta.getColumnDisplaySize(18));
    assertEquals(20, meta.getPrecision(18));
    assertEquals(0, meta.getScale(18));

    assertEquals("c21", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.VARCHAR, colRS.getInt("DATA_TYPE"));
    assertEquals("varchar", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(18), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(18), colRS.getInt("DECIMAL_DIGITS"));

    assertTrue(colRS.next());

    assertEquals("c22", meta.getColumnName(19));
    assertEquals(Types.CHAR, meta.getColumnType(19));
    assertEquals("char", meta.getColumnTypeName(19));
    // char columns should have correct display size/precision
    assertEquals(15, meta.getColumnDisplaySize(19));
    assertEquals(15, meta.getPrecision(19));
    assertEquals(0, meta.getScale(19));

    assertEquals("c23", meta.getColumnName(20));
    assertEquals(Types.BINARY, meta.getColumnType(20));
    assertEquals("binary", meta.getColumnTypeName(20));
    assertEquals(Integer.MAX_VALUE, meta.getColumnDisplaySize(20));
    assertEquals(Integer.MAX_VALUE, meta.getPrecision(20));
    assertEquals(0, meta.getScale(20));

    assertEquals("c22", colRS.getString("COLUMN_NAME"));
    assertEquals(Types.CHAR, colRS.getInt("DATA_TYPE"));
    assertEquals("char", colRS.getString("TYPE_NAME").toLowerCase());
    assertEquals(meta.getPrecision(19), colRS.getInt("COLUMN_SIZE"));
    assertEquals(meta.getScale(19), colRS.getInt("DECIMAL_DIGITS"));

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        assertFalse(meta.isAutoIncrement(i));
        assertFalse(meta.isCurrency(i));
        assertEquals(ResultSetMetaData.columnNullable, meta.isNullable(i));
    }
}

From source file:org.apache.ddlutils.io.TestDatabaseIO.java

/**
 * Tests a database model with an index with two columns.
 *///from   ww  w .j a  v a  2 s  . c  o m
public void testIndexWithTwoColumns() throws Exception {
    Database model = readModel("<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n"
            + "  <table name='TableWithIndex'>\n" + "    <column name='id'\n" + "            type='DOUBLE'\n"
            + "            primaryKey='true'\n" + "            required='true'/>\n"
            + "    <column name='when'\n" + "            type='TIMESTAMP'\n" + "            required='true'/>\n"
            + "    <column name='value'\n" + "            type='SMALLINT'\n" + "            default='1'/>\n"
            + "    <index>\n" + "      <index-column name='when'/>\n" + "      <index-column name='id'/>\n"
            + "    </index>\n" + "  </table>\n" + "</database>");

    assertEquals("test", model.getName());
    assertEquals(1, model.getTableCount());

    Table table = model.getTable(0);

    assertEquals("TableWithIndex", null, 3, 1, 0, 0, 1, table);
    assertEquals("id", Types.DOUBLE, 0, 0, null, null, null, true, true, false, table.getColumn(0));
    assertEquals("when", Types.TIMESTAMP, 0, 0, null, null, null, false, true, false, table.getColumn(1));
    assertEquals("value", Types.SMALLINT, 0, 0, "1", null, null, false, false, false, table.getColumn(2));

    Index index = table.getIndex(0);

    assertEquals(null, false, 2, index);
    assertEquals(table.getColumn(1), null, index.getColumn(0));
    assertEquals(table.getColumn(0), null, index.getColumn(1));

    assertEquals("<?xml version='1.0' encoding='UTF-8'?>\n" + "<database xmlns=\""
            + DatabaseIO.DDLUTILS_NAMESPACE + "\" name=\"test\">\n" + "  <table name=\"TableWithIndex\">\n"
            + "    <column name=\"id\" primaryKey=\"true\" required=\"true\" type=\"DOUBLE\" autoIncrement=\"false\" />\n"
            + "    <column name=\"when\" primaryKey=\"false\" required=\"true\" type=\"TIMESTAMP\" autoIncrement=\"false\" />\n"
            + "    <column name=\"value\" primaryKey=\"false\" required=\"false\" type=\"SMALLINT\" default=\"1\" autoIncrement=\"false\" />\n"
            + "    <index>\n" + "      <index-column name=\"when\" />\n"
            + "      <index-column name=\"id\" />\n" + "    </index>\n" + "  </table>\n" + "</database>\n",
            model);
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

private void setTimestampValue(int queryType, String paramName, String value, String paramType,
        PreparedStatement sqlQuery, int i) throws DataServiceFault, SQLException {
    Timestamp timestamp = null;//w  ww. ja v a  2s  .  c o  m
    try {
        if (value != null) {
            timestamp = DBUtils.getTimestamp(value);
        }
    } catch (ParseException e) {
        throw new DataServiceFault(e, "Incorrect Timestamp format for parameter : " + paramName
                + ". Timestamp should be in one of following formats " + "yyyy-MM-dd'T'hh:mm:ss.sss'+'hh:mm, "
                + "yyyy-MM-dd'T'hh:mm:ss.sss'-'hh:mm, " + "yyyy-MM-dd'T'hh:mm:ss.sss'Z', "
                + "yyyy-MM-dd hh:mm:ss.SSSSSS or " + "yyyy-MM-dd hh:mm:ss");
    } catch (DataServiceFault e) {
        throw new DataServiceFault(e,
                "Error processing parameter - " + paramName + ", Error - " + e.getMessage());
    }
    if ("IN".equals(paramType)) {
        if (queryType == SQLQuery.DS_QUERY_TYPE_NORMAL) {
            if (value == null) {
                sqlQuery.setNull(i + 1, java.sql.Types.TIMESTAMP);
            } else {
                sqlQuery.setTimestamp(i + 1, timestamp, calendar);
            }
        } else {
            if (value == null) {
                ((CallableStatement) sqlQuery).setNull(i + 1, java.sql.Types.TIMESTAMP);
            } else {
                ((CallableStatement) sqlQuery).setTimestamp(i + 1, timestamp, calendar);
            }
        }
    } else if ("INOUT".equals(paramType)) {
        if (value == null) {
            ((CallableStatement) sqlQuery).setNull(i + 1, java.sql.Types.TIMESTAMP);
        } else {
            ((CallableStatement) sqlQuery).setTimestamp(i + 1, timestamp, calendar);
        }
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIMESTAMP);
    } else {
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIMESTAMP);
    }
}

From source file:nl.ordina.bag.etl.dao.AbstractBAGDAO.java

@Override
public void update(final Standplaats standplaats) throws DAOException {
    try {/*from  w w w  .  jav  a 2  s.c om*/
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                jdbcTemplate.update(new PreparedStatementCreator() {
                    @Override
                    public PreparedStatement createPreparedStatement(Connection connection)
                            throws SQLException {
                        PreparedStatement ps = connection.prepareStatement("update bag_standplaats set"
                                + " aanduiding_record_inactief = ?," + " officieel = ?,"
                                + " standplaats_status = ?," + " standplaats_geometrie = ?,"
                                + " einddatum_tijdvak_geldigheid = ?," + " in_onderzoek = ?,"
                                + " bron_documentdatum = ?," + " bron_documentnummer = ?,"
                                + " bag_nummeraanduiding_id = ?" + " where bag_standplaats_id = ?"
                                + " and aanduiding_record_correctie = ?"
                                + " and begindatum_tijdvak_geldigheid = ?");
                        ps.setInt(1, standplaats.getAanduidingRecordInactief().ordinal());
                        ps.setInt(2, standplaats.getOfficieel().ordinal());
                        ps.setInt(3, standplaats.getStandplaatsStatus().ordinal());
                        ps.setString(4, standplaats.getStandplaatsGeometrie());
                        if (standplaats.getEinddatumTijdvakGeldigheid() == null)
                            ps.setNull(5, Types.TIMESTAMP);
                        else
                            ps.setTimestamp(5,
                                    new Timestamp(standplaats.getEinddatumTijdvakGeldigheid().getTime()));
                        ps.setInt(6, standplaats.getInOnderzoek().ordinal());
                        ps.setDate(7, new Date(standplaats.getDocumentdatum().getTime()));
                        ps.setString(8, standplaats.getDocumentnummer());
                        ps.setLong(9, standplaats.getHoofdAdres());
                        ps.setLong(10, standplaats.getIdentificatie());
                        ps.setLong(11, standplaats.getAanduidingRecordCorrectie());
                        ps.setTimestamp(12,
                                new Timestamp(standplaats.getBegindatumTijdvakGeldigheid().getTime()));
                        return ps;
                    }
                });
                deleteNevenadressen(TypeAdresseerbaarObject.STANDPLAATS, standplaats);
                insertNevenadressen(TypeAdresseerbaarObject.STANDPLAATS, standplaats);
            }
        });
    } catch (DataAccessException e) {
        throw new DAOException("Error updating standplaats: " + standplaats.getIdentificatie(), e);
    }
}

From source file:org.nuclos.server.masterdata.ejb3.MetaDataFacadeBean.java

private String getBestJavaType(int colType) {
    String sType = "java.lang.String";
    switch (colType) {
    case Types.VARCHAR:
        return sType;
    case Types.CHAR:
        return sType;
    case Types.NCHAR:
        return sType;
    case Types.NVARCHAR:
        return sType;
    case Types.LONGNVARCHAR:
        return sType;
    case Types.LONGVARCHAR:
        return sType;
    case Types.LONGVARBINARY:
        return sType;
    case Types.NUMERIC:
        return "java.lang.Integer";
    case Types.DECIMAL:
        return "java.lang.Double";
    case Types.BOOLEAN:
        return "java.lang.Integer";
    case Types.DATE:
        return "java.util.Date";
    case Types.TIME:
        return "java.util.Date";
    case Types.TIMESTAMP:
        return "java.util.Date";

    default:// w  w w  .  j  a  va2 s  .  co  m

        return sType;
    }
}

From source file:edu.jhuapl.openessence.datasource.jdbc.entry.JdbcOeDataEntrySource.java

/**
 * Sets arguments of the proper type on a PreparedSatement
 *
 * @param pStmt     prepared statement on which to set arguments
 * @param dimIds    dimension ids that map to columns on the prepared statement
 * @param valueList values to set for the paramenters on the prepared statement
 * @param valueMap  dimIds mapped to values to set for the paramenters on the prepared statement
 * @throws SQLException          if error occurs while processing the prepared statement
 * @throws OeDataSourceException if error occurs converting value to it's sql type
 *//*  w ww  .j  ava 2  s.  c  o m*/
protected void setArgumentsOnSqlType(PreparedStatement pStmt, List<String> dimIds, List<Object> valueList,
        Map<String, Object> valueMap) throws SQLException, OeDataSourceException {

    if ((valueList == null && valueMap == null) || (valueList != null && valueMap != null)) {
        throw new OeDataSourceException("Invalid value lists. Only one form of the list can be provided.");
    }

    int argCount = 0;
    Object val;
    boolean isValueList = (valueList != null);

    for (String dimId : dimIds) {
        Dimension dim = getEditDimension(dimId);
        FieldType sqlType = dim.getSqlType();

        if (isValueList) {
            val = valueList.get(argCount);
        } else {
            val = valueMap.get(dimId);
        }

        try {
            argCount++;
            switch (sqlType) {
            case DATE_TIME:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlTimestampType(val),
                        Types.TIMESTAMP);
                continue;
            case DATE:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.DATE);
                continue;
            case BOOLEAN:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.BOOLEAN);
                continue;
            case FLOAT:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.FLOAT);
                continue;
            case DOUBLE:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.DOUBLE);
                continue;
            case INTEGER:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.INTEGER);
                continue;
            case LONG:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.BIGINT);
                continue;
            case TEXT:
                pStmt.setObject(argCount, DataTypeConversionHelper.convert2SqlType(val), Types.VARCHAR);
                continue;
            default:
                throw new AssertionError("Unexpected sqlType " + sqlType + ".");
            }

        } catch (OeDataSourceException e) {
            throw new SQLException("Error occured converting value \"" + val + "\" to its sql type.", e);
        }
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Return a SQL string to act as a placeholder for the given column.
 *//*  ww  w.j  av a 2  s  .  c  om*/
public String getPlaceholderValueString(Column col) {
    switch (col.getType()) {
    case Types.BIGINT:
    case Types.BIT:
    case Types.INTEGER:
    case Types.NUMERIC:
    case Types.SMALLINT:
    case Types.TINYINT:
        return "0";
    case Types.CHAR:
        return (storeCharsAsNumbers) ? "0" : "' '";
    case Types.CLOB:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
        return "''";
    case Types.DATE:
        return ZERO_DATE_STR;
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.REAL:
        return "0.0";
    case Types.TIME:
        return ZERO_TIME_STR;
    case Types.TIMESTAMP:
        return ZERO_TIMESTAMP_STR;
    default:
        return "NULL";
    }
}

From source file:nl.ordina.bag.etl.dao.AbstractBAGDAO.java

@Override
public void update(final Woonplaats origineel, final Woonplaats wijziging) throws DAOException {
    try {//  w  w w.  j  a v a 2 s.  co m
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement("update bag_woonplaats set"
                        + " aanduiding_record_inactief = ?," + " aanduiding_record_correctie = ?,"
                        + " woonplaats_naam = ?," + " woonplaats_geometrie = ?," + " officieel = ?,"
                        + " einddatum_tijdvak_geldigheid = ?," + " in_onderzoek = ?,"
                        + " bron_documentdatum = ?," + " bron_documentnummer = ?," + " woonplaats_status = ?"
                        + " where bag_woonplaats_id = ?" + " and aanduiding_record_correctie = ?"
                        + " and begindatum_tijdvak_geldigheid = ?");
                ps.setInt(1, wijziging.getAanduidingRecordInactief().ordinal());
                ps.setLong(2, wijziging.getAanduidingRecordCorrectie());
                ps.setString(3, wijziging.getWoonplaatsNaam());
                ps.setString(4, wijziging.getWoonplaatsGeometrie());
                ps.setInt(5, wijziging.getOfficieel().ordinal());
                if (wijziging.getEinddatumTijdvakGeldigheid() == null)
                    ps.setNull(6, Types.TIMESTAMP);
                else
                    ps.setTimestamp(6, new Timestamp(wijziging.getEinddatumTijdvakGeldigheid().getTime()));
                ps.setInt(7, wijziging.getInOnderzoek().ordinal());
                ps.setDate(8, new Date(wijziging.getDocumentdatum().getTime()));
                ps.setString(9, wijziging.getDocumentnummer());
                ps.setInt(10, wijziging.getWoonplaatsStatus().ordinal());
                ps.setLong(11, origineel.getIdentificatie());
                ps.setLong(12, origineel.getAanduidingRecordCorrectie());
                ps.setTimestamp(13, new Timestamp(origineel.getBegindatumTijdvakGeldigheid().getTime()));
                return ps;
            }
        });
    } catch (DataAccessException e) {
        throw new DAOException("Error updating woonplaats: " + origineel.getIdentificatie(), e);
    }
}

From source file:nl.ordina.bag.etl.dao.AbstractBAGDAO.java

@Override
public void update(final OpenbareRuimte origineel, final OpenbareRuimte mutation) throws DAOException {
    try {// w w w .  j a  v  a  2 s. co  m
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement("update bag_openbare_ruimte set"
                        + " aanduiding_record_inactief = ?," + " aanduiding_record_correctie = ?,"
                        + " openbare_ruimte_naam = ?," + " officieel = ?,"
                        + " einddatum_tijdvak_geldigheid = ?," + " in_onderzoek = ?,"
                        + " openbare_ruimte_type = ?," + " bron_documentdatum = ?,"
                        + " bron_documentnummer = ?," + " openbareruimte_status = ?,"
                        + " bag_woonplaats_id = ?," + " verkorte_openbare_ruimte_naam = ?"
                        + " where bag_openbare_ruimte_id = ?" + " and aanduiding_record_correctie = ?"
                        + " and begindatum_tijdvak_geldigheid = ?");
                ps.setInt(1, mutation.getAanduidingRecordInactief().ordinal());
                ps.setLong(2, mutation.getAanduidingRecordCorrectie());
                ps.setString(3, mutation.getOpenbareRuimteNaam());
                ps.setInt(4, mutation.getOfficieel().ordinal());
                if (mutation.getEinddatumTijdvakGeldigheid() == null)
                    ps.setNull(5, Types.TIMESTAMP);
                else
                    ps.setTimestamp(5, new Timestamp(mutation.getEinddatumTijdvakGeldigheid().getTime()));
                ps.setInt(6, mutation.getInOnderzoek().ordinal());
                ps.setInt(7, mutation.getOpenbareRuimteType().ordinal());
                ps.setDate(8, new Date(mutation.getDocumentdatum().getTime()));
                ps.setString(9, mutation.getDocumentnummer());
                ps.setInt(10, mutation.getOpenbareruimteStatus().ordinal());
                ps.setLong(11, mutation.getGerelateerdeWoonplaats());
                ps.setString(12, mutation.getVerkorteOpenbareRuimteNaam());
                ps.setLong(13, origineel.getIdentificatie());
                ps.setLong(14, origineel.getAanduidingRecordCorrectie());
                ps.setTimestamp(15, new Timestamp(origineel.getBegindatumTijdvakGeldigheid().getTime()));
                return ps;
            }
        });
    } catch (DataAccessException e) {
        throw new DAOException("Error updating openbare ruimte: " + origineel.getIdentificatie(), e);
    }
}