JDBC有够慢的,出到了3.0了才有个Rowset能类似于windows下ADO的概念,却还是达不到ADO.net的水平。幸亏Java里ORM这块比较争气,趁着C#的ObjectSpace跳票,可以继续耀武扬威。
Rowset对比于ResultSet,除了不用保持Connecton外,更重要特点是能够类似于ADO的编程方式,直接对Row赋值来进行Insert与Update, 而不用写SQL语句。 在windows编程中经常可见这种模式,特别是ADO.net用的好时,ORM也只能算是锦上添花....
在oracle的sample代码中,rowset02.java完整demo了RowSet的ADO模式。
简化后的代码如下:
public class
RowSet02
{
public static void main (String []args)
{
try
{
OracleCachedRowSet crowset = new OracleCachedRowSet ();
crowset.setUrl (java:oracle:oci8:@);
crowset.setUsername ("hr" );
crowset.setPassword ("hr" );
/*Select*/
crowset.setCommand ("SELECT seatno, tdate, name, class FROM reservation" );
crowset.execute ();
System.out.println ("Seat no Travel Date Name Class" );
while (crowset.next ())
{
printRow (crowset);
}
crowset.setReadOnly (false );
/*Update*/
crowset.beforeFirst ();
if (crowset.absolute (2 ))
{
crowset.updateString (4, "Business" );
crowset.updateRow ();
}
/*Insert*/
crowset.beforeFirst ();
crowset.moveToInsertRow ();
crowset.updateInt (1, 107 );
crowset.updateDate (2, new Date (975915381774L ));
crowset.updateString (3, "Pluto" );
crowset.insertRow ();
/*Delete*/
crowset.beforeFirst ();
if (crowset.absolute (6 ))
{
crowset.deleteRow ();
}
crowset.acceptChanges ();
crowset.close ();
}
catch (SQLException ea)
{
ea.printStackTrace ();
}
}
}
{
public static void main (String []args)
{
try
{
OracleCachedRowSet crowset = new OracleCachedRowSet ();
crowset.setUrl (java:oracle:oci8:@);
crowset.setUsername ("hr" );
crowset.setPassword ("hr" );
/*Select*/
crowset.setCommand ("SELECT seatno, tdate, name, class FROM reservation" );
crowset.execute ();
System.out.println ("Seat no Travel Date Name Class" );
while (crowset.next ())
{
printRow (crowset);
}
crowset.setReadOnly (false );
/*Update*/
crowset.beforeFirst ();
if (crowset.absolute (2 ))
{
crowset.updateString (4, "Business" );
crowset.updateRow ();
}
/*Insert*/
crowset.beforeFirst ();
crowset.moveToInsertRow ();
crowset.updateInt (1, 107 );
crowset.updateDate (2, new Date (975915381774L ));
crowset.updateString (3, "Pluto" );
crowset.insertRow ();
/*Delete*/
crowset.beforeFirst ();
if (crowset.absolute (6 ))
{
crowset.deleteRow ();
}
crowset.acceptChanges ();
crowset.close ();
}
catch (SQLException ea)
{
ea.printStackTrace ();
}
}
}