简体   繁体   English

如何更新JdbcRowSet对象?

[英]How can update a JdbcRowSet object?

I want to execute the following SQL command with one JdbcRowSet object: 我想用一个JdbcRowSet对象执行以下SQL命令:

INSERT INTO Authors (FirstName,LastName) VALUES ('Sue', 'Smith') INSERT INTO作者(FirstName,LastName)VALUES('Sue','Smith')

I know that i can execute with Connection and Satements objects, but i want to do this with the interface JdbcRowSet, because one JdbcRowSet object is updatable and scrollable by default. 我知道我可以使用Connection和Satements对象执行,但我想使用接口JdbcRowSet执行此操作,因为默认情况下一个JdbcRowSet对象是可更新和可滚动的。

My code is: 我的代码是:

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 error: 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)

You can't use a JdbcRowSet to execute insert statements like that. 您不能使用JdbcRowSet来执行这样的插入语句。 If that is what you want, then you should use a normal Statement or PreparedStatement instead. 如果这是你想要的,那么你应该使用普通的StatementPreparedStatement

The RowSet.setCommand is for queries only: RowSet.setCommand仅用于查询:

Sets this RowSet object's command property to the given SQL query. 将此RowSet对象的command属性设置为给定的SQL查询。 This property is optional when a rowset gets its data from a data source that does not support commands, such as a spreadsheet. 当行集从不支持命令的数据源(如电子表格)获取其数据时,此属性是可选的。
Parameters: 参数:
cmd - the SQL query that will be used to get the data for this RowSet object; cmd - 将用于获取此RowSet对象的数据的SQL查询; may be null 可能是null

If you really want to use a row set, then you can update or insert new rows in the way documented for an updatable ResultSet : 如果您确实想要使用行集,那么您可以按照可更新的ResultSet记录的方式更新或插入新行:

Updating: 更新:

to update a column value in the current row. 更新当前行中的列值。 In a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an absolute position, or to a position relative to the current row. 在可滚动的ResultSet对象中,光标可以前后移动,移动到绝对位置,或移动到相对于当前行的位置。 The following code fragment updates the NAME column in the fifth row of the ResultSet object rs and then uses the method updateRow to update the data source table from which rs was derived. 以下代码片段更新ResultSet对象rs的第五行中的NAME列,然后使用方法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 

For inserting: 用于插入:

to insert column values into the insert row. 将列值插入插入行。 An updatable ResultSet object has a special row associated with it that serves as a staging area for building a row to be inserted. 可更新的ResultSet对象具有与之关联的特殊行,该行用作构建要插入的行的临时区域。 The following code fragment moves the cursor to the insert row, builds a three-column row, and inserts it into rs and into the data source table using the method insertRow . 以下代码片段将光标移动到插入行,构建一个三列行,并使用insertRow方法将其插入到rs中并插入到数据源表中。

 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(); 

Note that the reference implementation of javax.sql.rowset in my experience regularly doesn't work as expected. 请注意,根据我的经验, javax.sql.rowset的引用实现经常无法按预期工作。 You might be better off using plain JDBC. 使用普通JDBC可能会更好。

You are missing execute() : 你缺少execute()

To query using "Select" statement and return a JdbcRowSet: 要使用“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();
    // ...
}

Update: 更新:
Once you have the returned JdbcRowSet, you can insert a new row as this example shows: 获得返回的JdbcRowSet后,可以插入一个新行,如下例所示:

public void updateJdbcRowSet(String username, String password) {
    jdbcRs.moveToInsertRow();
    jdbcRs.updateString("USERNAME", "NewUser");
    jdbcRs.updateString("PASSWORD", "ENCRYPTED");
    jdbcRs.insertRow();
}
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();

//The down code is used for update the value //向下代码用于更新值

System.out.println("=--------------------Update-------------------------");
jdbcrow.absolute(3);// 3rd row
jdbcrow.updateString("Password","Sirs" ); //colname password 
jdbcrow.updateRow();

For Delete you can use 对于删除,您可以使用

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

//this is used to delete last record from table //这用于从表中删除最后一条记录

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM