是可以的 /** * A simple sample to demonstrate ResultSet.udpateRow(). */import java.sql.*;public class ResultSet4 { public static void main(String[] args) throws SQLException { // Load the Oracle JDBC driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); String url = "jdbc:oracle:oci8:@"; try { String url1 = System.getProperty("JDBC_URL"); if (url1 != null) url = url1; } catch (Exception e) { // If there is any security exception, ignore it // and use the default } // Connect to the database Connection conn = DriverManager.getConnection (url, "scott", "tiger"); // Create a Statement Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); // Query the EMP table ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP"); // Give everybody a $500 raise adjustSalary (rset, 500); // Verify the sarlary changes System.out.println ("Verify the changes with a new query: "); rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP"); while (rset.next()) { System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+ rset.getInt(3)); } System.out.println (); // Close the RseultSet rset.close(); // Close the Statement stmt.close(); // Cleanup cleanup(conn); // Close the connection conn.close(); } /** * Update the ResultSet content using updateRow(). */ public static void adjustSalary (ResultSet rset, int raise) throws SQLException { System.out.println ("Give everybody in the EMP table a $500 raise\n"); int salary = 0; while (rset.next ()) { // save the old value salary = rset.getInt (3); // update the row rset.updateInt (3, salary + raise);
// flush the changes to database rset.updateRow (); // show the changes System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+ salary+" -> "+rset.getInt(3)); } System.out.println (); } /** * Generic cleanup. */ public static void cleanup (Connection conn) throws SQLException { Statement stmt = conn.createStatement (); stmt.execute ("UPDATE EMP SET SAL = SAL - 500"); stmt.execute ("COMMIT"); stmt.close (); } }
/**
* A simple sample to demonstrate ResultSet.udpateRow().
*/import java.sql.*;public class ResultSet4
{
public static void main(String[] args) throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); String url = "jdbc:oracle:oci8:@";
try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
} // Connect to the database
Connection conn =
DriverManager.getConnection (url, "scott", "tiger"); // Create a Statement
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE); // Query the EMP table
ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP"); // Give everybody a $500 raise
adjustSalary (rset, 500); // Verify the sarlary changes
System.out.println ("Verify the changes with a new query: ");
rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP");
while (rset.next())
{
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
System.out.println (); // Close the RseultSet
rset.close(); // Close the Statement
stmt.close(); // Cleanup
cleanup(conn); // Close the connection
conn.close();
} /**
* Update the ResultSet content using updateRow().
*/
public static void adjustSalary (ResultSet rset, int raise)
throws SQLException
{
System.out.println ("Give everybody in the EMP table a $500 raise\n"); int salary = 0; while (rset.next ())
{
// save the old value
salary = rset.getInt (3); // update the row
rset.updateInt (3, salary + raise);
// flush the changes to database
rset.updateRow (); // show the changes
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
salary+" -> "+rset.getInt(3));
}
System.out.println ();
} /**
* Generic cleanup.
*/
public static void cleanup (Connection conn) throws SQLException
{
Statement stmt = conn.createStatement ();
stmt.execute ("UPDATE EMP SET SAL = SAL - 500");
stmt.execute ("COMMIT");
stmt.close ();
}
}
你的这段程序可以执行吗
rset.updateInt (3, salary + raise);可以正常更新吗??????
String url1 = System.getProperty("JDBC_URL");是不不因为用了这句????
你的ORACLE板本是多少
????
我的死活不行不管怎么设,总是只读!!!
1。要用oracle jdbc中class12类库,若一定要用class1.x.x类库,则要加import oracle.jdbc2.*;
2。select 语句不能用select * from yourtab 要用select col1,col2,... from yourtab
你用SQLPLUS能更新和写入记录吗。
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@pc16oang:1521:ORCL";
String user = "prox";
String password = "prox";
Connection conn = DriverManager.getConnection(url,user,password); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//this ResultSet can been Update,not readonly.
ResultSet rs = stmt.executeQuery("select name,price from fruit");
//rs.first();
rs.last();
rs.updateInt("price",177);
rs.updateRow();
System.out.println("Update had Completed!");
conn.commit(); stmt.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
} }
}