Java Code Examples for java.sql.PreparedStatement.setBoolean()

The following are Jave code examples for showing how to use setBoolean() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: alvisnlp   File: SQLParameterEvaluator.java   View Source Code Vote up 6 votes
void updateStatement(PreparedStatement statement, int parameterIndex, EvaluationContext ctx, Element elt) throws SQLException, ProcessingException {
	switch (type) {
		case BOOLEAN:
			boolean b = evaluator.evaluateBoolean(ctx, elt);
			statement.setBoolean(parameterIndex, b);
			break;
		case DOUBLE:
			double d = evaluator.evaluateDouble(ctx, elt);
			statement.setDouble(parameterIndex, d);
			break;
		case INT:
			int i = evaluator.evaluateInt(ctx, elt);
			statement.setInt(parameterIndex, i);
			break;
		case STRING:
			String s = evaluator.evaluateString(ctx, elt);
			statement.setString(parameterIndex, s);
			break;
		default:
			throw new ProcessingException("unhandled type " + type);
	}
}
 
Example 2
Project: incubator-netbeans   File: DefaultAdaptor.java   View Source Code Vote up 6 votes
/**
* Gets a description of a table's indices and statistics. They are
* ordered by NON_UNIQUE, TYPE, INDEX_NAME, and ORDINAL_POSITION.
*
*/
public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException
{
    if (getCapitializeUsername() && schema != null) schema = schema.toUpperCase();
    String query = (String)properties.get(PROP_INDEX_INFO_QUERY);
    if (query != null) {
        if (con != null) {
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, catalog);
            stmt.setString(2, schema);
            stmt.setString(3, table);
            stmt.setBoolean(4, unique);
            stmt.setBoolean(5, approximate);
            return stmt.executeQuery();
        } else throw new SQLException(bundle.getString("EXC_NoConnection")); // NOI18N
    }

    if (dmd == null) throw new SQLException(bundle.getString("EXC_NoDBMetadata")); // NOI18N
    return dmd.getIndexInfo(catalog, schema, table, unique, approximate);
}
 
Example 3
Project: tcp   File: SqlInserts.java   View Source Code Vote up 6 votes
public static boolean insertAccionDestinatario(AccionDestinatario destinatario, String usuarioResponsable){
	try {
		Connection conn=ConnectionConfiguration.conectar();

		String query = " insert into accion_destinatario (cantidad, descripcion, version, borrado, beneficiario_tipo_id, accion_id, beneficiario_grupo_id, usuario_responsable)"
	+ " values (?, ?, ?, ?, ?, ?, ?, ?)";
		
		PreparedStatement insert = conn.prepareStatement(query);
		
		insert.setInt (1, destinatario.getCantidad());
		insert.setString (2, destinatario.getDescripcion());
		insert.setInt (3, destinatario.getVersion());
		insert.setBoolean (4, destinatario.isBorrado());
		insert.setInt (5, destinatario.getBeneficiarioTipoId());
		insert.setInt (6, destinatario.getAccionId());
		insert.setInt (7, destinatario.getBeneficiarioGrupoId());
		insert.setString (8, usuarioResponsable);
		
		insert.execute();
		   
		conn.close();
		return true;
	} catch (SQLException e) {e.printStackTrace(); return false;}
	
}
 
Example 4
Project: AdvancedDataProfilingSeminar   File: PostgreSQLDataAccessObject.java   View Source Code Vote up 6 votes
@Override
public PreparedStatement insertValuesIntoStatement(PreparedStatement statement, String[] values, String[] valueTypes, int offset) throws NumberFormatException, SQLException {
	for (int i = 0; i < values.length; i++) {
		String valueType = valueTypes[i + offset].toLowerCase();
		
		if ((values[i] == null) || (values[i].equals("")))  {
			if (valueType.contains("long") || valueType.contains("big") || valueType.contains("int8"))
				statement.setNull(i + 1, java.sql.Types.BIGINT);
			else if (valueType.contains("int") || valueType.contains("serial"))
				statement.setNull(i + 1, java.sql.Types.INTEGER);
			else if (valueType.contains("float") || valueType.contains("numeric") || valueType.contains("decimal") || valueType.contains("real") || valueType.contains("precision"))
				statement.setNull(i + 1, java.sql.Types.FLOAT);
			else if (valueType.contains("bool"))
				statement.setNull(i + 1, java.sql.Types.BOOLEAN);
			else if (valueType.contains("date"))
				statement.setNull(i + 1, java.sql.Types.DATE);
			else 
				statement.setNull(i + 1, java.sql.Types.VARCHAR);
		}
		else {
			if (valueType.contains("long") || valueType.contains("big") || valueType.contains("int8"))
				statement.setLong(i + 1, Long.valueOf(values[i]).longValue());
			else if (valueType.contains("int") || valueType.contains("serial"))
				statement.setInt(i + 1, Integer.valueOf(values[i]).intValue());
			else if (valueType.contains("float") || valueType.contains("numeric") || valueType.contains("decimal") || valueType.contains("real") || valueType.contains("precision"))
				statement.setFloat(i + 1, Float.valueOf(values[i]).shortValue());
			else if (valueType.contains("bool"))
				statement.setBoolean(i + 1, Boolean.valueOf(values[i]).booleanValue());
			else if (valueType.contains("date"))
				statement.setDate(i + 1, Date.valueOf(values[i]));
			else 
				statement.setString(i + 1, values[i]);
		}
	}
	return statement;
}
 
