1 package nmp.dbms.JDBC; 2 3 import java.sql.*; 4 import nmp.dbms.*; 5 6 /*** 7 * Handle used to process transaction with multiple statements 8 */ 9 public class TransactionHandle 10 { 11 protected DBConnection con; 12 protected Jdbc_Sql database; 13 protected boolean inuse; 14 15 public TransactionHandle( Jdbc_Sql database, DBConnection con) throws SQLException { 16 this.database = database; 17 this.con = con; 18 con.setAutoCommit( false); 19 inuse = true; 20 } 21 22 /*** 23 * Executes a SQL query, returning all found rows 24 */ 25 public SQLVectorResult executeQuery( String sqlstring) 26 throws SQLException { 27 if( inuse) 28 return database.executeQuery( con, sqlstring, -1); 29 throw new SQLException( "Connection already released"); 30 } 31 32 /*** 33 * Executes a SQL query, returning the ResultSet 34 */ 35 public ResultSet executeRawQuery( String sqlstring) 36 throws SQLException { 37 if( inuse) 38 return database.executeRawQuery( con, sqlstring); 39 throw new SQLException( "Connection already released"); 40 } 41 42 /*** 43 * Executes a SQL query, returning only some of the found rows 44 * To fetch more rows you should do the following:<br> 45 * <p> SQLVectorResult result = executeQuery( "select * from X;", 10);<br> 46 * if( executeQuery( result,10) )<br>//some rows available<br>else<br>////no more rows 47 */ 48 public SQLVectorResult executeQuery( String sqlstring, int count) 49 throws SQLException { 50 if( inuse) 51 return database.executeQuery( con, sqlstring, count); 52 throw new SQLException( "Connection already released"); 53 } 54 55 /*** 56 * Gets more rows that conform to the executed query 57 */ 58 public boolean executeQuery( SQLVectorResult result, int count) 59 throws SQLException { 60 if( inuse) 61 return database.executeQuery( result, count); 62 throw new SQLException( "Connection already released"); 63 } 64 65 /*** 66 * Executes a SQL update, returning the number of modified rows 67 */ 68 public int executeUpdate(String sqlstring) 69 throws SQLException { 70 if( inuse) 71 return database.executeUpdate( con, sqlstring); 72 throw new SQLException( "Connection already released"); 73 } 74 75 /*** 76 * Releases the resources associated with this connection 77 */ 78 public void release() throws SQLException { 79 if( inuse) { 80 inuse = false; 81 con.setAutoCommit( true); 82 database.putConnection( con); 83 } 84 } 85 86 /*** 87 * Commits transaction and restarts a new transaction 88 */ 89 public void commit() throws SQLException { 90 if( inuse) 91 con.commit(); 92 } 93 94 /*** 95 * Rollbacks transaction and restarts a new transaction 96 */ 97 public void rollback() throws SQLException { 98 if( inuse) 99 con.rollback(); 100 } 101 102 103 }

This page was automatically generated by Maven