1, create a Statement object
Established a connection to a particular database, after, you can use this connection to send SQL statements. Statement objects created with the Connection method createStatement, as shown in the following code snippet:
Connection con = DriverManager.getConnection(url, "sunny", ""); Statement stmt = con.createStatement();
Order to implement the Statement object, is sent to the database SQL statement that will be provided to the Statement as a parameter of the method:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table2");
2, using the Statement object execute the statement
Statement interface provides three kinds of implementation of the SQL statement means: executeQuery, executeUpdate, and execute. What a way to use SQL statements generated by the content of the decision.
ExecuteQuery method used to produce a single result set of statements, such as the SELECT statement.
ExecuteUpdate method used to execute INSERT, UPDATE, or DELETE statement, and SQL DDL (Data Definition Language) statements, such as CREATE TABLE and DROP TABLE. INSERT, UPDATE, or DELETE statement, the effect is to modify the table of zero or more rows in one or more columns. executeUpdate The return value is an integer indicating the number of rows affected (ie, updated count). For the CREATE TABLE or DROP TABLE statements, which do not operate the line, executeUpdate return value of the total is zero.
Execute method for performing return multiple result sets, multiple update counts, or both combined statements. Because most programmers do not need the advanced features, so this will be outlined in a separate section later in their life are introduced.
Implementation of the statement that all methods will be closed by the calling Statement object's current open result set (if it exists). This means that before the re-implementation of the Statement object, need to complete the current ResultSet object processing.
Should be noted that the Statement interface inherits all the methods of the PreparedStatement interface has its own executeQuery, executeUpdate, and execute methods. Statement object itself does not contain SQL statements, and therefore must give Statement.execute method provides SQL statement as a parameter. PreparedStatement object does not provide the SQL statement as a parameter to these methods, because they already contain pre-compiled SQL statement. CallableStatement objects inherit these methods PreparedStatement form. For these methods PreparedStatement or CallableStatement version, use the query parameters will throw SQLException.
3, statement completion
When the connection is in auto-commit mode, in which the statement is executed when completed, will automatically be submitted or restore. Statement has been implemented and all results are returned, ie, that have been completed. Returns a result set for the executeQuery method, retrieved all the rows in the ResultSet object statement completion. For the method executeUpdate, when it is executed to complete the statement. But the calling method execute in a few of the cases, all in the search result set or update count after it is generated statement complete.
Statement object is used to send SQL statements to the database. In fact there are three kinds of Statement objects, which are connected as a given SQL statement on the implementation of the package container: Statement, PreparedStatement (which inherited from Statement), and CallableStatement (it is inherited from the PreparedStatement). They are dedicated to sending a particular type of SQL statements: Statement object with no parameters for the implementation of a simple SQL statement; PreparedStatement object used to perform with or without IN parameters, pre-compiled SQL statement; CallableStatement object for the implementation of the database is stored procedure call.
Statement interface provides access to the results of the implementation of statements and the basic method. PreparedStatement interface to add methods to deal with IN parameters; and CallableStatement added to deal with OUT parameters.
Some DBMS will have been stored in the process of each statement as a separate statement; while others will be the entire process as a compound statement. Auto-commit is enabled, this difference becomes very important because it affects when the commit method is called. In the former case, each statement is submitted separately; In the latter case, all the statements submitted simultaneously.
4, close the Statement object
Statement object will automatically close the Java garbage collection. As a good programming style, no Statement object should be explicitly close them. This will be the immediate release of DBMS resources, help to avoid potential memory problems.