Executing the SQL Statement
Once a statement is provided, executing the statement encapsulated by the
RWDBOSql is as simple as calling its
execute() method. This call takes as a parameter the
RWDBConnection on which to execute the statement:
RWDBConnection aConn = aDb.connection(); // 1
RWDBOSql anOSql(“INSERT INTO MYTABLE VALUES(10)”); // 2
anOSql.execute(aConn); // 3
Let’s look at this example more closely. On
//1, we create an
RWDBConnection,
aConn, from an
RWDBDatabase object created earlier in the program. (For more information on this step, see the section “
Getting a Connection” in Chapter 4.) This
RWDBConnection encapsulates a single connection to our database. On
//2, we create an
RWDBOSql instance to encapsulate our SQL statement, in this case,
“INSERT INTO MYTABLE VALUES(10)”. On
//3, we use the
execute function to execute this statement on
aConn. When this function is invoked, the statement is prepared and immediately executed on our database through the connection
aConn.
One significant difference between
RWDBOSql and many other classes in the DB Interface Module is that its instances are
not produced by any other object. You can create an instance of
RWDBOSql any time, even before you connect to a database. You can pass instances of
RWDBOSql within your application, even if you are
not connected. Of course, when you want to invoke
execute() on your
RWDBOSql, you must have a valid
RWDBConnection on which to execute.
NOTE: You can create an
RWDBOSql any time, but you must establish a valid database connection to execute the encapsulated SQL statement.