Example 5
Project: Nird2   File: JdbcDatabase.java   View Source Code Vote up 6 votes
@Override
public void addGroupVisibility(Connection txn, ContactId c, GroupId g,
		boolean shared) throws DbException {
	PreparedStatement ps = null;
	try {
		String sql = "INSERT INTO groupVisibilities"
				+ " (contactId, groupId, shared)"
				+ " VALUES (?, ?, ?)";
		ps = txn.prepareStatement(sql);
		ps.setInt(1, c.getInt());
		ps.setBytes(2, g.getBytes());
		ps.setBoolean(3, shared);
		int affected = ps.executeUpdate();
		if (affected != 1) throw new DbStateException();
		ps.close();
	} catch (SQLException e) {
		tryToClose(ps);
		throw new DbException(e);
	}
}
 
Example 6
Project: Nird2   File: JdbcDatabase.java   View Source Code Vote up 6 votes
@Override
public void setGroupVisibility(Connection txn, ContactId c, GroupId g,
		boolean shared) throws DbException {
	PreparedStatement ps = null;
	try {
		String sql = "UPDATE groupVisibilities SET shared = ?"
				+ " WHERE contactId = ? AND groupId = ?";
		ps = txn.prepareStatement(sql);
		ps.setBoolean(1, shared);
		ps.setInt(2, c.getInt());
		ps.setBytes(3, g.getBytes());
		int affected = ps.executeUpdate();
		if (affected < 0 || affected > 1) throw new DbStateException();
		ps.close();
	} catch (SQLException e) {
		tryToClose(ps);
		throw new DbException(e);
	}
}
 
Example 7
Project: mcClans   File: QueryValue.java   View Source Code Vote up 6 votes
public void appendValue(PreparedStatement statement, int index) throws SQLException {
    index = index + 1;
    if (dataType == DataType.BOOLEAN) {
        statement.setBoolean(index, dataValue.equals("1"));
    } else if (dataType == DataType.INTEGER) {
        statement.setInt(index, Integer.parseInt(dataValue));
    } else if (dataType == DataType.STRING) {
        statement.setString(index, dataValue);
    } else if (dataType == DataType.DOUBLE) {
        statement.setDouble(index, Double.parseDouble(dataValue));
    } else if (dataType == DataType.FLOAT) {
        statement.setFloat(index, Float.parseFloat(dataValue));
    } else if (dataType == DataType.LONG) {
        statement.setLong(index, Long.parseLong(dataValue));
    }
}
 
Example 8
Project: tcp   File: SqlInserts.java   View Source Code Vote up 6 votes
public static void insertLineaEstrategica(LineaEstrategica lineaEstrategica, String usuarioResponsable){
	try {
		Connection conn=ConnectionConfiguration.conectar();
	   	
		String query = " insert into linea_estrategica (nombre,descripcion,orden,borrado, usuario_responsable)"
	+ " values (?, ?, ?, ?, ?)";
		
		PreparedStatement insert = conn.prepareStatement(query);
		
		//insert.setInt (1, lineaEstrategica.getId());
		insert.setString (1, lineaEstrategica.getNombre());
		insert.setString (2, lineaEstrategica.getDescripcion());
		insert.setInt (3, lineaEstrategica.getOrden());
		insert.setBoolean (4, lineaEstrategica.isBorrado());
		insert.setString (5, usuarioResponsable);
		
		insert.execute();
		   
		conn.close();
	} catch (SQLException e) {e.printStackTrace();}
		
}
 
