/** * Tests that the Reader got from a empty Clob reflects new data in the underlying Clob. * * @throws Exception */ public void testGetCharacterStreamCreateClob() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str = "Hi I am the insert String"; // The string reader corresponding to this // string that will be used in the comparison. StringReader r_string = new StringReader(str); // create the empty Clob. Clob clob = getConnection().createClob(); // Get the Reader from this // Clob Reader r_clob = clob.getCharacterStream(); // set the String into the clob. clob.setString(1, str); // Now compare the reader corresponding // to the string and the reader obtained // form the clob to see if they match. assertEquals(r_string, r_clob); }
/** * Tests that the InputStream got from a empty Clob reflects new data in the underlying Clob. * * @throws Exception */ public void testGetAsciiStreamCreateClob() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str = "Hi I am the insert String"; // Create the InputStream that will // be used for comparing the Stream // that is obtained from the Blob after // the update. ByteArrayInputStream str_is = new ByteArrayInputStream(str.getBytes("US-ASCII")); // create the empty Clob. Clob clob = getConnection().createClob(); // Get the InputStream from this // Clob InputStream is = clob.getAsciiStream(); // set the String into the clob. clob.setString(1, str); // Ensure that the Stream obtained from // the clob contains the expected bytes assertEquals(str_is, is); }
/** * Creates an item object from String parameters. * * @param conn connection to database * @param param map of parameters to set * @return an item object */ public static Item createItem(Connection conn, Map<String, String[]> param) throws SQLException { Item item = new Item(); item.setName(param.get("name")[0]); item.setPrice(Integer.parseInt(param.get("price")[0])); item.setQuantity(Integer.parseInt(param.get("quantity")[0])); Clob cl = conn.createClob(); cl.setString(1, param.get("description")[0]); item.setDescription(cl); item.setStringDescr(cl); return item; }
public static void saveKit(final Player p) { if (kit.get(p) != null) { ItemStack five = kit.get(p).getItem(5); ItemStack six = kit.get(p).getItem(6); kit.get(p).setItem(5, null); kit.get(p).setItem(6, null); if (!(MySQL.mySQLenabled())) { KitFile.getData().set("Kit." + p.getUniqueId(), SaveAndLoad.toString(kit.get(p))); KitFile.saveData(); KitFile.reloadData(); } else { String INSERT = "INSERT INTO CODKits VALUES(?, ?) ON DUPLICATE KEY UPDATE list=?"; try { Connection conn = MySQL.getConnection(); PreparedStatement ps = conn.prepareStatement(INSERT); ps.setString(1, p.getUniqueId().toString()); Clob clob = conn.createClob(); ArrayList<String> list = SaveAndLoad.toString(kit.get(p)); clob.setString(1, MySQL.listToString(list)); ps.setClob(2, clob); ps.setClob(3, clob); ps.executeUpdate(); ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } kit.get(p).setItem(5, five); kit.get(p).setItem(6, six); } else { return; } }
/** * Tests that the data updated in a Clob is always reflected in the InputStream got. Here the * updates into the Clob are done using both an OutputStream obtained from this Clob as well as * using Clob.setString. * * @throws Exception */ public void testGetAsciiStreamClobUpdates() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str1 = "Hi I am the insert string"; // Stores the byte array representation of // the insert string. byte[] str1_bytes = str1.getBytes(); // The String that will be used in the // second series of updates String str2 = "Hi I am the update string"; // create the empty Clob. Clob clob = getConnection().createClob(); // Get the InputStream from this // Clob before any writes happen. InputStream is_BeforeWrite = clob.getAsciiStream(); // Get an OutputStream from this Clob // into which the data can be written OutputStream os = clob.setAsciiStream(1); os.write(str1_bytes); // Doing a setString now on the Clob // should reflect the same extension // in the InputStream also. clob.setString((str1_bytes.length) + 1, str2); // Get the input stream from the // Clob after the update InputStream is_AfterWrite = clob.getAsciiStream(); // Now check if the two InputStreams // match assertEquals(is_BeforeWrite, is_AfterWrite); }
/** * Tests that the data updated in a Clob is always reflected in the Reader got. Here the updates * are done using both a Writer obtained from this Clob and using Clob.setString. * * @throws Exception */ public void testGetCharacterStreamClobUpdates() throws Exception { // The String that will be used // to do the inserts into the // Clob. String str1 = "Hi I am the insert string"; // The String that will be used in the // second series of updates String str2 = "Hi I am the update string"; // create the empty Clob. Clob clob = getConnection().createClob(); // Get the Reader from this // Clob Reader r_BeforeWrite = clob.getCharacterStream(); // Get a writer from this Clob // into which the data can be written Writer w = clob.setCharacterStream(1); char[] chars_str1 = new char[str1.length()]; str2.getChars(0, str1.length(), chars_str1, 0); w.write(chars_str1); // Doing a setString now on the Clob // should reflect the same extension // in the InputStream also. clob.setString((str1.length()) + 1, str2); // Now get the reader from the Clob after // the update has been done. Reader r_AfterWrite = clob.getCharacterStream(); // Now compare the two readers to see that they // contain the same data. assertEquals(r_BeforeWrite, r_AfterWrite); }
private static <T> void writeToSQLOutput( SQLOutput stream, Class<? extends T> type, DataType<T> dataType, T value) throws SQLException { if (value == null) { stream.writeObject(null); } else if (type == Blob.class) { stream.writeBlob((Blob) value); } else if (type == Boolean.class) { stream.writeBoolean((Boolean) value); } else if (type == BigInteger.class) { stream.writeBigDecimal(new BigDecimal((BigInteger) value)); } else if (type == BigDecimal.class) { stream.writeBigDecimal((BigDecimal) value); } else if (type == Byte.class) { stream.writeByte((Byte) value); } else if (type == byte[].class) { // [#1327] Oracle cannot serialise BLOBs as byte[] to SQLOutput // Use reflection to avoid dependency on OJDBC if (dataType.isLob()) { Blob blob = null; try { blob = on("oracle.sql.BLOB") .call( "createTemporary", on(stream).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.BLOB").get("DURATION_SESSION")) .get(); blob.setBytes(1, (byte[]) value); stream.writeBlob(blob); } finally { DefaultExecuteContext.register(blob); } } else { stream.writeBytes((byte[]) value); } } else if (type == Clob.class) { stream.writeClob((Clob) value); } else if (type == Date.class) { stream.writeDate((Date) value); } else if (type == Double.class) { stream.writeDouble((Double) value); } else if (type == Float.class) { stream.writeFloat((Float) value); } else if (type == Integer.class) { stream.writeInt((Integer) value); } else if (type == Long.class) { stream.writeLong((Long) value); } else if (type == Short.class) { stream.writeShort((Short) value); } else if (type == String.class) { // [#1327] Oracle cannot serialise CLOBs as String to SQLOutput // Use reflection to avoid dependency on OJDBC if (dataType.isLob()) { Clob clob = null; try { clob = on("oracle.sql.CLOB") .call( "createTemporary", on(stream).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.CLOB").get("DURATION_SESSION")) .get(); clob.setString(1, (String) value); stream.writeClob(clob); } finally { DefaultExecuteContext.register(clob); } } else { stream.writeString((String) value); } } else if (type == Time.class) { stream.writeTime((Time) value); } else if (type == Timestamp.class) { stream.writeTimestamp((Timestamp) value); } else if (type == YearToMonth.class) { stream.writeString(value.toString()); } else if (type == DayToSecond.class) { stream.writeString(value.toString()); } // else if (type.isArray()) { // stream.writeArray(value); // } else if (UNumber.class.isAssignableFrom(type)) { stream.writeString(value.toString()); } else if (ArrayRecord.class.isAssignableFrom(type)) { // [#1544] We can safely assume that localConfiguration has been // set on DefaultBindContext, prior to serialising arrays to SQLOut Connection connection = getDriverConnection(DefaultBindContext.LOCAL_CONFIGURATION.get()); ArrayRecord<?> arrayRecord = (ArrayRecord<?>) value; stream.writeArray( on(connection) .call("createARRAY", arrayRecord.getName(), arrayRecord.get()) .<Array>get()); } else if (EnumType.class.isAssignableFrom(type)) { stream.writeString(((EnumType) value).getLiteral()); } else if (MasterDataType.class.isAssignableFrom(type)) { Object key = ((MasterDataType<?>) value).getPrimaryKey(); writeToSQLOutput(stream, key.getClass(), key); } else if (UDTRecord.class.isAssignableFrom(type)) { stream.writeObject((UDTRecord<?>) value); } else { throw new UnsupportedOperationException("Type " + type + " is not supported"); } }
private void testClob(int length) throws Exception { Random r = new Random(length); char[] data = new char[length]; // Unicode problem: // The UCS code values 0xd800-0xdfff (UTF-16 surrogates) // as well as 0xfffe and 0xffff (UCS non-characters) // should not appear in conforming UTF-8 streams. // (String.getBytes("UTF-8") only returns 1 byte for 0xd800-0xdfff) for (int i = 0; i < length; i++) { char c; do { c = (char) r.nextInt(); } while (c >= 0xd800 && c <= 0xdfff); data[i] = c; } Clob c = conn.createClob(); Writer out = c.setCharacterStream(1); out.write(data, 0, data.length); out.close(); stat.execute("delete from test"); PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)"); prep.setInt(1, 1); prep.setClob(2, c); prep.execute(); c = conn.createClob(); c.setString(1, new String(data)); prep.setInt(1, 2); prep.setClob(2, c); prep.execute(); prep.setInt(1, 3); prep.setCharacterStream(2, new StringReader(new String(data))); prep.execute(); prep.setInt(1, 4); prep.setCharacterStream(2, new StringReader(new String(data)), -1); prep.execute(); NClob nc; nc = conn.createNClob(); nc.setString(1, new String(data)); prep.setInt(1, 5); prep.setNClob(2, nc); prep.execute(); prep.setInt(1, 5); prep.setNClob(2, new StringReader(new String(data))); prep.execute(); prep.setInt(1, 6); prep.setNClob(2, new StringReader(new String(data)), -1); prep.execute(); prep.setInt(1, 7); prep.setNString(2, new String(data)); prep.execute(); ResultSet rs; rs = stat.executeQuery("select * from test"); rs.next(); Clob c2 = rs.getClob(2); assertEquals(length, c2.length()); String s = c.getSubString(1, length); String s2 = c2.getSubString(1, length); while (rs.next()) { c2 = rs.getClob(2); assertEquals(length, c2.length()); s2 = c2.getSubString(1, length); assertEquals(s, s2); } }