手动将数据添加到Java ResultSet [英] Manually add data to a Java ResultSet
问题描述
我不确定这是否是一个愚蠢的问题.但是可以手动将数据/值添加到Java结果集中吗?例如,如果我已经有一个已填充的ResultSet,是否可以在其上面添加更多数据?
I'm not sure if this might be a rather stupid question. But is it possible to manually add data/values into a java resultset? For instance if I already have an existing populated ResultSet, is there a way to add more data ontop of it?
//if this was possible for instance
ResultSet rs;
rs.setData("someValue");
谢谢!
推荐答案
您可以使用自定义实现包装任何JDBC ResultSet
:
You can wrap any JDBC ResultSet
with your custom implementation:
public class MyResultSet implements ResultSet {
// Delegate most implementations to the underlying database result set:
private final ResultSet delegate;
public MyResultSet(ResultSet delegate) {
this.delegate = delegate;
}
@Override
public int getInt(int index) throws SQLException {
return delegate.getInt(index);
}
// [... more delegate methods ...]
// Add custom methods
public void setData(Object someValue) { ... }
public Object getData() { ... }
}
您的自定义结果集的行为类似于任何其他结果集.从您的自定义结果集中读取数据的客户端代码将忽略您在幕后"对其执行的更改.换句话说,您可以假装某些数据可用
Your custom result set behaves like any other result set. Client code reading data from your custom result set will be oblivious of the changes you perform to it "under the hood". In other words, you can pretend that some data is available
public class MyResultSet implements ResultSet {
// [...]
@Override
public int getInt(int index) throws SQLException {
if (index == 3) {
return 42;
} else {
return delegate.getInt(index);
}
}
}
这篇关于手动将数据添加到Java ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!