Example 9
Project: Nird2   File: JdbcDatabase.java   View Source Code Vote up 6 votes
@Override
public void addGroupVisibility(Connection txn, ContactId c, GroupId g,
		boolean shared) throws DbException {
	PreparedStatement ps = null;
	try {
		String sql = "INSERT INTO groupVisibilities"
				+ " (contactId, groupId, shared)"
				+ " VALUES (?, ?, ?)";
		ps = txn.prepareStatement(sql);
		ps.setInt(1, c.getInt());
		ps.setBytes(2, g.getBytes());
		ps.setBoolean(3, shared);
		int affected = ps.executeUpdate();
		if (affected != 1) throw new DbStateException();
		ps.close();
	} catch (SQLException e) {
		tryToClose(ps);
		throw new DbException(e);
	}
}
 
Example 10
Project: tcp   File: SqlInserts.java   View Source Code Vote up 6 votes
public static void insertAccionHasEtiqueta(AccionHasEtiqueta accionHasEtiqueta, String usuarioResponsable){
	try {
		Connection conn=ConnectionConfiguration.conectar();
	   	
		String query = " insert into accion_has_etiqueta (accion_id, etiqueta_id, proporcion,borrado, usuario_responsable)"
	+ " values (?, ?, ?, ?, ?)";
		
		PreparedStatement insert = conn.prepareStatement(query);
		
		insert.setInt (1, accionHasEtiqueta.getAccionId());
		insert.setInt (2, accionHasEtiqueta.getEtiquetaId());
		insert.setInt (3, accionHasEtiqueta.getProporcion());
		insert.setBoolean (4, accionHasEtiqueta.isBorrado());
		insert.setString (5, usuarioResponsable);					
		
		insert.execute();
		   
		conn.close();
	} catch (SQLException e) {e.printStackTrace();}
		
}
 
Example 11
Project: tcp   File: SqlInserts.java   View Source Code Vote up 6 votes
public static boolean insertInsLineaAccion(InsLineaAccion insLineaAccion, String usuarioResponsable){
	try {
		Connection conn=ConnectionConfiguration.conectar();
	   	
		String query = " insert into ins_linea_accion (linea_accion_id, institucion_id, periodo_id, meta, version, borrado, usuario_responsable)"
	+ " values (?, ?, ?, ?, ?, ?, ?)";
		
		PreparedStatement insert = conn.prepareStatement(query);
		
		//insert.setInt (1, insLineaAccion.getId());
		insert.setInt (1, insLineaAccion.getLineaAccionId());
		insert.setInt (2, insLineaAccion.getInstitucionId());
		insert.setInt (3, insLineaAccion.getPeriodoId());
		insert.setDouble (4, insLineaAccion.getMeta());
		insert.setInt(5, insLineaAccion.getVersion());
		insert.setBoolean (6, insLineaAccion.isBorrado());		
		insert.setString(7, usuarioResponsable);
		
		insert.execute();
				   
		conn.close();
		return true;
	} catch (SQLException e) {e.printStackTrace();return false;}
		
}
 
Example 12
Project: spanner-jdbc   File: TransactionTester.java   View Source Code Vote up 5 votes
private void insertRowInTest(long id) throws SQLException
{
	PreparedStatement ps = connection.prepareStatement(
			"INSERT INTO TEST (ID, UUID, ACTIVE, AMOUNT, DESCRIPTION, CREATED_DATE, LAST_UPDATED) VALUES (?, ?, ?, ?, ?, ?, ?)");
	ps.setLong(1, id);
	ps.setBytes(2, "FOO".getBytes());
	ps.setBoolean(3, true);
	ps.setDouble(4, 50d);
	ps.setString(5, "BAR");
	ps.setDate(6, new Date(1000l));
	ps.setTimestamp(7, new Timestamp(5000l));
	ps.executeUpdate();
}
 
Example 13
Project: spr   File: SqlUpdates.java   View Source Code Vote up 5 votes
public static boolean borradoInstitucion(Institucion objeto,
		String usuarioResponsable) {
	try {
		Connection conect = ConnectionConfiguration.conectar();

		String query = "update Institucion set ";
		PreparedStatement update = null;
		objeto.changeBorrado();

		query += " borrado = ?,";
		query += " usuario_responsable = ?,";

		query = query.substring(0, query.length() - 1);
		query += " where id = ? ";

		int cantCampos = 0;
		update = conect.prepareStatement(query);

		cantCampos++;
		update.setBoolean(cantCampos, objeto.isBorrado());
		cantCampos++;
		update.setString(cantCampos, usuarioResponsable);
		cantCampos++;
		update.setInt(cantCampos, objeto.getId());

		update.execute();
		conect.close();
		return true;
	} catch (SQLException e) {
		e.printStackTrace();
		return false;
	}

}
 
