Java 如何更新JdbcRowSet对象?

Java 如何更新JdbcRowSet对象?,java,mysql,jdbc,Java,Mysql,Jdbc,我想用一个JdbcRowSet对象执行以下SQL命令: 将值('Sue','Smith')插入作者(FirstName,LastName) 我知道我可以使用Connection和saments对象执行,但我想使用接口JdbcRowSet执行,因为一个JdbcRowSet对象在默认情况下是可更新和可滚动的 我的代码是: public class JdbcRowSetTest { public static final String DATABASE_URL = "jdbc:mysql://

我想用一个JdbcRowSet对象执行以下SQL命令:

将值('Sue','Smith')插入作者(FirstName,LastName)

我知道我可以使用Connection和saments对象执行,但我想使用接口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)

您缺少执行():

要使用“Select”语句进行查询并返回JdbcRowSet,请执行以下操作:

public void createJdbcRowSet(String url, String username, String password, String sql) {
    jdbcRs = new JdbcRowSetImpl();
    jdbcRs.setCommand(sql);
    jdbcRs.setUrl(url);
    jdbcRs.setUsername(username);
    jdbcRs.setPassword(password);
    jdbcRs.execute();
    // ...
}
更新:
获得返回的JdbcRowSet后,可以插入新行,如下例所示:

public void updateJdbcRowSet(String username, String password) {
    jdbcRs.moveToInsertRow();
    jdbcRs.updateString("USERNAME", "NewUser");
    jdbcRs.updateString("PASSWORD", "ENCRYPTED");
    jdbcRs.insertRow();
}

您不能使用
JdbcRowSet
来执行这样的insert语句。如果这是您想要的,那么您应该使用普通的
语句
PreparedStatement

仅用于查询:

将此
行集
对象的命令属性设置为给定的SQL查询。当行集从不支持命令的数据源(如电子表格)获取数据时,此属性是可选的。
参数:
cmd
-用于获取此行集对象数据的SQL查询;可能为空

如果确实要使用行集,则可以按照可更新行的文档化方式更新或插入新行:

更新:

更新当前行中的列值。在可滚动的
ResultSet
对象,光标可以前后移动到绝对位置 位置,或相对于当前行的位置。以下 代码片段更新文件第五行的
NAME
ResultSet
object
rs
,然后使用方法
updateRow
更新 从中导出
rs
的数据源表

rs.absolute(5); // moves the cursor to the fifth row of rs
rs.updateString("NAME", "AINSWORTH"); // updates the
// NAME column of row 5 to be AINSWORTH
rs.updateRow(); // updates the row in the data source
插入:

将列值插入到插入行中。可更新的
ResultSet
对象有一个与之关联的特殊行,该行用作暂存 用于生成要插入的行的区域。下面的代码片段 将光标移动到插入行,生成一个三列行,然后 使用以下方法将其插入rs和数据源表中
insertRow

rs.moveToInsertRow(); // moves cursor to the insert row
rs.updateString(1, "AINSWORTH"); // updates the
// first column of the insert row to be AINSWORTH
rs.updateInt(2,35); // updates the second column to be 35
rs.updateBoolean(3, true); // updates the third column to true
rs.insertRow();
rs.moveToCurrentRow();
请注意,在我的经验中,
javax.sql.rowset
的参考实现经常不能按预期工作。您最好使用普通JDBC

public static void main(String[] args) {

        try {
            RowSetFactory rowset =RowSetProvider.newFactory();
            JdbcRowSet jdbcrow=rowset.createJdbcRowSet();
            jdbcrow.setUrl(BD_Url);
            jdbcrow.setUsername(DB_User);   
            jdbcrow.setPassword(DB_password);
//this is your database connectivity like uername,password and url and
 driver in not imp in mysql 5.1.23 jar file 

            jdbcrow.setCommand("Select * from Demo");
            jdbcrow.execute();
//uppere parts is use to fetch the record from table



   System.out.println("--------------------Insert----------");
    jdbcrow.moveToInsertRow();
    jdbcrow.updateInt("ID", 115);
    jdbcrow.updateString("Username","Hitesh");
    jdbcrow.updateString("Password","Sir");
    jdbcrow.insertRow();
//向下代码用于更新值

System.out.println("=--------------------Update-------------------------");
jdbcrow.absolute(3);// 3rd row
jdbcrow.updateString("Password","Sirs" ); //colname password 
jdbcrow.updateRow();
对于删除,您可以使用

System.out.println("=-----------Delete-----------------------------");



    while (jdbcrow.next()) {  
            String id=jdbcrow.getString("Id"); 
         //here i getting  ID  record from my table
          System.out.println("s"   +id);
            if(id.equals("102")) { 
            //then i my Specifying that if my table iD is .eqauls to 102 then delete 
              that record you also used this type to updatetbale

                 jdbcrow.deleteRow();
                System.out.println("gaye");
                break;
            }
        }
        //if record delete then preform `enter code here`some operation
//用于从表中删除最后一条记录

System.out.println("=---------------delete-------------------------");
    jdbcrow.last();
    jdbcrow.deleteRow();

@Ezazel,它是有效的。将
e.printStackTrace()
添加到catch块,查看您遇到的错误SQLException错误:java.sql.SQLException:无法使用executeQuery()发出数据操作语句。您正在接受异常。请检查它是否没有引发异常,并向我们提供stacktrace.SQLException错误:java.sql.SQLException:无法使用executeQuery()发出数据操作语句。我以前甚至没有注意到它,但
setCommand
用于填充行集所需的select查询。你目前使用它的方式没有意义。更新/插入行集与使用(可更新的)
ResultSet
时完全相同。两者的javadoc还包括示例(您可能还需要检查
CachedRowSet
)。还请添加一些解释性文本,而不仅仅是代码。