- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用CachedRowSet
。但是当我调用 insertRow()
方法时,出现 SQLException
failed to insert row。
这是我的代码:
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/javapos";
static final String USERNAME = "root";
static final String PASSWORD = "sbc";
public static void main (String [] agr) throws SQLException
{
CachedRowSetImpl rs = new CachedRowSetImpl();
rs.setUrl(DATABASE_URL);
rs.setUsername(USERNAME);
rs.setPassword(PASSWORD);
rs.setCommand("select * from uom order by itemid");
rs.execute();
while(rs.next()){
System.out.println(rs.getString("itemid") + " - " + rs.getString("uom"));
}
rs.moveToInsertRow();
rs.updateString(2,"Sample code");
rs.insertRow();
rs.moveToCurrentRow();
rs.acceptChanges();
}
最佳答案
当您调用 insertRow()
时,CachedRowSet
的引用实现会检查是否已填充所有必需的列,否则会引发异常(来源来自 Grepcode CachedRowSet.insertRow()
,行号不完全匹配):
if (onInsertRow == false ||
insertRow.isCompleteRow(RowSetMD) == false) {
throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.failedins").toString());
}
检查在 InsertRow.isCompleteRow(RowSetMetaData)
中执行:
public boolean isCompleteRow(RowSetMetaData RowSetMD) throws SQLException {
for (int i = 0; i < cols; i++) {
if (colsInserted.get(i) == false &&
RowSetMD.isNullable(i + 1) == ResultSetMetaData.columnNoNulls) {
return false;
}
}
return true;
}
换句话说,插入行时,您必须为所有不可为空的列(包括主键)提供一个值。似乎有两种方法可以解决这个问题:
updateNull
将列显式设置为 null
。使用 setNull
不起作用:它会提供相同的错误,并且使用 setObject(idx, null)
会导致 NullPointerException
当使用您的代码进行这些更改时,我在调用 acceptChanges
时收到 SQLException
,因为该实现不会禁用 autoCommit
(似乎已经是 commented out ),但它确实显式调用 commit
(在 autoCommit
中时无效)。这似乎不容易解决,除非在 execute
上显式提供连接,或者创建您自己的实现。
我认为这类问题实际上表明了 RowSet
实现的实际使用量有多么少(否则它们早就被淘汰了)。
但请注意,如果这是您需要的实际代码,并且不需要 CachedRowSet
的断开连接特征,那么您可以简单地使用可更新的结果集。
关于java - CachedRowSet 插入行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819496/
我正在使用CachedRowSet。但是当我调用 insertRow() 方法时,出现 SQLException failed to insert row。 这是我的代码: static final
我想对两个 CachedRowSet 执行左连接。好像 JoinRowSetImpl 只支持 INNER JOIN 。我可以用什么来让左连接工作? 感谢和问候。 最佳答案 似乎以这种方式重建 Join
我正在开发一个应用程序,我的应用程序将从其他源以分隔格式(原始数据)获取数据,然后我需要将原始数据转换为结果集或缓存行集,然后我将构建的结果集/缓存行集传递给客户端应用程序(它调用我的应用程序来获取数
我想编写一个 java 函数,它接受一个 SQL 查询并返回一个 ResultSet 以供在别处处理。这无法完成,因为一旦连接关闭,ResultSet 就会失效。 谷歌搜索我发现了一个 VERY OL
我正在清理一个做得非常糟糕的大型网络应用程序。我无法一次性完成所有工作,所以我将其分成几个部分。我想做的一件事是实现一个带有接受 SQL 语句然后返回集合的函数的类。我的计划是,一旦解决了这个问题,我
我正在使用 CachedRowSetImpl,我可以从数据库中获取数据,但无法插入。 这是代码: public class NewClass { static final String DAT
我有以下 Java 7 代码来创建 CachedRowSet。 CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
我正在从可能发生变化的 View 中查询数据。在执行 crs.get******() 之前,我需要知道该列是否存在。 我发现我可以像这样查询元数据,在我从中请求数据之前查看列是否存在: ResultS
我正在使用 MySQL 数据库,我有一个包含 2 列的 Employee 表:Id(Int;主键)和 Name(String ).我已经编写了一些代码来向 Employee 表中插入一行,但是 acc
考虑以下代码片段: public void getUsers() { CachedRowSet rowSet = new CachedRowSetImpl(); .........
直到今天,我在处理查询结果时一直使用 ResultSet。但是今天我读了一些关于RowSet的内容和 CachedRowset我意识到它们可以更好地服务于我的目的。虽然在我阅读的所有示例中,RowSe
我正在使用我在代码中传递的javax.sql.rowset.CachedRowSet对象。在将 CachedRowSet 传递给我的代码的其余部分之前,填充它的 ResultSet 被关闭(仅用于阅读
我正在尝试通过jdbc类型4(microsoft jdbc 3.0)将图像文件(.png,200KB)插入Sql服务器(列类型varbinary(max)),这是我的代码: c
代码: OracleCachedRowSet rowSet = new OracleCachedRowSet(); ResultObject obj = new Resu
在我的 java 代码中,我使用 select 语句访问 oracle 数据库表。我收到很多行(大约 50.000 行),因此 rs.next() 需要一些时间来处理所有行。 using Result
我有一个 Derby SQL 数据库,其中有一个表,其中包含需要包含序列化对象的 blob 字段。我通过 JDBC 访问它。问题是,当我使用 ResultSet 反序列化对象时,一切正常,但如果我使用
我试图将一行插入到具有 CachedRowSet 增量 id 的表中(我正在使用 Java wit Java DB),但出现以下 SQLException: java.sql.SQLException
我很困惑什么是jdbcRowSet、CachedRowSet和WebRowSet。请给我最佳答案。 最佳答案 请参阅下面的所有三个示例。我想你会清楚地了解这些 RowSet接口(interface)。
我正在尝试使用 CachedRowSet 编写一个方法来执行 SQl 查询。示例代码如下(之前定义了用户和密码), public CachedRowSet getContentsOfCoffeesTa
我需要使用 CachedRowSet 或任何其他可能的方式复制 ResultSet 的帮助。我现在就在这个阶段 ResultSet rs = stmt.executeQuery(query); Cac
我是一名优秀的程序员,十分优秀!