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

The following are Jave code examples for showing how to use setFloat() 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.
Example 1
Project: morpheus-core   File: SQL.java   Source Code and License Vote up 7 votes
/**
 * Binds arguments to prepared statement
 * @param stmt  the prepared statement reference
 * @return      the same as arg
 * @throws SQLException if binding fails
 */
private PreparedStatement bindArgs(PreparedStatement stmt) throws SQLException {
    for (int i=0; i<args.length; ++i) {
        final Object value = args[i];
        if      (value instanceof Boolean)          stmt.setBoolean(i+1, (Boolean)value);
        else if (value instanceof Short)            stmt.setShort(i+1, (Short)value);
        else if (value instanceof Integer)          stmt.setInt(i+1, (Integer)value);
        else if (value instanceof Float)            stmt.setFloat(i+1, (Float)value);
        else if (value instanceof Long)             stmt.setLong(i+1, (Long)value);
        else if (value instanceof Double)           stmt.setDouble(i+1, (Double)value);
        else if (value instanceof String)           stmt.setString(i+1, (String)value);
        else if (value instanceof java.sql.Date)    stmt.setDate(i+1, (java.sql.Date)value);
        else if (value instanceof Timestamp)        stmt.setTimestamp(i+1, (Timestamp)value);
        else if (value instanceof LocalDate)        stmt.setDate(i + 1, java.sql.Date.valueOf((LocalDate)value));
        else if (value instanceof LocalTime)        stmt.setTime(i+1, Time.valueOf((LocalTime)value));
        else if (value instanceof LocalDateTime)    stmt.setTimestamp(i+1, Timestamp.valueOf((LocalDateTime)value));
        else if (value instanceof ZonedDateTime) {
            final ZonedDateTime zonedDateTime = (ZonedDateTime)value;
            final LocalDateTime dateTime = zonedDateTime.toLocalDateTime();
            stmt.setTimestamp(i+1, Timestamp.valueOf(dateTime));
        } else {
            throw new RuntimeException("Unsupported argument, cannot be bound to SQL statement: " + value);
        }
    }
    return stmt;
}
 
