Creating a JDBC Statement Object


Before you can issue SQL statements using JDBC, you need to create a JDBC Statement object of the class java.sql.Statement . A Statement object is used to represent a SQL statement, such as a DML statement ( SELECT , INSERT , UPDATE , or DELETE ) or a DDL statement (such as CREATE TABLE ). You ll learn how to issue both DML and DDL statements later in this chapter.

To create a Statement object, you use the createStatement() method of a Connectio n object. In the following example, a Statement object named myStatement is created using the createStatement() method of the myConnection object that was created in the previous section:

 Statement myStatement = myConnection.createStatement(); 

Depending on the SQL statement you want to perform, you use a different method in the Statement class to run the SQL statement. If you want to perform a SELECT statement, you use the executeQuery() method. If you want to perform an INSERT , UPDATE , or DELETE statement, you use the executeUpdate() method. If you don t know ahead of time which type of SQL statement is to be performed, you can use the execute() method, which may be used to perform SELECT , INSERT , UPDATE , or DELETE statements. You may also use the execute() method to perform DDL statements, as you ll learn later in this chapter.

There is another JDBC class that may be used to represent a SQL statement: the PreparedStatement class. This offers more advanced functionality than the Statement class; I will defer discussion of the PreparedStatement class until after I have discussed the use of the Statement class.

Once you have a Statement object, you re ready to issue SQL statements using JDBC.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net