Example 14
Project: dev-courses   File: TestOdbcTypes.java   View Source Code Vote up 5 votes
public void testBooleanComplex() {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = netConn.prepareStatement(
            "INSERT INTO alltypes(id, b) VALUES(?, ?)");
        ps.setInt(1, 3);
        ps.setBoolean(2, false);
        assertEquals(1, ps.executeUpdate());
        ps.setInt(1, 4);
        assertEquals(1, ps.executeUpdate());
        ps.close();
        netConn.commit();
        ps = netConn.prepareStatement(
            "SELECT * FROM alltypes WHERE b = ?");
        ps.setBoolean(1, false);
        rs = ps.executeQuery();
        assertTrue("Got no rows with b = false", rs.next());
        assertEquals(Boolean.class, rs.getObject("b").getClass());
        assertTrue("Got only one row with b = false", rs.next());
        assertEquals(false, rs.getBoolean("b"));
        assertFalse("Got too many rows with b = false", rs.next());
    } catch (SQLException se) {
        junit.framework.AssertionFailedError ase
            = new junit.framework.AssertionFailedError(se.getMessage());
        ase.initCause(se);
        throw ase;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        } catch(Exception e) {
        }
    }
}
 
Example 15
Project: tcp   File: SqlInserts.java   View Source Code Vote up 5 votes
public static void insertActividad(Cronograma actividad, String usuarioResponsable){
	try {
		Connection conn=ConnectionConfiguration.conectar();
	   	
		String query = " insert into actividad (nombre,descripcion,proporcion,peso,version,borrado,accion_id,unidad_medida_id,hito_tipo_id,acumulable, usuario_responsable, prod_concat)"
	+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		PreparedStatement insert = conn.prepareStatement(query);
		
		insert.setString (1, actividad.getNombre());
		insert.setString (2, actividad.getDescripcion());
		insert.setDouble (3, actividad.getProporcion());
		insert.setDouble (4, actividad.getPeso());
		insert.setInt (5, actividad.getVersion());
		insert.setBoolean (6, actividad.isBorrado());
		insert.setInt (7, actividad.getAccion_id());
		insert.setInt (8, actividad.getUnidad_medida_id());
		insert.setInt (9, actividad.getHito_tipo_id());	
		insert.setBoolean(10, actividad.isAcumulable());		
		insert.setString (11, usuarioResponsable);
		insert.setString(12, actividad.getProdConcat());
		
		insert.execute();
		   
		conn.close();
	} catch (SQLException e) {e.printStackTrace();}
	
}
 
Example 16
Project: open-rmbt   File: BooleanField.java   View Source Code Vote up 5 votes
@Override
public void getField(final PreparedStatement ps, final int idx) throws SQLException
{
    if (value == null)
        ps.setNull(idx, Types.BOOLEAN);
    else
        ps.setBoolean(idx, value);
}
 
