【发布时间】:2015-12-27 20:04:32
【问题描述】:
我想用一个 JdbcRowSet 对象执行以下 SQL 命令:
插入作者(名字、姓氏)值('Sue'、'Smith')
我知道我可以使用 Connection 和 Satements 对象执行,但我想使用接口 JdbcRowSet 来执行此操作,因为默认情况下一个 JdbcRowSet 对象是可更新和可滚动的。
我的代码是:
public class JdbcRowSetTest
{
public static final String DATABASE_URL = "jdbc:mysql://localhost/books";
public static final String USERNAME = "Ezazel";
public static final String PASSWORD = "Ezazel";
public JdbcRowSetTest()
{
try
{
JdbcRowSet rowSet = new JdbcRowSetImpl();
rowSet.setUrl( DATABASE_URL );
rowSet.setUsername( USERNAME );
rowSet.setPassword( PASSWORD );
rowSet.setCommand( "INSERT INTO Authors (FirstName,LastName) VALUES ('Sue', 'Smith')" );
rowSet.execute();
}
catch( SQLException e )
{
}
}
public static void main( String[] args )
{
JdbcRowSetTest app = new JdbcRowSetTest ();
}
}
SQLException 错误:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:502)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2224)
at com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:582)
at JdbcRowSetTest.(JdbcRowSetTest.java:23)
at JdbcRowSetTest.main(JdbcRowSetTest.java:53)
【问题讨论】:
-
您正在吞食异常。请检查它是否没有抛出异常并为我们提供堆栈跟踪。
-
SQLException 错误:java.sql.SQLException:无法使用 executeQuery() 发出数据操作语句。
-
我以前什至没有注意到它,但
setCommand用于填充行集所需的 select 查询。您当前使用它的方式没有意义。更新/插入行集与使用(可更新的)ResultSet完全相同。两者的 javadoc 还包括示例(您可能还想查看CachedRowSet)。