繁体   English   中英

如何更新JdbcRowSet对象?

[英]How can update a JdbcRowSet object?

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

INSERT INTO作者(FirstName,LastName)VALUES('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)

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

RowSet.setCommand仅用于查询:

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

如果您确实想要使用行集,那么您可以按照可更新的ResultSet记录的方式更新或插入新行:

更新:

更新当前行中的列值。 在可滚动的ResultSet对象中,光标可以前后移动,移动到绝对位置,或移动到相对于当前行的位置。 以下代码片段更新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 

用于插入:

将列值插入插入行。 可更新的ResultSet对象具有与之关联的特殊行,该行用作构建要插入的行的临时区域。 以下代码片段将光标移动到插入行,构建一个三列行,并使用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(); 

请注意,根据我的经验, javax.sql.rowset的引用实现经常无法按预期工作。 使用普通JDBC可能会更好。

你缺少execute()

要使用“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();
}
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();

暂无
暂无

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

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