Example 2
Project: AdvancedDataProfilingSeminar   File: PostgreSQLDataAccessObject.java   Source Code and License 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 3
Project: mcClans   File: QueryValue.java   Source Code and License 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 4
Project: Storage-System   File: ItemDAO.java   Source Code and License Vote up 6 votes
public void createItem(ItemCreation i) {
    Connection conn = null;
    try {
        conn = MysqlDAO.getInstance().connect();
        PreparedStatement statement = conn.prepareStatement(""+ "INSERT INTO `item` (`ID`, `name`, `dop`, `price`, `notes`) " + "VALUES   (?, ?, ?, ?, ?)");
        statement.setInt(1, i.getID());
        statement.setString(2, i.getName());
        statement.setString(3, i.getPurchasedate());
        statement.setFloat(4, i.getPrice());
        statement.setString(5, i.getNotes());
        statement.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(MysqlDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        MysqlDAO.getInstance().closeConnection(conn);   
    }
}
 
Example 5
Project: cuttlefish   File: DumpToDBWorker.java   Source Code and License Vote up 6 votes
private void setLinkAttr(Edge e, String attr, PreparedStatement st, int index) throws SQLException {
	if(attr.compareToIgnoreCase("id_origin") == 0) {
		st.setInt(index, e.getSource().getId() );
	} else if(attr.compareToIgnoreCase("id_dest") == 0) {
		st.setInt(index, e.getTarget().getId() );
	} else if(attr.compareToIgnoreCase("weight") == 0) {
		st.setFloat(index, (float)e.getWeight() );
	} else if(attr.compareToIgnoreCase("label") == 0) {
		st.setString(index, e.getLabel() );
	} else if(attr.compareToIgnoreCase("width") == 0) {
		st.setFloat(index, (float)e.getWidth() );
	} else if(attr.compareToIgnoreCase("color") == 0) {
		st.setString(index, "" + ((double)e.getColor().getRed()/256.d)
			+","+((double)e.getColor().getGreen()/256.d)
			+","+((double)e.getColor().getBlue()/256.d));
	} else if(attr.compareToIgnoreCase("var1") == 0) {
		st.setString(index, e.getVar1() );
	} else if(attr.compareToIgnoreCase("var2") == 0) {
		st.setString(index, e.getVar2() );
	} else if(attr.compareToIgnoreCase("hide") == 0) {
		st.setBoolean(index, e.isExcluded() );
	}
}
 
Example 6
Project: ogo   File: PlaceOrderDao.java   Source Code and License Vote up 6 votes
public static boolean placeOrder(String orderId, String customerId, String address, String payMode, String orderData, float amount) {
    boolean status = false;
    try {
        Class.forName("org.postgresql.Driver");
        Connection con = DriverManager.getConnection(com.sahilsunny.ogo.Main.getDbUri());
        PreparedStatement ps = con.prepareStatement("INSERT INTO orders VALUES (?, ?, ?, ?, ?, ?)");
        ps.setString(1, orderId);
        ps.setString(2, customerId);
        ps.setString(3, address);
        ps.setString(4, payMode);
        ps.setString(5, orderData);
        ps.setFloat(6, amount);
        int count = ps.executeUpdate();
        if (count > 0) {
            status = true;
        }
        ps.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return status;
}
 
Example 7
Project: cuttlefish   File: DumpToDBWorker.java   Source Code and License Vote up 6 votes
private void setLinkAttr(Edge e, String attr, PreparedStatement st, int index) throws SQLException {
	if(attr.compareToIgnoreCase("id_origin") == 0) {
		st.setInt(index, network.getSource(e).getId() );
	} else if(attr.compareToIgnoreCase("id_dest") == 0) {
		st.setInt(index, network.getDest(e).getId() );
	} else if(attr.compareToIgnoreCase("weight") == 0) {
		st.setFloat(index, (float)e.getWeight() );
	} else if(attr.compareToIgnoreCase("label") == 0) {
		st.setString(index, e.getLabel() );
	} else if(attr.compareToIgnoreCase("width") == 0) {
		st.setFloat(index, (float)e.getWidth() );
	} else if(attr.compareToIgnoreCase("color") == 0) {
		st.setString(index, "" + ((double)e.getColor().getRed()/256.d)
			+","+((double)e.getColor().getGreen()/256.d)
			+","+((double)e.getColor().getBlue()/256.d));
	} else if(attr.compareToIgnoreCase("var1") == 0) {
		st.setString(index, e.getVar1() );
	} else if(attr.compareToIgnoreCase("var2") == 0) {
		st.setString(index, e.getVar2() );
	} else if(attr.compareToIgnoreCase("hide") == 0) {
		st.setBoolean(index, e.isExcluded() );
	}
}
 
Example 8
Project: metanome-algorithms   File: PostgreSQLDataAccessObject.java   Source Code and License Vote up 5 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 9
Project: jigsaw-payment   File: JdbcProtobufTemplate.java   Source Code and License Vote up 5 votes
/**
 * set preparedstatement params
 * 
 * @param ps
 * @param args
 * @return
 * @throws SQLException
 */
private void populate(PreparedStatement ps, List<?> args)
		throws SQLException {
	for (int i = 0; i < args.size(); i++) {
		Object o = args.get(i);
		if (o instanceof Integer) {
			ps.setInt(i + 1, (int) o);
		} else if (o instanceof Long) {
			ps.setLong(i + 1, (long) o);
		} else if (o instanceof String) {
			ps.setString(i + 1, (String) o);
		} else if (o instanceof Date) {
			ps.setDate(i + 1, (Date) o);
		} else if (o instanceof Float) {
			ps.setFloat(i + 1, (Float) o);
		} else if (o instanceof Double) {
			ps.setDouble(i + 1, (Double) o);
		} else if (o instanceof Date) {
			ps.setDate(i + 1, (Date) o);
		} else if (o instanceof Timestamp) {
			ps.setTimestamp(i + 1, (Timestamp) o);
		} else if (o instanceof Descriptors.EnumValueDescriptor) {
			ps.setInt(i + 1,
					((Descriptors.EnumValueDescriptor) o).getNumber());
		} else if(o instanceof Boolean){
			ps.setBoolean(i+1, (Boolean)o);
		} else {
			ps.setObject(i+1, o);
		}
	}
}
 
Example 10
Project: lams   File: RealTypeDescriptor.java   Source Code and License Vote up 5 votes
@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
	return new BasicBinder<X>( javaTypeDescriptor, this ) {
		@Override
		protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
			st.setFloat( index, javaTypeDescriptor.unwrap( value, Float.class, options ) );
		}
	};
}
 
