本文整理匯總了Java中java.sql.Array類的典型用法代碼示例。如果您正苦於以下問題:Java Array類的具體用法?Java Array怎麽用?Java Array使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Array類屬於java.sql包,在下文中一共展示了Array類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testArrayA
import java.sql.Array; //導入依賴的package包/類
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");
}
}
示例2: toSqlArray
import java.sql.Array; //導入依賴的package包/類
public Array toSqlArray ( final Connection connection, final Event event ) throws SQLException
{
final DateFormat isoDateFormat = new SimpleDateFormat ( isoDatePatterrn );
final String[] fields;
// array must be large enough to hold all attributes plus id and both time stamps
fields = new String[ ( event.getAttributes ().size () + 3 ) * 2];
// now populate values
fields[0] = "id";
fields[1] = event.getId ().toString ();
fields[2] = "sourceTimestamp";
fields[3] = isoDateFormat.format ( event.getSourceTimestamp () );
fields[4] = "entryTimestamp";
fields[5] = isoDateFormat.format ( event.getEntryTimestamp () );
int i = 6;
for ( final Entry<String, Variant> entry : event.getAttributes ().entrySet () )
{
fields[i] = entry.getKey ();
fields[i + 1] = entry.getValue ().toString ();
i += 2;
}
return connection.createArrayOf ( "text", fields );
}
示例3: shortArraysWithNull
import java.sql.Array; //導入依賴的package包/類
@Test public void shortArraysWithNull() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.SMALLINT, "SMALLINT", Rep.SHORT);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 5; i++) {
List<Short> elements = new ArrayList<>();
for (int j = 0; j < 4; j++) {
short value = (short) r.nextInt(Short.MAX_VALUE);
// 50% of the time, negate the value
if (0 == r.nextInt(2)) {
value *= -1;
}
elements.add(Short.valueOf(value));
}
elements.add(null);
arrays.add(createArray("SMALLINT", component, elements));
}
// Verify read/write
writeAndReadArrays(conn, "short_arrays", "SMALLINT", component, arrays,
PRIMITIVE_LIST_VALIDATOR);
}
}
示例4: put
import java.sql.Array; //導入依賴的package包/類
/**
* Stores the Array in static for username + connectionId
*
* @param array
* the Array to store
*/
public void put(Array array) {
debug("Creating an array for user: " + connectionKey);
if (array == null) {
throw new IllegalArgumentException("array is null!");
}
Set<Array> arraySet = arrayMap.get(connectionKey);
if (arraySet == null) {
arraySet = new LinkedHashSet<Array>();
}
arraySet.add(array);
arrayMap.put(connectionKey, arraySet);
}
示例5: test
import java.sql.Array; //導入依賴的package包/類
@Test
public void test() {
BindParameterMapperManager parameterMapperManager = new BindParameterMapperManager();
Array jdbcArray = newProxy(Array.class);
Double[] array = { Double.valueOf(111.11d), Double.valueOf(222.22d) };
Connection conn = newProxy(Connection.class, (proxy, method, args) -> {
if (method.getName().equals("createArrayOf")) {
assertThat(args[0], is("FLOAT"));
assertThat(args[1], is(array));
return jdbcArray;
}
return method.invoke(proxy, args);
});
assertThat(parameterMapperManager.toJdbc(array, conn), is(jdbcArray));
Object[] objArray = { Double.valueOf(333.33d), "A" };
assertThat(parameterMapperManager.toJdbc(objArray, conn), is(objArray));
}
示例6: convertToArray
import java.sql.Array; //導入依賴的package包/類
protected Array convertToArray(Connection con, Set<Access> accessTypes) throws SQLException {
if (accessTypes != null) {
Integer[] accessIds = new Integer[accessTypes.size()];
int j = 0;
for (Access access : accessTypes) {
accessIds[j++] = access.getId();
}
return con.createArrayOf("smallint", accessIds);
} else {
return null;
}
}
示例7: createArrayOf
import java.sql.Array; //導入依賴的package包/類
/**
* Factory method for creating Array objects.
* <p>
* <b>Note: </b>When <code>createArrayOf</code> is used to create an array object
* that maps to a primitive data type, then it is implementation-defined
* whether the <code>Array</code> object is an array of that primitive
* data type or an array of <code>Object</code>.
* <p>
* <b>Note: </b>The JDBC driver is responsible for mapping the elements
* <code>Object</code> array to the default JDBC SQL type defined in
* java.sql.Types for the given class of <code>Object</code>. The default
* mapping is specified in Appendix B of the JDBC specification. If the
* resulting JDBC type is not the appropriate type for the given typeName then
* it is implementation defined whether an <code>SQLException</code> is
* thrown or the driver supports the resulting conversion.
*
* @param typeName the SQL name of the type the elements of the array map to. The typeName is a
* database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This
* is the value returned by <code>Array.getBaseTypeName</code>
* @param elements the elements that populate the returned object
* @return an Array object whose elements map to the specified SQL type
* @throws SQLException if a database error occurs, the JDBC type is not
* appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection
* @throws SQLFeatureNotSupportedException if the JDBC driver does not support this data type
* @since 1.6
*/
//#ifdef JAVA6
public Array createArrayOf(String typeName,
Object[] elements) throws SQLException {
checkClosed();
if (typeName == null) {
throw JDBCUtil.nullArgument();
}
typeName = typeName.toUpperCase();
int typeCode = Type.getTypeNr(typeName);
if (typeCode < 0) {
throw JDBCUtil.invalidArgument(typeName);
}
Type type = Type.getDefaultType(typeCode);
if (type.isArrayType() || type.isLobType() || type.isRowType()) {
throw JDBCUtil.invalidArgument(typeName);
}
Object[] newData = new Object[elements.length];
try {
for (int i = 0; i < elements.length; i++) {
Object o = type.convertJavaToSQL(sessionProxy, elements[i]);
newData[i] = type.convertToTypeLimits(sessionProxy, o);
}
} catch (HsqlException e) {
throw JDBCUtil.sqlException(e);
}
return new JDBCArray(newData, type, this);
}
示例8: createArrayOf
import java.sql.Array; //導入依賴的package包/類
public Array createArrayOf(String typeName, Object[] elements) throws SQLException
{
try
{
return realConnection.createArrayOf(typeName, elements);
}
catch(SQLException s)
{
String methodCall = "createArrayOf(" + typeName + ", " + elements + ")";
reportException(methodCall, s, null);
throw s;
}
}
示例9: createDataTypesRows
import java.sql.Array; //導入依賴的package包/類
protected void createDataTypesRows(RowSet crs) throws SQLException {
Integer aInteger = 100;
String aChar = "Oswald Cobblepot";
Long aLong = Long.MAX_VALUE;
Short aShort = Short.MAX_VALUE;
Double aDouble = Double.MAX_VALUE;
BigDecimal aBigDecimal = BigDecimal.ONE;
Boolean aBoolean = false;
Float aFloat = Float.MAX_VALUE;
Byte aByte = Byte.MAX_VALUE;
Date aDate = Date.valueOf(LocalDate.now());
Time aTime = Time.valueOf(LocalTime.now());
Timestamp aTimeStamp = Timestamp.valueOf(LocalDateTime.now());
Array aArray = new StubArray("INTEGER", new Object[1]);
Ref aRef = new SerialRef(new StubRef("INTEGER", query));
byte[] bytes = new byte[10];
crs.moveToInsertRow();
crs.updateInt(1, aInteger);
crs.updateString(2, aChar);
crs.updateString(3, aChar);
crs.updateLong(4, aLong);
crs.updateBoolean(5, aBoolean);
crs.updateShort(6, aShort);
crs.updateDouble(7, aDouble);
crs.updateBigDecimal(8, aBigDecimal);
crs.updateFloat(9, aFloat);
crs.updateByte(10, aByte);
crs.updateDate(11, aDate);
crs.updateTime(12, aTime);
crs.updateTimestamp(13, aTimeStamp);
crs.updateBytes(14, bytes);
crs.updateArray(15, aArray);
crs.updateRef(16, aRef);
crs.updateDouble(17, aDouble);
crs.insertRow();
crs.moveToCurrentRow();
}
示例10: getArray
import java.sql.Array; //導入依賴的package包/類
public Array getArray(int parameterIndex) throws SQLException {
try {
if (this.wrappedStmt != null) {
return ((CallableStatement) this.wrappedStmt).getArray(parameterIndex);
}
throw SQLError.createSQLException("No operations allowed after statement closed", SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
} catch (SQLException sqlEx) {
checkAndFireConnectionError(sqlEx);
}
return null;
}
示例11: getNullableResult
import java.sql.Array; //導入依賴的package包/類
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
Array array = rs.getArray(columnName);
return array == null ? null : array.getArray();
}
示例12: fromSqlArray
import java.sql.Array; //導入依賴的package包/類
public Event fromSqlArray ( final Array array ) throws SQLException, ParseException
{
final DateFormat isoDateFormat = new SimpleDateFormat ( isoDatePatterrn );
final EventBuilder eb = Event.create ();
final String[] fields = (String[])array.getArray ();
for ( int i = 0; i < fields.length; i += 2 )
{
final String key = fields[i];
final String value = fields[i + 1];
if ( key.equals ( "id" ) )
{
eb.id ( UUID.fromString ( value ) );
}
else if ( key.equals ( "sourceTimestamp" ) )
{
eb.sourceTimestamp ( isoDateFormat.parse ( value ) );
}
else if ( key.equals ( "entryTimestamp" ) )
{
eb.entryTimestamp ( isoDateFormat.parse ( value ) );
}
else
{
eb.attribute ( key, VariantEditor.toVariant ( value ) );
}
}
return eb.build ();
}
示例13: getArray
import java.sql.Array; //導入依賴的package包/類
@SuppressWarnings("unchecked") @Override public Array getArray() throws SQLException {
final Object o = getObject();
if (o == null) {
return null;
}
if (o instanceof ArrayImpl) {
return (ArrayImpl) o;
}
// If it's not an Array already, assume it is a List.
return new ArrayImpl((List<Object>) o, this);
}
示例14: test06
import java.sql.Array; //導入依賴的package包/類
@Test(enabled = true)
public void test06() throws Exception {
Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast",
"Cappuccino"};
Array a = new StubArray("VARCHAR", coffees);
Object[] values = {a};
SQLInputImpl sqli = new SQLInputImpl(values, map);
Array a2 = sqli.readArray();
assertTrue(Arrays.equals((Object[]) a2.getArray(), (Object[]) a.getArray()));
assertTrue(a.getBaseTypeName().equals(a2.getBaseTypeName()));
}
示例15: test04
import java.sql.Array; //導入依賴的package包/類
@Test(enabled = true)
public void test04() throws Exception {
Object[] coffees = new Object[]{"Espresso", "Colombian", "French Roast",
"Cappuccino"};
Array a = new StubArray("VARCHAR", coffees);
outImpl.writeArray(a);
SerialArray sa = (SerialArray) results.get(0);
assertTrue(Arrays.equals(coffees, (Object[]) sa.getArray()));
assertTrue(a.getBaseTypeName().equals(sa.getBaseTypeName()));
}