4-Statement
This overview is taken from JDBCTM Database Access from JavaTM: A Tutorial and Annotated Reference. JavaSoft is currently preparing this book. This is a tutorial and an important reference manual for JDBC, which will be published by Addison-Wesley in the spring of 1997 as part of the Java series.
4.1 Overview
The Statement object is used to send SQL statements to the database. There are actually three Statement objects, which are used as the package containers for executing SQL statements on a given connection: Statement, PreparedStatement (inherited from Statement), and CallableStatement (inherited from PreparedStatement ). They are dedicated to sending specific types of SQL statements: the Statement object is used to execute simple SQL statements without parameters; the PreparedStatement object is used to execute pre-compiled SQL statements with or without the IN parameter; the CallableStatement object is used to call stored procedures of the database.
The Statement interface provides basic methods for executing statements and obtaining results. The PreparedStatement interface adds a method to process IN parameters, while the CallableStatement interface adds a method to process OUT parameters.
4.1.1 create a Statement object
After a connection is established to a specific database, the connection can be used to send SQL statements. The Statement object is created using the Connection method createStatement, as shown in the following code segment:
Connection con = DriverManager. getConnection (url, "sunny ","");
Statement stmt = con. createStatement ();
To execute the Statement object, the SQL Statement sent to the database will be provided to the Statement as a parameter:
ResultSet rs = stmt.exe cuteQuery ("SELECT a, B, c FROM Table2 ");
4.1.2 execute the Statement using the Statement object
The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate, and execute. The method used is determined by the content generated by the SQL statement.
The executeQuery method is used to generate a single result set statement, such as the SELECT statement.
ExecuteUpdate is used to execute INSERT, UPDATE, or DELETE statements and SQL DDL (Data Definition Language) statements, such as CREATE TABLE and DROP TABLE. The INSERT, UPDATE, or DELETE statement modifies one or more columns in zero or multiple rows of the table. The returned value of executeUpdate is an integer indicating the number of affected rows (that is, the update count ). For statements that do not operate on rows such as create table or drop table, the return value of executeUpdate is always zero.
Execute is used to execute a statement that returns multiple result sets, multiple update counts, or a combination of the two. Because most programmers do not need this advanced feature, this overview will be introduced later in a single section.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service