Principle: ResultSet can be seen as a data structure after the JDBC and database interaction, containing the database table structure, after this structure, you can know its data type, field name, and the like. Update Updaterow (), INSERTROW () is implemented using two methods provided in JDBC, and updates and inserts data. When UPDATE, first perform an air operation Select * from test where 1 <> 1, though not return data, but it takes the structure of the database table. Then you can update it with the value of the name of the parameter according to some clients. Similar techniques can be used when ClientDataSet in Delphi. (Thanks to my colleague Wang Zhifeng to my Delphi's enlightenment) The following is a simple example, including how to insert data in this way, and provide three ways performance comparison. / * * CREATED ON 2004-10-13 * /
/ ** * @Author liuwei * /
Import java.sql. *;
public class TestResultSet {/ ** * update * * @param s * @throws SQLException * / public static void updateRow (Statement s) throws SQLException {ResultSet rs = s.executeQuery ( "Select * from test"); rs.next (); Rs.UpdateString (1, "222"); rs.UpdateString (2, "liuwei"); rs.UpdateObject (3, new integer (3)); rs.Updaterow ();}
/ ** * Insert operation * * @Param s * @Throws SQLException * / public static void inSertrow (statement s) throws sqlexception {// reads an empty result set, from the structure of the table, the structure ResultSet RS = S.ExecuteQuery (" Select * from test where 1 <> 1 "); for (int i = 0; i <1000; i ) {r.movetoInsertrow (); rs.UpdateObject (1, new string (" 333 "); rs.UpdateObject (2, New String ("leo"); rs.UpdateObject (3, New Integer (3)); rs.insertrow ();}}
public static void main (String [] args) {Connection con = null; try {Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver"); con = DriverManager .getConnection ( "jdbc: microsoft: sqlserver: // 127.0.0.1:1433; DatabaseName =pubs "," sa "," "); // Directly calls Execute's Operation Long Begin = System.currentTimeMillis (); Statement S = Con.CreateStatement (); for (int i = 0 i <1000; I ) S.Execute ("INSERT INTO TEST VALUES (" 333 ',' Leo ', 3) "); long end = system.currenttimemillis (); system.out.println (Begin - End); s.execute ( "delete from test"); performance test Statement ss // operating with ResultSet = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); begin = System.currentTimeMillis (); insertRow (ss); end = System.currenttimemillis (); system.out.println (begin - end); S.Execute ("delete from test"); // Diplomibility String SQL = "Insert Into Test Values ('333', ' Leo ', 3) "; begin = system.currenttimemillis (); statement s3 = con.createstatement (); for (INT i = 0; I <1000; I ) s3.addbatch (sql); s3.executebatch (); end = system.currenttimemillis (); system.out.println (begin - end); s.execute ("delete From test ");
/ * * While (rs.next ()) {system.out.println (rs.getstring (2));} * / con.close ();} catch (exception e) {E.PrintStackTrace ();}}
} As can be seen from the results, the performance of this approach is acceptable when the operation of reading the table structure is not considered (there is not much inductance with direct INSERT performance), but this way is exempt from the large number of code to spend SQL work.