List of usage examples for java.sql PreparedStatement setNull
void setNull(int parameterIndex, int sqlType) throws SQLException;
NULL
. From source file:org.kontalk.system.Database.java
private static void setValue(PreparedStatement stat, int i, Object value) throws SQLException { if (value instanceof String) { stat.setString(i + 1, (String) value); } else if (value instanceof Integer) { stat.setInt(i + 1, (int) value); } else if (value instanceof Date) { stat.setLong(i + 1, ((Date) value).getTime()); } else if (value instanceof Boolean) { stat.setBoolean(i + 1, (boolean) value); } else if (value instanceof Enum) { stat.setInt(i + 1, ((Enum) value).ordinal()); } else if (value instanceof EnumSet) { stat.setInt(i + 1, EncodingUtils.enumSetToInt(((EnumSet) value))); } else if (value instanceof Optional) { Optional<?> o = (Optional) value; setValue(stat, i, o.orElse(null)); } else if (value == null) { stat.setNull(i + 1, Types.NULL); } else {// w w w . j a va 2 s . c om LOGGER.warning("unknown type: " + value); } }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Insert a long value (which could be null) into * the given field of a statement.// ww w . j a v a2s. com * @param s a prepared Statement * @param i the number of a given field in the prepared statement * @param value the long value to insert (maybe null) * @throws SQLException If i does not correspond to a * parameter marker in the PreparedStatement, or a database access error * occurs or this method is called on a closed PreparedStatement */ public static void setLongMaybeNull(PreparedStatement s, int i, Long value) throws SQLException { ArgumentNotValid.checkNotNull(s, "PreparedStatement s"); if (value != null) { s.setLong(i, value); } else { s.setNull(i, Types.BIGINT); } }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Insert an Integer in prepared statement. * @param s a prepared statement/*from ww w . j ava 2 s . co m*/ * @param i the index of the statement, where the Integer should be inserted * @param value The Integer to insert (maybe null) * @throws SQLException If i does not correspond to a * parameter marker in the PreparedStatement, or a database access error * occurs or this method is called on a closed PreparedStatement */ public static void setIntegerMaybeNull(PreparedStatement s, int i, Integer value) throws SQLException { ArgumentNotValid.checkNotNull(s, "PreparedStatement s"); if (value != null) { s.setInt(i, value); } else { s.setNull(i, Types.INTEGER); } }
From source file:com.keybox.manage.db.PublicKeyDB.java
/** * checks if key has already been registered under user's profile * * @param userId user id//from w ww . j a v a 2s . c o m * @param publicKey public key * @return true if duplicate */ public static boolean isKeyRegistered(Long userId, PublicKey publicKey) { boolean isDuplicate = false; PreparedStatement stmt; Connection con = null; try { con = DBUtils.getConn(); stmt = con.prepareStatement( "select * from public_keys where user_id=? and fingerprint like ? and profile_id is ? and id is not ?"); stmt.setLong(1, userId); stmt.setString(2, SSHUtil.getFingerprint(publicKey.getPublicKey())); if (publicKey.getProfile() != null && publicKey.getProfile().getId() != null) { stmt.setLong(3, publicKey.getProfile().getId()); } else { stmt.setNull(3, Types.NULL); } if (publicKey.getId() != null) { stmt.setLong(4, publicKey.getId()); } else { stmt.setNull(4, Types.NULL); } ResultSet rs = stmt.executeQuery(); if (rs.next()) { isDuplicate = true; } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception ex) { log.error(ex.toString(), ex); } DBUtils.closeConn(con); return isDuplicate; }
From source file:net.sf.jdbcwrappers.nulltype.NullTypeTest.java
@Test public void testSetNull() throws SQLException { Connection connection = dataSource.getConnection(); try {//w w w . ja v a2 s .c o m PreparedStatement statement = connection.prepareStatement("UPDATE TEST SET CHAR_COL=?"); try { statement.setNull(1, Types.NULL); statement.executeUpdate(); } finally { statement.close(); } } finally { connection.close(); } }
From source file:net.sf.jdbcwrappers.nulltype.NullTypeTest.java
@Test(expected = SQLException.class) public void crossCheckDerbyBehaviour() throws SQLException { Connection rawConnection = rawDataSource.getConnection(); try {//from www. j a v a 2 s .co m PreparedStatement statement = rawConnection.prepareStatement("UPDATE TEST SET CHAR_COL=?"); try { statement.setNull(1, Types.NULL); statement.executeUpdate(); } finally { statement.close(); } } finally { rawConnection.close(); } }
From source file:org.LexGrid.LexBIG.caCore.hibernate.types.EnumUserType.java
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException { if (null == value) { preparedStatement.setNull(index, Types.VARCHAR); } else {//from w w w . ja va 2 s . c o m preparedStatement.setString(index, ((Enum) value).name()); } }
From source file:nl.strohalm.cyclos.utils.hibernate.IntValuedEnumType.java
public void nullSafeSet(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException { if (value == null) { st.setNull(index, Types.INTEGER); } else {//from ww w . j av a 2 s.c om int val; if (value instanceof IntValuedEnum) { val = ((IntValuedEnum) value).getValue(); } else { val = ((Number) value).intValue(); } st.setInt(index, val); } if (LOG.isDebugEnabled()) { LOG.debug("Binding " + value + " to parameter: " + index); } }
From source file:nl.strohalm.cyclos.utils.hibernate.StringValuedEnumType.java
public void nullSafeSet(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException { if (value == null) { st.setNull(index, Types.VARCHAR); } else {/* www .j a v a2 s . co m*/ String str; if (value instanceof StringValuedEnum) { str = ((StringValuedEnum) value).getValue(); } else { str = value.toString(); } st.setString(index, str); } if (LOG.isDebugEnabled()) { LOG.debug("Binding " + value + " to parameter: " + index); } }
From source file:com.opengamma.masterdb.security.hibernate.ExpiryAccuracyUserType.java
@SuppressWarnings("deprecation") @Override//from ww w .j av a 2 s .co m public void nullSafeSet(PreparedStatement stmt, Object value, int index) throws HibernateException, SQLException { if (value == null) { stmt.setNull(index, Hibernate.INTEGER.sqlType()); } else { switch ((ExpiryAccuracy) value) { case MIN_HOUR_DAY_MONTH_YEAR: stmt.setInt(index, 5); break; case HOUR_DAY_MONTH_YEAR: stmt.setInt(index, 4); break; case DAY_MONTH_YEAR: stmt.setInt(index, 3); break; case MONTH_YEAR: stmt.setInt(index, 2); break; case YEAR: stmt.setInt(index, 1); break; } } }