[英]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
來執行這樣的插入語句。 如果這是你想要的,那么你應該使用普通的Statement
或PreparedStatement
。
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.