Creating a Statement Object
Once you connect to the database and, in the process, create your Connection
object, the next step is to create a Statement
object.
The createStatement
method of your JDBC Connection
object returns an object of the JDBC Statement
interface.
Below is an example of how to create the Statement
object:
Statement stmt = conn.createStatement( );
Note that there is nothing Oracle-specific about this statement; it follows standard JDBC syntax.
When the Connection
object first gets created, it is simply a direct link to the database.
You use a Connection
object to generate implementations of java.sql.Statement
tied to the same database transaction.
java.sql.Statement
contains methods to execute SQL statements directly against the database and to obtain the results.
Few comments about this command:
- SQL statements without parameters are normally executed using
Statement
objects.
If the same SQL statement is executed many times, it is more efficient to use a PreparedStatement
object.
- Only one
ResultSet
object per Statement
object can be open at any point in time.
Therefore, if the reading of one ResultSet
object is interleaved with the reading of another, each must have been generated by different Statement
objects.
- All statement execute methods implicitly close a statement’s current
ResultSet
object if an open one exists.