下面列出了java.sql.ResultSet#updateCharacterStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void updateClob(ResultSet rs, String column, Object clobUpdateHandle) throws SQLException, JdbcException {
File clobFile = (File)clobUpdateHandle;
try {
InputStream is = new FileInputStream(clobFile);
Reader isr = StreamUtil.getCharsetDetectingInputStreamReader(is);
rs.updateCharacterStream(column, isr, (int)clobFile.length()); // FIXME: should use character count instead of byte count!
} catch (Exception e) {
throw new JdbcException("cannot read clob for column ["+column+"] from file ["+clobFile.toString()+"]",e);
}
}
public void updateClob(ResultSet rs, int column, Object clobUpdateHandle) throws SQLException, JdbcException {
File clobFile = (File)clobUpdateHandle;
try {
InputStream is = new FileInputStream(clobFile);
Reader isr = StreamUtil.getCharsetDetectingInputStreamReader(is);
rs.updateCharacterStream(column, isr, (int)clobFile.length()); // FIXME: should use character count instead of byte count!
} catch (Exception e) {
throw new JdbcException("cannot read clob for column ["+column+"] from file ["+clobFile.toString()+"]",e);
}
}
public void testUpdateCharacterStreamLengthless()
throws IOException, SQLException {
String str = "This is the (\u0FFF\u1234) test string";
String strUpdated = "An updated (\u0FEF\u9876) test string";
// Insert test data
PreparedStatement psChar = prep("dLongVarchar");
psChar.setInt(1, key);
psChar.setCharacterStream(2, new StringReader(str));
psChar.execute();
psChar.close();
// Update test data
ResultSet rs = fetchUpd("dLongVarchar", key);
rs.next();
rs.updateCharacterStream(1, new StringReader(strUpdated));
rs.updateRow();
rs.close();
// Verify that update took place and is correct.
rs = fetch("dLongVarchar", key);
rs.next();
Reader updatedStr = rs.getCharacterStream(1);
for (int i=0; i < strUpdated.length(); i++) {
assertEquals("Strings differ at index " + i,
strUpdated.charAt(i),
updatedStr.read());
}
assertEquals("Too much data in stream", -1, updatedStr.read());
updatedStr.close();
}
public void testUpdateCharacterStreamLengthlessParameterName()
throws IOException, SQLException {
String str = "This is the (\u0FFF\u1234) test string";
String strUpdated = "An updated (\u0FEF\u9876) test string";
// Insert test data
PreparedStatement psChar = prep("dLongVarchar");
psChar.setInt(1, key);
psChar.setCharacterStream(2, new StringReader(str));
psChar.execute();
psChar.close();
// Update test data
ResultSet rs = fetchUpd("dLongVarchar", key);
rs.next();
rs.updateCharacterStream("dLongVarchar", new StringReader(strUpdated));
rs.updateRow();
rs.close();
// Verify that update took place and is correct.
rs = fetch("dLongVarchar", key);
rs.next();
Reader updatedStr = rs.getCharacterStream(1);
for (int i=0; i < strUpdated.length(); i++) {
assertEquals("Strings differ at index " + i,
strUpdated.charAt(i),
updatedStr.read());
}
assertEquals("Too much data in stream", -1, updatedStr.read());
updatedStr.close();
}
public void testUpdateCharacterStreamLengthlessParameterName()
throws IOException, SQLException {
String str = "This is the (\u0FFF\u1234) test string";
String strUpdated = "An updated (\u0FEF\u9876) test string";
// Insert test data
PreparedStatement psChar = prep("dLongVarchar");
psChar.setInt(1, key);
psChar.setCharacterStream(2, new StringReader(str));
psChar.execute();
psChar.close();
// Update test data
ResultSet rs = fetchUpd("dLongVarchar", key);
rs.next();
rs.updateCharacterStream("dLongVarchar", new StringReader(strUpdated));
rs.updateRow();
rs.close();
// Verify that update took place and is correct.
rs = fetch("dLongVarchar", key);
rs.next();
Reader updatedStr = rs.getCharacterStream(1);
for (int i=0; i < strUpdated.length(); i++) {
assertEquals("Strings differ at index " + i,
strUpdated.charAt(i),
updatedStr.read());
}
assertEquals("Too much data in stream", -1, updatedStr.read());
updatedStr.close();
}
public void testUpdateCharacterStreamLengthless()
throws IOException, SQLException {
String str = "This is the (\u0FFF\u1234) test string";
String strUpdated = "An updated (\u0FEF\u9876) test string";
// Insert test data
PreparedStatement psChar = prep("dLongVarchar");
psChar.setInt(1, key);
psChar.setCharacterStream(2, new StringReader(str));
psChar.execute();
psChar.close();
// Update test data
ResultSet rs = fetchUpd("dLongVarchar", key);
rs.next();
rs.updateCharacterStream(1, new StringReader(strUpdated));
rs.updateRow();
rs.close();
// Verify that update took place and is correct.
rs = fetch("dLongVarchar", key);
rs.next();
Reader updatedStr = rs.getCharacterStream(1);
for (int i=0; i < strUpdated.length(); i++) {
assertEquals("Strings differ at index " + i,
strUpdated.charAt(i),
updatedStr.read());
}
assertEquals("Too much data in stream", -1, updatedStr.read());
updatedStr.close();
}
public void testUpdateCharacterStreamLengthlessParameterName()
throws IOException, SQLException {
String str = "This is the (\u0FFF\u1234) test string";
String strUpdated = "An updated (\u0FEF\u9876) test string";
// Insert test data
PreparedStatement psChar = prep("dLongVarchar");
psChar.setInt(1, key);
psChar.setCharacterStream(2, new StringReader(str));
psChar.execute();
psChar.close();
// Update test data
ResultSet rs = fetchUpd("dLongVarchar", key);
rs.next();
rs.updateCharacterStream("dLongVarchar", new StringReader(strUpdated));
rs.updateRow();
rs.close();
// Verify that update took place and is correct.
rs = fetch("dLongVarchar", key);
rs.next();
Reader updatedStr = rs.getCharacterStream(1);
for (int i=0; i < strUpdated.length(); i++) {
assertEquals("Strings differ at index " + i,
strUpdated.charAt(i),
updatedStr.read());
}
assertEquals("Too much data in stream", -1, updatedStr.read());
updatedStr.close();
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnIndex() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream(1, new StringReader(""));
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream("label", new StringReader(""));
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnIndexWithIntegerLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream(1, new StringReader(""), 1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnLabelWithIntegerLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream("label", new StringReader(""), 1);
}
}
/**
* This methods tests the ResultSet interface method
* updateCharacterStream
*
* @throws SQLException if some error occurs while calling the method
*/
public void testUpdateCharacterStreamStringParameterName()
throws Exception {
String str = "Test data";
String str_for_update = "Test data used for update";
StringReader r = new StringReader(str);
StringReader r_for_update = new StringReader
(str_for_update);
//Prepared Statement used to insert the data
PreparedStatement ps_sb = prep("dLongVarchar");
ps_sb.setInt(1, key);
ps_sb.setCharacterStream(2,r,str.length());
ps_sb.executeUpdate();
ps_sb.close();
//Update operation
//use a different ResultSet variable so that the
//other tests can go on unimpacted
ResultSet rs1 = fetchUpd("dLongVarchar", key);
rs1.next();
rs1.updateCharacterStream("dLongVarchar",
r_for_update,
str_for_update.length());
rs1.updateRow();
rs1.close();
//Query to see whether the data that has been updated
//using the updateAsciiStream method is the same
//data that we expected
rs1 = fetch("dLongVarchar", key);
rs1.next();
StringReader r_ret = (StringReader)rs1.getCharacterStream(1);
char [] c_ret = new char[str_for_update.length()];
r_ret.read(c_ret);
String str_ret = new String(c_ret);
assertEquals("Error in updateCharacterStream" + str_ret,str_for_update,
str_ret);
rs1.close();
}
/**
* This methods tests the ResultSet interface method
* updateCharacterStream
*
* @throws SQLException if some error occurs while calling the method
*/
public void testUpdateCharacterStreamStringParameterName()
throws Exception {
String str = "Test data";
String str_for_update = "Test data used for update";
StringReader r = new StringReader(str);
StringReader r_for_update = new StringReader
(str_for_update);
//Prepared Statement used to insert the data
PreparedStatement ps_sb = prep("dLongVarchar");
ps_sb.setInt(1, key);
ps_sb.setCharacterStream(2,r,str.length());
ps_sb.executeUpdate();
ps_sb.close();
//Update operation
//use a different ResultSet variable so that the
//other tests can go on unimpacted
ResultSet rs1 = fetchUpd("dLongVarchar", key);
rs1.next();
rs1.updateCharacterStream("dLongVarchar",
r_for_update,
str_for_update.length());
rs1.updateRow();
rs1.close();
//Query to see whether the data that has been updated
//using the updateAsciiStream method is the same
//data that we expected
rs1 = fetch("dLongVarchar", key);
rs1.next();
StringReader r_ret = (StringReader)rs1.getCharacterStream(1);
char [] c_ret = new char[str_for_update.length()];
r_ret.read(c_ret);
String str_ret = new String(c_ret);
assertEquals("Error in updateCharacterStream" + str_ret,str_for_update,
str_ret);
rs1.close();
}
/**
* This methods tests the ResultSet interface method
* updateCharacterStream
*
* @throws SQLException if some error occurs while calling the method
*/
public void testUpdateCharacterStream()
throws Exception {
String str = "Test data";
String str_for_update = "Test data used for update";
StringReader r = new StringReader(str);
StringReader r_for_update = new StringReader
(str_for_update);
//Prepared Statement used to insert the data
PreparedStatement ps_sb = prep("dLongVarchar");
ps_sb.setInt(1,key);
ps_sb.setCharacterStream(2,r,str.length());
ps_sb.executeUpdate();
ps_sb.close();
//Update operation
//use a different ResultSet variable so that the
//other tests can go on unimpacted
ResultSet rs1 = fetchUpd("dLongVarchar", key);
rs1.next();
rs1.updateCharacterStream(1,r_for_update,str_for_update.length());
rs1.updateRow();
rs1.close();
//Query to see whether the data that has been updated
//using the updateAsciiStream method is the same
//data that we expected
rs1 = fetch("dLongVarchar", key);
rs1.next();
StringReader r_ret = (StringReader)rs1.getCharacterStream(1);
char [] c_ret = new char[str_for_update.length()];
r_ret.read(c_ret);
String str_ret = new String(c_ret);
assertEquals("Error in updateCharacterStream" +
str_ret,str_for_update,str_ret);
rs1.close();
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream("label", new StringReader(""));
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnLabelWithIntegerLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream("label", new StringReader(""), 1);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnIndexWithLongLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream(1, new StringReader(""), 1L);
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateCharacterStreamForColumnLabelWithLongLength() throws SQLException {
for (ResultSet each : resultSets) {
each.updateCharacterStream("label", new StringReader(""), 1L);
}
}
/**
* This methods tests the ResultSet interface method
* updateCharacterStream
*
* @throws SQLException if some error occurs while calling the method
*/
public void testUpdateCharacterStream()
throws Exception {
String str = "Test data";
String str_for_update = "Test data used for update";
StringReader r = new StringReader(str);
StringReader r_for_update = new StringReader
(str_for_update);
//Prepared Statement used to insert the data
PreparedStatement ps_sb = prep("dLongVarchar");
ps_sb.setInt(1,key);
ps_sb.setCharacterStream(2,r,str.length());
ps_sb.executeUpdate();
ps_sb.close();
//Update operation
//use a different ResultSet variable so that the
//other tests can go on unimpacted
ResultSet rs1 = fetchUpd("dLongVarchar", key);
rs1.next();
rs1.updateCharacterStream(1,r_for_update,str_for_update.length());
rs1.updateRow();
rs1.close();
//Query to see whether the data that has been updated
//using the updateAsciiStream method is the same
//data that we expected
rs1 = fetch("dLongVarchar", key);
rs1.next();
StringReader r_ret = (StringReader)rs1.getCharacterStream(1);
char [] c_ret = new char[str_for_update.length()];
r_ret.read(c_ret);
String str_ret = new String(c_ret);
assertEquals("Error in updateCharacterStream" +
str_ret,str_for_update,str_ret);
rs1.close();
}
/**
* This methods tests the ResultSet interface method
* updateCharacterStream
*
* @throws SQLException if some error occurs while calling the method
*/
public void testUpdateCharacterStreamStringParameterName()
throws Exception {
String str = "Test data";
String str_for_update = "Test data used for update";
StringReader r = new StringReader(str);
StringReader r_for_update = new StringReader
(str_for_update);
//Prepared Statement used to insert the data
PreparedStatement ps_sb = prep("dLongVarchar");
ps_sb.setInt(1, key);
ps_sb.setCharacterStream(2,r,str.length());
ps_sb.executeUpdate();
ps_sb.close();
//Update operation
//use a different ResultSet variable so that the
//other tests can go on unimpacted
ResultSet rs1 = fetchUpd("dLongVarchar", key);
rs1.next();
rs1.updateCharacterStream("dLongVarchar",
r_for_update,
str_for_update.length());
rs1.updateRow();
rs1.close();
//Query to see whether the data that has been updated
//using the updateAsciiStream method is the same
//data that we expected
rs1 = fetch("dLongVarchar", key);
rs1.next();
StringReader r_ret = (StringReader)rs1.getCharacterStream(1);
char [] c_ret = new char[str_for_update.length()];
r_ret.read(c_ret);
String str_ret = new String(c_ret);
assertEquals("Error in updateCharacterStream" + str_ret,str_for_update,
str_ret);
rs1.close();
}