Example 11
Project: McLink   File: PlayerUtil.java   Source Code and License Vote up 5 votes
public static void setMoney(String uuid, float newAmount) {
	PlayerUtil.initDataset(uuid);
	
	try {
		String query = "UPDATE users SET money = ? WHERE uuid = ?";
		PreparedStatement prepStmt = McLink.instance.database.getConnection().prepareStatement(query);
		prepStmt.setFloat(1, newAmount);
		prepStmt.setString(2, uuid);
		prepStmt.executeUpdate();
		prepStmt.close();
		McLink.instance.database.getConnection().commit();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 12
Project: cuttlefish   File: DumpToDBWorker.java   Source Code and License Vote up 5 votes
private void setNodeAttr(Vertex n, String attr, PreparedStatement st, int index) throws SQLException {
	if(attr.compareToIgnoreCase("id") == 0) {
		st.setInt(index, n.getId());
	} else if(attr.compareToIgnoreCase("label") == 0) {
		st.setString(index, n.getLabel());
	} else if(attr.compareToIgnoreCase("color") == 0) {
		st.setString(index, "" + ((double)n.getFillColor().getRed()/256.d)
			+","+((double)n.getFillColor().getGreen()/256.d)
			+","+((double)n.getFillColor().getBlue()/256.d));
	} else if(attr.compareToIgnoreCase("borderColor") == 0) {
		st.setString(index, "" + ((double)n.getBorderColor().getRed()/256.d)
			+","+((double)n.getBorderColor().getGreen()/256.d)
			+","+((double)n.getBorderColor().getBlue()/256.d));
	} else if(attr.compareToIgnoreCase("size") == 0) {
		st.setDouble(index, n.getSize());
	} else if(attr.compareToIgnoreCase("shape") == 0) {
		st.setString(index, n.getShape());
	} else if(attr.compareToIgnoreCase("width") == 0) {
		st.setInt(index, n.getWidth());
	} else if(attr.compareToIgnoreCase("hide") == 0) {
		st.setBoolean(index, n.isExcluded() );
	} else if(attr.compareToIgnoreCase("var1") == 0) {
		st.setString(index, n.getVar1() );
	} else if(attr.compareToIgnoreCase("var2") == 0) {
		st.setString(index, n.getVar2() );
	} else if(attr.compareToIgnoreCase("x") == 0) {
		st.setFloat(index, (float) n.getPosition().getX() );
	} else if(attr.compareToIgnoreCase("y") == 0) {
		st.setFloat(index, (float) n.getPosition().getY() );
	} else if(attr.compareToIgnoreCase("fixed") == 0) {
		st.setBoolean(index, n.isFixed() );
	}
}
 
Example 13
Project: aliyun-maxcompute-data-collectors   File: JdbcWritableBridge.java   Source Code and License Vote up 5 votes
public static void writeFloat(Float val, int paramIdx, int sqlType,
    PreparedStatement s) throws SQLException {
  if (null == val) {
    s.setNull(paramIdx, sqlType);
  } else {
    s.setFloat(paramIdx, val);
  }
}
 
Example 14
Project: mm-system-2017   File: DataAccess.java   Source Code and License Vote up 4 votes
public boolean editUser(User user) throws SQLException {
	PreparedStatement stm = c.prepareStatement(SQLStatements.selectUserById);
	stm.setInt(1, user.getId());
	ResultSet rs = stm.executeQuery();
	if (!rs.next()) { // user does not exist
		rs.close();
		stm.close();

		return false;
	}
	rs.close();
	stm.close();

	PreparedStatement stm2 = c.prepareStatement(SQLStatements.updateUserById);
	stm2.setString(1, user.getFirstName());
	stm2.setString(2, user.getLastName());
	stm2.setString(3, user.getPhoneNumber());
	stm2.setInt(4, user.getGender());
	stm2.setString(5, user.getAddress());
	stm2.setString(6, user.getNote());
	stm2.setString(7, user.getProfilePicture());
	stm2.setInt(8, user.getAreaId());
	stm2.setInt(9, user.getCityId());
	stm2.setInt(10, user.getId());
	stm2.executeUpdate();

	if (user.getType() == userType.TSOFEN || user.getType() == userType.ADMIN) {
		stm2.close();

		return true;
	}
	if (user.getType() == userType.MENTOR) {
		PreparedStatement stm3 = c.prepareStatement(SQLStatements.updateMentorById);

		stm3.setString(1, ((Mentor) user).getExperience());
		stm3.setString(2, ((Mentor) user).getRole());
		stm3.setInt(3, ((Mentor) user).getCompany());
		stm3.setString(4, ((Mentor) user).getVolunteering());
		stm3.setString(5, ((Mentor) user).getWorkHistory());
		stm3.setInt(6, user.getId());
		stm3.executeUpdate();

		stm3.close();

		return true;
	}

	if (user.getType() == userType.MENTEE) {
		PreparedStatement stm4 = c.prepareStatement(SQLStatements.updaterMenteeById);
		stm4.setFloat(1, ((Mentee) user).getRemainingSemesters());
		stm4.setString(2, ((Mentee) user).getGraduationStatus());
		stm4.setInt(3, ((Mentee) user).getAcademiclnstitution());
		stm4.setFloat(4, ((Mentee) user).getAverage());
		stm4.setString(5, ((Mentee) user).getAcademicDicipline());
		stm4.setString(6, ((Mentee) user).getAcademicDicipline2());
		stm4.setInt(7, ((Mentee) user).getSignedEULA() ? 1 : 0);
		stm4.setString(8, ((Mentee) user).getResume());
		stm4.setString(9, ((Mentee) user).getGradeSheet());
		stm4.setInt(10, user.getId());
		stm4.executeUpdate();

		stm4.close();
		return true;
	}

	return false;
}
 
Example 15
Project: ats-framework   File: SQLServerDbWriteAccess.java   Source Code and License Vote up 4 votes
public void insertCheckpointSummary(
                                     String name,

                                     int numRunning,
                                     int numPassed,
                                     int numFailed,

                                     int minResponseTime,
                                     float avgResponseTime,
                                     int maxResponseTime,

                                     float minTransferRate,
                                     float avgTransferRate,
                                     float maxTransferRate,
                                     String transferRateUnit,
                                     int loadQueueId,
                                     boolean closeConnection ) throws DatabaseAccessException {

    final String errMsg = "Unable to insert checkpoint summary '" + name + "' for load queue "
                          + loadQueueId;

    PreparedStatement perparedStatement = null;
    try {
        refreshInternalConnection();

        perparedStatement = connection.prepareStatement("INSERT INTO tCheckpointsSummary"
                                                        + " (name,numRunning,numPassed,numFailed,minResponseTime,avgResponseTime,maxResponseTime,minTransferRate,avgTransferRate,maxTransferRate,transferRateUnit,loadQueueId) "
                                                        + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
        perparedStatement.setString(1, name);

        perparedStatement.setInt(2, numRunning);
        perparedStatement.setInt(3, numPassed);
        perparedStatement.setInt(4, numFailed);

        perparedStatement.setInt(5, minResponseTime);
        perparedStatement.setFloat(6, avgResponseTime);
        perparedStatement.setInt(7, maxResponseTime);

        perparedStatement.setFloat(8, minTransferRate);
        perparedStatement.setFloat(9, avgTransferRate);
        perparedStatement.setFloat(10, maxTransferRate);

        perparedStatement.setString(11, transferRateUnit);

        perparedStatement.setInt(12, loadQueueId);

        int updatedRecords = perparedStatement.executeUpdate();
        if (updatedRecords != 1) {
            throw new DatabaseAccessException(errMsg);
        }
    } catch (SQLException e) {
        throw new DatabaseAccessException(errMsg, e);
    } finally {
        if (closeConnection) {
            DbUtils.close(connection, perparedStatement);
        } else {
            DbUtils.closeStatement(perparedStatement);
        }
    }
}
 
Example 16
Project: iDataBaseConnection   File: SQLPreparedParamUtil.java   Source Code and License Vote up 4 votes
private static void setValue(PreparedStatement preStmt,int index,Value value)
{
	try 
	{
		if(value.isStringValue())
		{
			preStmt.setString(index, value.getString_value());
		}
		else if(value.isIntValue())
		{
			preStmt.setInt(index, value.getInt_value());
		}
		else if(value.isDoubleValue())
		{
			preStmt.setDouble(index, value.getDouble_value());
		}
		else if(value.isBooleanValue())
		{
			preStmt.setBoolean(index, value.getBoolean_value());
		}
		else if(value.isBlobValue())
		{
			preStmt.setBlob(index, value.getBlob_value());
		}
		else if(value.isBytesValue())
		{
			preStmt.setBytes(index, value.getBytes_value());
		}
		else if(value.isLongValue())
		{
			preStmt.setLong(index, value.getLong_value());
		}
		else if(value.isFloatValue())
		{
			preStmt.setFloat(index, value.getFloat_value());
		}
		else if(value.isBigdecimalValue())
		{
			preStmt.setBigDecimal(index, value.getBigdecimal_value());
		}
		else if(value.isByteValue())
		{
			preStmt.setByte(index, value.getByte_value());
		}
		else if(value.isDateValue())
		{
			preStmt.setDate(index, value.getDate_value());
		}
		else if(value.isTimeValue())
		{
			preStmt.setTime(index, value.getTime_value());
		}
		else
		{
			preStmt.setObject(index, value.getObject_value());
		}
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		DBCException.logException(DBCException.E_PreparedStatement, e);
	}
	
}
 
Example 17
Project: tangyuan2   File: FloatTypeHandler.java   Source Code and License Vote up 4 votes
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Float parameter, JdbcType jdbcType) throws SQLException {
	ps.setFloat(i, parameter);
}
 
Example 18
Project: uavstack   File: DAOFactory.java   Source Code and License Vote up 4 votes
public ResultSet query(Object... args) throws SQLException {

            PreparedStatement st = null;
            ResultSet rs = null;
            Connection conn = null;

            try {
                conn = fac.getConnection();

                st = conn.prepareStatement(sqlTemplate);

                if (null != args && args.length > 0) {

                    int index = 1;
                    for (Object arg : args) {

                        if (String.class.isAssignableFrom(arg.getClass())) {
                            st.setString(index, (String) arg);
                        }
                        else if (Integer.class.isAssignableFrom(arg.getClass())) {
                            st.setInt(index, Integer.class.cast(arg));
                        }
                        else if (Float.class.isAssignableFrom(arg.getClass())) {
                            st.setFloat(index, Float.class.cast(arg));
                        }
                        else if (Double.class.isAssignableFrom(arg.getClass())) {
                            st.setDouble(index, Double.class.cast(arg));
                        }
                        else if (Long.class.isAssignableFrom(arg.getClass())) {
                            st.setLong(index, Long.class.cast(arg));
                        }
                        else if (BigDecimal.class.isAssignableFrom(arg.getClass())) {
                            st.setBigDecimal(index, BigDecimal.class.cast(arg));
                        }
                        else if (arg instanceof byte[]) {
                            st.setBytes(index, (byte[]) arg);
                        }

                        else {
                            throw new RuntimeException("Unkown arg type:" + arg.getClass().getName());
                        }

                        index++;
                    }

                }

                rs = st.executeQuery();

                return rs;
            }
            catch (SQLException e) {
                throw e;
            }
            finally {
                DAOThreadContext c = new DAOThreadContext(rs, conn, st);
                DAOThreadContext.setContext(c);
            }
        }
 
Example 19
Project: uavstack   File: DAOFactory.java   Source Code and License Vote up 4 votes
public boolean execute(String[] sqls, List<List<Object>> paramsList) throws SQLException {

            if (sqls.length != paramsList.size()) {
                return false;
            }

            Connection conn = fac.getConnection();
            conn.setAutoCommit(false);
            PreparedStatement st = null;
            try {

                for (int i = 0; i < sqls.length; i++) {
                    st = conn.prepareStatement(sqls[i]);
                    int index = 1;
                    for (Object arg : paramsList.get(i)) {

                        if (String.class.isAssignableFrom(arg.getClass())) {
                            st.setString(index, (String) arg);
                        }
                        else if (Integer.class.isAssignableFrom(arg.getClass())) {
                            st.setInt(index, Integer.class.cast(arg));
                        }
                        else if (Float.class.isAssignableFrom(arg.getClass())) {
                            st.setFloat(index, Float.class.cast(arg));
                        }
                        else if (Double.class.isAssignableFrom(arg.getClass())) {
                            st.setDouble(index, Double.class.cast(arg));
                        }
                        else if (Long.class.isAssignableFrom(arg.getClass())) {
                            st.setLong(index, Long.class.cast(arg));
                        }
                        else if (BigDecimal.class.isAssignableFrom(arg.getClass())) {
                            st.setBigDecimal(index, BigDecimal.class.cast(arg));
                        }
                        else if (arg instanceof byte[]) {
                            st.setBytes(index, (byte[]) arg);
                        }

                        else {
                            throw new RuntimeException("Unkown arg type:" + arg.getClass().getName());
                        }

                        index++;
                    }
                    st.executeUpdate();
                }
                conn.commit();
                return true;
            }
            catch (SQLException e) {
                throw e;
            }
            finally {
                DAOThreadContext c = new DAOThreadContext(conn, st);
                DAOThreadContext.setContext(c);

            }
        }
 
Example 20
Project: uavstack   File: DAOFactory.java   Source Code and License Vote up 2 votes
public void setParameter(PreparedStatement ps, int i, Object parameter) throws SQLException {

            ps.setFloat(i, ((Float) parameter).floatValue());
        }