Example 17
Project: spr   File: SqlUpdates.java   View Source Code Vote up 4 votes
public static boolean updateProductoPresupuesto(ProductoPresupuesto objeto,
		String usuarioResponsable) {
	try {
		Connection conect = ConnectionConfiguration.conectar();
		String query = "update producto_presupusto set ";
		PreparedStatement update = null;

		query += " numero_fila  = ?, ";
		query += " anho  = ?, ";
		query += " version  = ?, ";
		query += " intermedio  = ?, ";
		query += " usuario_responsable  = ?, ";

		query = query.substring(0, query.length() - 2);
		query += " where id = ? ";

		int cantCampos = 0;

		update = conect.prepareStatement(query);

		cantCampos++;
		update.setInt(cantCampos, objeto.getNumero_fila());
		cantCampos++;
		update.setInt(cantCampos, objeto.getAnho());
		cantCampos++;
		update.setInt(cantCampos, objeto.getVersion());
		cantCampos++;
		update.setBoolean(cantCampos, objeto.isIntermedio());
		cantCampos++;
		update.setString(cantCampos, usuarioResponsable);

		cantCampos++;
		update.setInt(cantCampos, objeto.getId());

		update.execute();
		conect.close();
		return true;
	} catch (SQLException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 18
Project: jaffa-framework   File: FormDataWrapper.java   View Source Code Vote up 4 votes
private void addPrinterDefinitions(Connection connection)
throws SQLException {
    //1=PRINTER_ID, DESCRIPTION, SITE_CODE, LOCATION_CODE, REMOTE_PRINTER,
    //6=REAL_PRINTER_NAME, PRINTER_OPTION1, PRINTER_OPTION2, OUTPUT_TYPE, CREATED_ON
    //11=CREATED_BY, LAST_CHANGED_ON, LAST_CHANGED_BY
    String sql = "insert into J_PRINTER_DEFINITIONS values (?,?,?,?,?,  ?,?,?,?,?,  ?,?,?)";
    PreparedStatement pstmnt = connection.prepareStatement(sql);
 
    // Create first record
    pstmnt.setString(1, VAILD_PDF_PRINTER);
    pstmnt.setString(2, "Main Laser Printer");
    pstmnt.setString(3, "UTC");
    pstmnt.setString(4, "Copier Room");
    pstmnt.setBoolean(5, false);
    pstmnt.setString(6, REAL_PDF_PRINTER);
    pstmnt.setString(7, "-color");
    pstmnt.setString(8, null);
    pstmnt.setString(9, "PDF");
    pstmnt.setTimestamp(10, DateTime.addMonth(new  DateTime(), -2).timestamp() );
    pstmnt.setString(11, "user2");
    pstmnt.setTimestamp(12,(new DateTime()).timestamp());
    pstmnt.setString(13, "user3");
    pstmnt.execute();
    pstmnt.clearParameters();
    
    // Create second record
    pstmnt.setString(1, VAILD_LABEL_PRINTER);
    pstmnt.setString(2, "Intermec 3400D Label Printer");
    pstmnt.setString(3, "UTC");
    pstmnt.setString(4, "Main Hall");
    pstmnt.setBoolean(5, false);
    pstmnt.setString(6, "\\\\Hematite\\barcode1");
    pstmnt.setString(7, null);
    pstmnt.setString(8, null);
    pstmnt.setString(9, "Intermec 3400D");
    pstmnt.setTimestamp(10, DateTime.addMonth(new  DateTime(), -2).timestamp() );
    pstmnt.setString(11, "user2");
    pstmnt.setTimestamp(12, null);
    pstmnt.setString(13, null);
    pstmnt.execute();
    pstmnt.close();
}
 
Example 19
Project: sqlify   File: Sqlify.java   View Source Code Vote up 4 votes
private PreparedStatement applyParameterMapToPreparedStatement(
    PreparedStatement preparedStatement, 
    Map<String, Object> parameterMap, 
    List<String> parametersInSqlSorted) {
  try {
    for (int i = 0; i < parametersInSqlSorted.size(); i++) {
      Object value = parameterMap.get(parametersInSqlSorted.get(i));
      int positionInPreparedStatement = i + 1; // jdbc parameters start with 1...
      
      if (value instanceof BigDecimal) {
        preparedStatement.setBigDecimal(positionInPreparedStatement, (BigDecimal) value);
      } else if (value instanceof Boolean) {
        preparedStatement.setBoolean(positionInPreparedStatement, (Boolean) value);
      } else if (value instanceof Date) {
        preparedStatement.setDate(positionInPreparedStatement, (Date) value);
      } else if (value instanceof Double) {
        preparedStatement.setDouble(positionInPreparedStatement, (Double) value);
      } else if (value instanceof Float) {
        preparedStatement.setFloat(positionInPreparedStatement, (Float) value);
      } else if (value instanceof Integer) {
        preparedStatement.setInt(positionInPreparedStatement, (Integer) value);
      } else if (value instanceof Long) {
        preparedStatement.setLong(positionInPreparedStatement, (Long) value);
      } else if (value instanceof Short) {
        preparedStatement.setShort(positionInPreparedStatement, (Short) value);
      } else if (value instanceof String) {
        preparedStatement.setString(positionInPreparedStatement, (String) value);
      } else if (value instanceof Time) {
        preparedStatement.setTime(positionInPreparedStatement, (Time) value);
      } else if (value instanceof Timestamp) {
        preparedStatement.setTimestamp(positionInPreparedStatement, (Timestamp) value);
      } else if (value instanceof URL) {
        preparedStatement.setURL(positionInPreparedStatement, (URL) value);
      } else {
        // Kind of a fallback. If you expect some other behavior feel
        // free to implement it.
        preparedStatement.setObject(positionInPreparedStatement, value);
      }
    }
  } catch (SQLException ex) {
    throw new SqlifyException("Ops. An error occurred.", ex);
  }
  return preparedStatement;
}
 
Example 20
Project: lams   File: StdJDBCDelegate.java   View Source Code Vote up 2 votes
/**
 * Sets the designated parameter to the given Java <code>boolean</code> value.
 * This just wraps <code>{@link PreparedStatement#setBoolean(int, boolean)}</code>
 * by default, but it can be overloaded by subclass delegates for databases that
 * don't explicitly support the boolean type.
 */
protected void setBoolean(PreparedStatement ps, int index, boolean val) throws SQLException {
    ps.setBoolean(index, val);
}