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

The following are Jave code examples for showing how to use setArray() 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: OpenDiabetes   File: TestTypeConversion.java   Source Code and License Vote up 10 votes
public void testArrayA() {

        try {
            String ddl0 = "DROP TABLE ARRAYTEST IF EXISTS";
            String ddl1 = "CREATE TABLE ARRAYTEST(A INTEGER ARRAY)";
            String dml1 = "INSERT INTO ARRAYTEST VALUES(ARRAY[0,0])";
            String dml2 = "INSERT INTO ARRAYTEST VALUES ?";

            statement.execute(ddl0);
            statement.execute(ddl1);
            statement.execute(dml1);

            PreparedStatement ps      = connection.prepareStatement(dml2);
            Object[]          objects = new Object[] {
                "1", 3, 9
            };
            Array array = connection.createArrayOf("INTEGER", objects);

            ps.setArray(1, array);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            fail("array failure");
        }
    }
 
Example 2
Project: mark-framework   File: ArrayTypeHandler.java   Source Code and License Vote up 7 votes
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object[] parameter,
                                JdbcType jdbcType) throws SQLException {

    String typeName = null;
    if (parameter instanceof Integer[]) {
        typeName = TYPE_NAME_INTEGER;
    } else if (parameter instanceof String[]) {
        typeName = TYPE_NAME_VARCHAR;
    } else if (parameter instanceof Boolean[]) {
        typeName = TYPE_NAME_BOOLEAN;
    } else if (parameter instanceof Double[]) {
        typeName = TYPE_NAME_NUMERIC;
    }

    if (typeName == null) {
        throw new TypeException("ArrayTypeHandler parameter typeName error, your type is " + parameter.getClass().getName());
    }

    // 这3行是关键的代码,创建Array,然后ps.setArray(i, array)就可以了
    Connection conn = ps.getConnection();
    Array array = conn.createArrayOf(typeName, parameter);
    ps.setArray(i, array);
}
 
Example 3
Project: SqlSauce   File: ArrayListLongUserType.java   Source Code and License Vote up 6 votes
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
        throws HibernateException, SQLException {
    Connection connection = st.getConnection();

    if (value == null) {
        st.setNull(index, sqlTypes()[0]);
    } else {
        @SuppressWarnings("unchecked") ArrayList<Long> castObject = (ArrayList) value;

        Long[] longs = castObject.toArray(new Long[castObject.size()]);
        Array array = connection.createArrayOf("bigint", longs);

        st.setArray(index, array);
    }
}
 
Example 4
Project: uroborosql   File: Parameter.java   Source Code and License Vote up 6 votes
/**
 * PreparedStatementへのパラメータセット処理
 *
 * @param preparedStatement PreparedStatement
 * @param parameterIndex index
 * @param param オリジナルパラメータ
 * @param parameterMapperManager パラメータ変換管理クラス
 * @throws SQLException SQL例外
 */
private void setParameterObject(final PreparedStatement preparedStatement, final int parameterIndex,
		final Object param, final BindParameterMapperManager parameterMapperManager) throws SQLException {
	//JDBCの受け付ける型に変換
	Object jdbcParam = parameterMapperManager.toJdbc(param, preparedStatement.getConnection());
	if (Objects.equals(sqlType, SQL_TYPE_NOT_SET)) {
		if (jdbcParam instanceof java.sql.Array) {
			preparedStatement.setArray(parameterIndex, (java.sql.Array) jdbcParam);
		} else {
			preparedStatement.setObject(parameterIndex, jdbcParam);
		}
	} else {
		int targetSqlType = sqlType.getVendorTypeNumber();//各JDBCの対応状況が怪しいのでintで扱う
		if (jdbcParam != null) {
			if (jdbcParam instanceof java.sql.Array) {
				preparedStatement.setArray(parameterIndex, (java.sql.Array) jdbcParam);
			} else {
				preparedStatement.setObject(parameterIndex, jdbcParam, targetSqlType);
			}
		} else {
			preparedStatement.setNull(parameterIndex, targetSqlType);
		}
	}
}
 
Example 5
Project: postguice   File: TextArrayUserType.java   Source Code and License Vote up 5 votes
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
	if (value == null) {
		st.setNull(index, SQL_TYPES[0]);
	} else {
		final Array array = session.connection().createArrayOf("text", (String[])value);
		st.setArray(index, array);
	}
}
 
Example 6
Project: dev-courses   File: TestTypeConversion.java   Source Code and License Vote up 5 votes
public void testArrayA() {

        try {
            String ddl0 = "DROP TABLE ARRAYTEST IF EXISTS";
            String ddl1 = "CREATE TABLE ARRAYTEST(A INTEGER ARRAY)";
            String dml1 = "INSERT INTO ARRAYTEST VALUES(ARRAY[0,0])";
            String dml2 = "INSERT INTO ARRAYTEST VALUES ?";

            statement.execute(ddl0);
            statement.execute(ddl1);
            statement.execute(dml1);

            PreparedStatement ps      = connection.prepareStatement(dml2);
            Object[]          objects = new Object[] {
                "1", 3, 9
            };
            Array array = connection.createArrayOf("INTEGER", objects);

            ps.setArray(1, array);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            fail("array failure");
        }
    }
 
Example 7
Project: tangyuan2   File: ArrayTypeHandler.java   Source Code and License Vote up 4 votes
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
	ps.setArray(i, (Array) parameter);
}