List of usage examples for java.sql PreparedStatement setObject
void setObject(int parameterIndex, Object x) throws SQLException;
Sets the value of the designated parameter using the given object.
From source file:org.apache.phoenix.query.KeyRangeClipTest.java
private static byte[] getRange(PhoenixConnection pconn, List<Object> startValues) throws SQLException { byte[] lowerRange; if (startValues == null) { lowerRange = KeyRange.UNBOUND;// w w w .j a va 2s . co m } else { String upsertValues = StringUtils.repeat("?,", startValues.size()).substring(0, startValues.size() * 2 - 1); String upsertStmt = "UPSERT INTO T VALUES(" + upsertValues + ")"; PreparedStatement stmt = pconn.prepareStatement(upsertStmt); for (int i = 0; i < startValues.size(); i++) { stmt.setObject(i + 1, startValues.get(i)); } stmt.execute(); Cell startCell = PhoenixRuntime.getUncommittedDataIterator(pconn).next().getSecond().get(0); lowerRange = CellUtil.cloneRow(startCell); pconn.rollback(); } return lowerRange; }
From source file:Main.java
public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException { int numRowsUpdated = 0; PreparedStatement ps = null; try {/*from w ww .ja v a 2s. c om*/ ps = connection.prepareStatement(sql); int i = 0; for (Object parameter : parameters) { ps.setObject(++i, parameter); } numRowsUpdated = ps.executeUpdate(); } finally { close(ps); } return numRowsUpdated; }
From source file:Main.java
public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException { List<Map<String, Object>> results = null; PreparedStatement ps = null; ResultSet rs = null;//w w w. j av a 2 s . c o m try { ps = connection.prepareStatement(sql); int i = 0; for (Object parameter : parameters) { ps.setObject(++i, parameter); } rs = ps.executeQuery(); results = map(rs); } finally { close(rs); close(ps); } return results; }
From source file:mangotiger.sql.SQL.java
private static PreparedStatement newPreparedStatement(final Connection connection, final String sql, final Object[] parameters) throws SQLException { if (log().isDebugEnabled()) { log().debug(asString(connection)); log().debug(asString(sql, parameters)); }//from ww w . j a va 2 s . c o m final PreparedStatement statement = connection.prepareStatement(sql); if (parameters != null) { for (int i = 0; i < parameters.length; ++i) { statement.setObject(i + 1, parameters[i]); } } return statement; }
From source file:com.ibm.research.rdf.store.jena.impl.DB2Graph.java
private static ResultSet getRS(PreparedStatement stmt, List<String> params, boolean isException) throws SQLException { for (int i = 0; i < params.size(); i++) { if (isException) { stmt.setObject(i + 1, null); } else {// www.j av a 2 s . c o m stmt.setObject(i + 1, params.get(i)); } } return stmt.executeQuery(); }
From source file:org.b3log.latke.repository.jdbc.util.JdbcUtil.java
/** * executeSql.//from ww w . ja v a2 s . c om * * @param sql sql * @param paramList paramList * @param connection connection * @return is success * @throws SQLException SQLException */ public static boolean executeSql(final String sql, final List<Object> paramList, final Connection connection) throws SQLException { LOGGER.log(Level.FINEST, "executeSql: {0}", sql); final PreparedStatement preparedStatement = connection.prepareStatement(sql); for (int i = 1; i <= paramList.size(); i++) { preparedStatement.setObject(i, paramList.get(i - 1)); } final boolean isSuccess = preparedStatement.execute(); preparedStatement.close(); return isSuccess; }
From source file:org.b3log.latke.repository.jdbc.util.JdbcUtil.java
/** * @param sql sql// ww w.jav a 2 s .c o m * @param paramList paramList * @param connection connection * @param ifOnlyOne ifOnlyOne to determine return object or array. * @param tableName tableName * * @return JSONObject * @throws SQLException SQLException * @throws JSONException JSONException * @throws RepositoryException respsitoryException */ private static JSONObject queryJson(final String sql, final List<Object> paramList, final Connection connection, final boolean ifOnlyOne, final String tableName) throws SQLException, JSONException, RepositoryException { LOGGER.log(Level.FINEST, "querySql: {0}", sql); final PreparedStatement preparedStatement = connection.prepareStatement(sql); for (int i = 1; i <= paramList.size(); i++) { preparedStatement.setObject(i, paramList.get(i - 1)); } final ResultSet resultSet = preparedStatement.executeQuery(); final JSONObject jsonObject = resultSetToJsonObject(resultSet, ifOnlyOne, tableName); preparedStatement.close(); return jsonObject; }
From source file:org.openadaptor.auxil.connector.jdbc.writer.map.AbstractMapWriter.java
/** * Setup arguments for a prepared statement. * <br>// w w w . ja va 2s . c o m * Note that it only requires the SQL type as setObject(x,null) cannot be used. * Seems silly, since the driver must be able to determine the type itself * for the null call. * @throws SQLException */ protected static void setArguments(PreparedStatement ps, Map map, String[] colNames, int[] sqlTypes) throws SQLException { for (int i = 1; i <= colNames.length; i++) { Object value = map.get(colNames[i - 1]); //Loop isn't zero based if (value != null || (sqlTypes == null)) { //Worth a try if sqlTypes ain't available! ps.setObject(i, value); //Don't need type here } else { ps.setNull(i, sqlTypes[i - 1]); } if (log.isDebugEnabled()) { log.debug("PreparedStatement arg [" + i + "] " + value); } } }
From source file:com.example.querybuilder.server.Jdbc.java
public static void setObject(PreparedStatement preparedStatement, int parameterNumber, Object value) { try {// ww w .jav a 2 s . c o m preparedStatement.setObject(parameterNumber, value); } catch (SQLException e) { throw new SqlRuntimeException(e); } }
From source file:org.eclipse.ecr.core.storage.sql.extensions.EmbeddedFunctions.java
/** * Checks if an id is a (strict) descendant of a given base id. * * @param conn the connection to the database * @param id the id to check for/*from w ww. ja v a2 s .co m*/ * @param baseId the base id */ public static boolean isInTree(Connection conn, Serializable id, Serializable baseId) throws SQLException { if (baseId == null || id == null || baseId.equals(id)) { // containment check is strict return false; } PreparedStatement ps = null; try { ps = conn.prepareStatement("SELECT PARENTID FROM HIERARCHY WHERE ID = ?"); do { ps.setObject(1, id); ResultSet rs = ps.executeQuery(); if (!rs.next()) { // no such id return false; } if (id instanceof String) { id = rs.getString(1); } else { id = Long.valueOf(rs.getLong(1)); } if (rs.wasNull()) { id = null; } rs.close(); if (baseId.equals(id)) { // found a match return true; } } while (id != null); // got to the root return false; } finally { if (ps != null) { ps.close(); } } }