protected Reader getReader(ResultSet rs) throws SenderException { try { return rs.getCharacterStream(1); } catch (SQLException e) { throw new SenderException(e); } }
@Override public Reader getClobReaderOrNull(int columnOneBased) { try { column = columnOneBased + 1; return rs.getCharacterStream(columnOneBased); } catch (SQLException e) { throw new DatabaseException(e); } }
@Override public Reader getClobReaderOrNull(String columnName) { try { column = rs.findColumn(columnName) + 1; return rs.getCharacterStream(columnName); } catch (SQLException e) { throw new DatabaseException(e); } }
@Override public Reader getCharacterStream(String columnName) throws SQLException { try { return _res.getCharacterStream(columnName); } catch (SQLException e) { handleException(e); return null; } }
public Object get(ResultSet rs, String name) throws SQLException { Reader stream = rs.getCharacterStream(name); if (stream == null) return toExternalFormat(null); CharArrayWriter writer = new CharArrayWriter(); for (; ; ) { try { int c = stream.read(); if (c == -1) return toExternalFormat(writer.toCharArray()); writer.write(c); } catch (IOException e) { throw new HibernateException("Unable to read character stream from rs"); } } }
@Override public void fire(Connection conn, ResultSet oldRow, ResultSet newRow) throws SQLException { StringBuilder buff = new StringBuilder(); if (oldRow != null) { buff.append("-").append(oldRow.getString("id")).append(';'); } if (newRow != null) { buff.append("+").append(newRow.getString("id")).append(';'); } if (!"TEST_INSERT".equals(triggerName)) { throw new RuntimeException("Wrong trigger name: " + triggerName); } if (!"TEST".equals(tableName)) { throw new RuntimeException("Wrong table name: " + tableName); } if (!"PUBLIC".equals(schemaName)) { throw new RuntimeException("Wrong schema name: " + schemaName); } if (type != (Trigger.INSERT | Trigger.UPDATE | Trigger.DELETE)) { throw new RuntimeException("Wrong type: " + type); } if (newRow != null) { if (oldRow == null) { if (newRow.getInt(1) != 1) { throw new RuntimeException("Expected: 1 got: " + newRow.getString(1)); } } else { if (newRow.getInt(1) != 2) { throw new RuntimeException("Expected: 2 got: " + newRow.getString(1)); } } newRow.getCharacterStream(2); newRow.getBinaryStream(3); newRow.updateInt(1, newRow.getInt(1) * 10); } conn.createStatement().execute("insert into message values('" + buff.toString() + "')"); }
/** 读取数据库带文本的一条记录 */ @Test public void getText() { Connection conn = null; Statement stmt = null; ResultSet rs = null; conn = JDBCUtils.getConnection(); try { stmt = conn.createStatement(); String sql = "select * from bt_user where id=2"; rs = stmt.executeQuery(sql); if (rs.next()) { Reader reader = rs.getCharacterStream("resume"); String path = "D:\\work\\Workspaces\\day14_jdbc\\src\\cn\\itcast\\mysql\\bt\\工作2.txt"; BufferedReader br = new BufferedReader(reader); OutputStream os = new FileOutputStream(path); Writer writer = new OutputStreamWriter(os, "utf-8"); String s = null; while ((s = br.readLine()) != null) { writer.write(s); writer.write("\n"); } writer.close(); os.close(); br.close(); reader.close(); } } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn, stmt, rs); } }
private void testClob() throws SQLException { trace("Test CLOB"); ResultSet rs; String string; stat = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE CLOB)"); stat.execute("INSERT INTO TEST VALUES(1,'Test')"); stat.execute("INSERT INTO TEST VALUES(2,'Hello')"); stat.execute("INSERT INTO TEST VALUES(3,'World!')"); stat.execute("INSERT INTO TEST VALUES(4,'Hallo')"); stat.execute("INSERT INTO TEST VALUES(5,'Welt!')"); stat.execute("INSERT INTO TEST VALUES(6,NULL)"); stat.execute("INSERT INTO TEST VALUES(7,NULL)"); rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID"); assertResultSetMeta( rs, 2, new String[] {"ID", "VALUE"}, new int[] {Types.INTEGER, Types.CLOB}, new int[] {10, Integer.MAX_VALUE}, new int[] {0, 0}); rs.next(); Object obj = rs.getObject(2); assertTrue(obj instanceof java.sql.Clob); string = rs.getString(2); assertTrue(string != null && string.equals("Test")); assertTrue(!rs.wasNull()); rs.next(); InputStreamReader reader = null; try { reader = new InputStreamReader(rs.getAsciiStream(2), "ISO-8859-1"); } catch (Exception e) { assertTrue(false); } string = readString(reader); assertTrue(!rs.wasNull()); trace(string); assertTrue(string != null && string.equals("Hello")); rs.next(); try { reader = new InputStreamReader(rs.getAsciiStream("value"), "ISO-8859-1"); } catch (Exception e) { assertTrue(false); } string = readString(reader); assertTrue(!rs.wasNull()); trace(string); assertTrue(string != null && string.equals("World!")); rs.next(); string = readString(rs.getCharacterStream(2)); assertTrue(!rs.wasNull()); trace(string); assertTrue(string != null && string.equals("Hallo")); rs.next(); string = readString(rs.getCharacterStream("value")); assertTrue(!rs.wasNull()); trace(string); assertTrue(string != null && string.equals("Welt!")); rs.next(); assertTrue(rs.getCharacterStream(2) == null); assertTrue(rs.wasNull()); rs.next(); assertTrue(rs.getAsciiStream("Value") == null); assertTrue(rs.wasNull()); assertTrue(rs.getStatement() == stat); assertTrue(rs.getWarnings() == null); rs.clearWarnings(); assertTrue(rs.getWarnings() == null); assertEquals(ResultSet.FETCH_FORWARD, rs.getFetchDirection()); assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency()); rs.next(); stat.execute("DROP TABLE TEST"); }
public Reader getCharacterStream(String columnName) throws SQLException { return rs.getCharacterStream(columnName); }
public Reader getCharacterStream(int columnIndex) throws SQLException { return rs.getCharacterStream(columnIndex); }
private void testUpdateDataType() throws Exception { Connection conn = getConnection(); Statement stat = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); stat.execute( "CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255), " + "DEC DECIMAL(10,2), BOO BIT, BYE TINYINT, BIN BINARY(100), " + "D DATE, T TIME, TS TIMESTAMP, DB DOUBLE, R REAL, L BIGINT, " + "O_I INT, SH SMALLINT, CL CLOB, BL BLOB)"); ResultSet rs = stat.executeQuery("SELECT * FROM TEST"); ResultSetMetaData meta = rs.getMetaData(); assertEquals("java.lang.Integer", meta.getColumnClassName(1)); assertEquals("java.lang.String", meta.getColumnClassName(2)); assertEquals("java.math.BigDecimal", meta.getColumnClassName(3)); assertEquals("java.lang.Boolean", meta.getColumnClassName(4)); assertEquals("java.lang.Byte", meta.getColumnClassName(5)); assertEquals("[B", meta.getColumnClassName(6)); assertEquals("java.sql.Date", meta.getColumnClassName(7)); assertEquals("java.sql.Time", meta.getColumnClassName(8)); assertEquals("java.sql.Timestamp", meta.getColumnClassName(9)); assertEquals("java.lang.Double", meta.getColumnClassName(10)); assertEquals("java.lang.Float", meta.getColumnClassName(11)); assertEquals("java.lang.Long", meta.getColumnClassName(12)); assertEquals("java.lang.Integer", meta.getColumnClassName(13)); assertEquals("java.lang.Short", meta.getColumnClassName(14)); assertEquals("java.sql.Clob", meta.getColumnClassName(15)); assertEquals("java.sql.Blob", meta.getColumnClassName(16)); rs.moveToInsertRow(); rs.updateInt(1, 0); rs.updateNull(2); rs.updateNull("DEC"); // 'not set' values are set to null assertThrows(ErrorCode.NO_DATA_AVAILABLE, rs).cancelRowUpdates(); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt(1, 1); rs.updateString(2, null); rs.updateBigDecimal(3, null); rs.updateBoolean(4, false); rs.updateByte(5, (byte) 0); rs.updateBytes(6, null); rs.updateDate(7, null); rs.updateTime(8, null); rs.updateTimestamp(9, null); rs.updateDouble(10, 0.0); rs.updateFloat(11, (float) 0.0); rs.updateLong(12, 0L); rs.updateObject(13, null); rs.updateShort(14, (short) 0); rs.updateCharacterStream(15, new StringReader("test"), 0); rs.updateBinaryStream(16, new ByteArrayInputStream(new byte[] {(byte) 0xff, 0x00}), 0); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 2); rs.updateString("NAME", "+"); rs.updateBigDecimal("DEC", new BigDecimal("1.2")); rs.updateBoolean("BOO", true); rs.updateByte("BYE", (byte) 0xff); rs.updateBytes("BIN", new byte[] {0x00, (byte) 0xff}); rs.updateDate("D", Date.valueOf("2005-09-21")); rs.updateTime("T", Time.valueOf("21:46:28")); rs.updateTimestamp("TS", Timestamp.valueOf("2005-09-21 21:47:09.567890123")); rs.updateDouble("DB", 1.725); rs.updateFloat("R", (float) 2.5); rs.updateLong("L", Long.MAX_VALUE); rs.updateObject("O_I", 10); rs.updateShort("SH", Short.MIN_VALUE); // auml, ouml, uuml rs.updateCharacterStream("CL", new StringReader("\u00ef\u00f6\u00fc"), 0); rs.updateBinaryStream("BL", new ByteArrayInputStream(new byte[] {(byte) 0xab, 0x12}), 0); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 3); rs.updateCharacterStream("CL", new StringReader("\u00ef\u00f6\u00fc")); rs.updateBinaryStream("BL", new ByteArrayInputStream(new byte[] {(byte) 0xab, 0x12})); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 4); rs.updateCharacterStream(15, new StringReader("\u00ef\u00f6\u00fc")); rs.updateBinaryStream(16, new ByteArrayInputStream(new byte[] {(byte) 0xab, 0x12})); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 5); rs.updateClob("CL", new StringReader("\u00ef\u00f6\u00fc")); rs.updateBlob("BL", new ByteArrayInputStream(new byte[] {(byte) 0xab, 0x12})); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 6); rs.updateClob(15, new StringReader("\u00ef\u00f6\u00fc")); rs.updateBlob(16, new ByteArrayInputStream(new byte[] {(byte) 0xab, 0x12})); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 7); rs.updateNClob("CL", new StringReader("\u00ef\u00f6\u00fc")); Blob b = conn.createBlob(); OutputStream out = b.setBinaryStream(1); out.write(new byte[] {(byte) 0xab, 0x12}); out.close(); rs.updateBlob("BL", b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 8); rs.updateNClob(15, new StringReader("\u00ef\u00f6\u00fc")); rs.updateBlob(16, b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 9); rs.updateNClob("CL", new StringReader("\u00ef\u00f6\u00fc"), -1); rs.updateBlob("BL", b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 10); rs.updateNClob(15, new StringReader("\u00ef\u00f6\u00fc"), -1); rs.updateBlob(16, b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 11); rs.updateNCharacterStream("CL", new StringReader("\u00ef\u00f6\u00fc"), -1); rs.updateBlob("BL", b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 12); rs.updateNCharacterStream(15, new StringReader("\u00ef\u00f6\u00fc"), -1); rs.updateBlob(16, b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 13); rs.updateNCharacterStream("CL", new StringReader("\u00ef\u00f6\u00fc")); rs.updateBlob("BL", b); rs.insertRow(); rs.moveToInsertRow(); rs.updateInt("ID", 14); rs.updateNCharacterStream(15, new StringReader("\u00ef\u00f6\u00fc")); rs.updateBlob(16, b); rs.insertRow(); rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID NULLS FIRST"); rs.next(); assertTrue(rs.getInt(1) == 0); assertTrue(rs.getString(2) == null && rs.wasNull()); assertTrue(rs.getBigDecimal(3) == null && rs.wasNull()); assertTrue(!rs.getBoolean(4) && rs.wasNull()); assertTrue(rs.getByte(5) == 0 && rs.wasNull()); assertTrue(rs.getBytes(6) == null && rs.wasNull()); assertTrue(rs.getDate(7) == null && rs.wasNull()); assertTrue(rs.getTime(8) == null && rs.wasNull()); assertTrue(rs.getTimestamp(9) == null && rs.wasNull()); assertTrue(rs.getDouble(10) == 0.0 && rs.wasNull()); assertTrue(rs.getFloat(11) == 0.0 && rs.wasNull()); assertTrue(rs.getLong(12) == 0 && rs.wasNull()); assertTrue(rs.getObject(13) == null && rs.wasNull()); assertTrue(rs.getShort(14) == 0 && rs.wasNull()); assertTrue(rs.getCharacterStream(15) == null && rs.wasNull()); assertTrue(rs.getBinaryStream(16) == null && rs.wasNull()); rs.next(); assertTrue(rs.getInt(1) == 1); assertTrue(rs.getString(2) == null && rs.wasNull()); assertTrue(rs.getBigDecimal(3) == null && rs.wasNull()); assertTrue(!rs.getBoolean(4) && !rs.wasNull()); assertTrue(rs.getByte(5) == 0 && !rs.wasNull()); assertTrue(rs.getBytes(6) == null && rs.wasNull()); assertTrue(rs.getDate(7) == null && rs.wasNull()); assertTrue(rs.getTime(8) == null && rs.wasNull()); assertTrue(rs.getTimestamp(9) == null && rs.wasNull()); assertTrue(rs.getDouble(10) == 0.0 && !rs.wasNull()); assertTrue(rs.getFloat(11) == 0.0 && !rs.wasNull()); assertTrue(rs.getLong(12) == 0 && !rs.wasNull()); assertTrue(rs.getObject(13) == null && rs.wasNull()); assertTrue(rs.getShort(14) == 0 && !rs.wasNull()); assertEquals("test", rs.getString(15)); assertEquals(new byte[] {(byte) 0xff, 0x00}, rs.getBytes(16)); rs.next(); assertTrue(rs.getInt(1) == 2); assertEquals("+", rs.getString(2)); assertEquals("1.20", rs.getBigDecimal(3).toString()); assertTrue(rs.getBoolean(4)); assertTrue((rs.getByte(5) & 0xff) == 0xff); assertEquals(new byte[] {0x00, (byte) 0xff}, rs.getBytes(6)); assertEquals("2005-09-21", rs.getDate(7).toString()); assertEquals("21:46:28", rs.getTime(8).toString()); assertEquals("2005-09-21 21:47:09.567890123", rs.getTimestamp(9).toString()); assertTrue(rs.getDouble(10) == 1.725); assertTrue(rs.getFloat(11) == (float) 2.5); assertTrue(rs.getLong(12) == Long.MAX_VALUE); assertEquals(10, ((Integer) rs.getObject(13)).intValue()); assertTrue(rs.getShort(14) == Short.MIN_VALUE); // auml ouml uuml assertEquals("\u00ef\u00f6\u00fc", rs.getString(15)); assertEquals(new byte[] {(byte) 0xab, 0x12}, rs.getBytes(16)); for (int i = 3; i <= 14; i++) { rs.next(); assertEquals(i, rs.getInt(1)); assertEquals("\u00ef\u00f6\u00fc", rs.getString(15)); assertEquals(new byte[] {(byte) 0xab, 0x12}, rs.getBytes(16)); } assertFalse(rs.next()); stat.execute("DROP TABLE TEST"); conn.close(); }
private void testClob(Connection conn) throws SQLException { trace("testClob"); Statement stat = conn.createStatement(); PreparedStatement prep; ResultSet rs; stat.execute("CREATE TABLE T_CLOB(ID INT PRIMARY KEY,V1 CLOB,V2 CLOB)"); StringBuilder asciiBuffer = new StringBuilder(); int len = getLength(); for (int i = 0; i < len; i++) { asciiBuffer.append((char) ('a' + (i % 20))); } String ascii1 = asciiBuffer.toString(); String ascii2 = "Number2 " + ascii1; prep = conn.prepareStatement("INSERT INTO T_CLOB VALUES(?,?,?)"); prep.setInt(1, 1); prep.setString(2, null); prep.setNull(3, Types.CLOB); prep.executeUpdate(); prep.clearParameters(); prep.setInt(1, 2); prep.setAsciiStream(2, null, 0); prep.setCharacterStream(3, null, 0); prep.executeUpdate(); prep.clearParameters(); prep.setInt(1, 3); prep.setCharacterStream(2, new StringReader(ascii1), ascii1.length()); prep.setCharacterStream(3, null, 0); prep.setAsciiStream(3, new ByteArrayInputStream(ascii2.getBytes()), ascii2.length()); prep.executeUpdate(); prep.clearParameters(); prep.setInt(1, 4); prep.setNull(2, Types.CLOB); prep.setString(2, ascii2); prep.setCharacterStream(3, null, 0); prep.setNull(3, Types.CLOB); prep.setString(3, ascii1); prep.executeUpdate(); prep.clearParameters(); prep.setInt(1, 5); prep.setObject(2, new StringReader(ascii1)); prep.setObject(3, new StringReader(ascii2), Types.CLOB, 0); prep.executeUpdate(); rs = stat.executeQuery("SELECT ID, V1, V2 FROM T_CLOB ORDER BY ID"); rs.next(); assertEquals(1, rs.getInt(1)); assertTrue(rs.getCharacterStream(2) == null && rs.wasNull()); assertTrue(rs.getAsciiStream(3) == null && rs.wasNull()); rs.next(); assertEquals(2, rs.getInt(1)); assertTrue(rs.getString(2) == null && rs.wasNull()); assertTrue(rs.getString(3) == null && rs.wasNull()); rs.next(); assertEquals(3, rs.getInt(1)); assertEquals(ascii1, rs.getString(2)); assertEquals(ascii2, rs.getString(3)); rs.next(); assertEquals(4, rs.getInt(1)); assertEquals(ascii2, rs.getString(2)); assertEquals(ascii1, rs.getString(3)); rs.next(); assertEquals(5, rs.getInt(1)); assertEquals(ascii1, rs.getString(2)); assertEquals(ascii2, rs.getString(3)); assertFalse(rs.next()); assertTrue(prep.getWarnings() == null); prep.clearWarnings(); assertTrue(prep.getWarnings() == null); assertTrue(conn == prep.getConnection()); }
public Reader getCharacterStream(String arg0) throws SQLException { return current.getCharacterStream(arg0); }