Re: [問題] 程式執行到updateRow()會發生錯誤
程式碼如下
public class updateRS {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;user=sa;password=1234";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT * FROM HumanResources.Department;";
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(SQL);
rs.moveToInsertRow();
rs.updateString("Name", "Accounting");
rs.updateString("GroupName", "Executive General and Administration");
rs.updateString("ModifiedDate", "08/01/2006");
rs.insertRow();
SQL = "SELECT * FROM HumanResources.Department WHERE Name =
'Accounting';";
rs = stmt.executeQuery(SQL);
displayRow("ADDED ROW", rs);
//以上執行都沒有問題,也有看到新增的資料列
rs.first();
rs.updateString("GroupName", "Finance");
//在上一行之前可以印出一些測試文字
rs.updateRow();//這行之後就會跳出錯誤訊息
rs = stmt.executeQuery(SQL);
displayRow("UPDATED ROW", rs);
rs.first();
rs.deleteRow();
System.out.println("ROW DELETED");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
private static void displayRow(String title, ResultSet rs) {
try {
System.out.println(title);
while (rs.next()) {
System.out.println(rs.getString("Name") + " : " +
rs.getString("GroupName"));
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
第一次發文,不懂規矩請各位見諒。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.231.151.82
※ 編輯: satank 來自: 61.231.151.82 (09/03 13:30)
※ 編輯: satank 來自: 61.231.151.82 (09/03 13:37)
※ 編輯: satank 來自: 61.231.151.82 (09/03 13:38)
→
09/03 15:29, , 1F
09/03 15:29, 1F
→
09/03 15:53, , 2F
09/03 15:53, 2F
推
09/03 20:41, , 3F
09/03 20:41, 3F
→
09/03 20:46, , 4F
09/03 20:46, 4F
→
09/03 20:48, , 5F
09/03 20:48, 5F
→
09/03 20:50, , 6F
09/03 20:50, 6F
討論串 (同標題文章)