JDBC to operate a database of three objects: Statement: PreparedStatement: CallableStatement

2010-03-20  来源:本站原创  分类:Java  人气:249 

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.

Overview

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.

相关文章
  • JDBC to operate a database of three objects: Statement: PreparedStatement: CallableStatement 2010-03-20

    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: C

  • JDBC Example Statement, PreparedStatement, CallableStatement instances three methods 2010-11-08

    public void ListStudents () throws SQLException { int i, NoofColumns; String StNo, StFName, StLName; / / Initialize and load the JDBC-ODBC driver Class.forName ("jdbc.odbc.JdbcOdbcDriver"); / / Create a connection object Connection Ex1Con = Driv

  • JDBC examples Statement, PreparedStatement, CallableStatement instance the three methods 2010-11-08

    public void ListStudents () throws SQLException { int i, NoofColumns; String StNo, StFName, StLName; / / Initialize and load the JDBC-ODBC driver Class.forName ("jdbc.odbc.JdbcOdbcDriver"); / / Create connection object Connection Ex1Con = Driver

  • JDBC using the Statement, PreparedStatement, CallableStatement. Instances 2010-03-20

    java to operate the database to create Statement, PreparedStatement, CallableStatement. 1.Statement int max =0; String sql ="select max(subStr(no,10,10)) as max from INF_APPLY_RESULT"; Statement stmt = conn.createStatement(); ResultSet rs = stmt

  • jdbc of the Statement, PreparedStatement, CallableStatement 2010-02-04

    Statement each time you run sql statement, the database should be the implementation of sql statements to compile, it is best used only to perform a query and returns the results of the case, more efficient than PreparedStatement. PreparedStatement a

  • JDBC's Statement, PreparedStatement, CallableStatement difference 2011-03-07

    Statement each time you run the sql statement, the database should be compiled to execute sql statement, it is best to perform a query and returns only the results of the case, more efficient than PreparedStatement. PreparedStatement is precompiled,

  • Analysis using the Jdbc connect to the database when the objects used in 2010-04-27

    Have given a look at the above link msyql source of Jdbc. //------------------------------------------------------------------ import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import

  • JDBC Statement PreparedStatement CallableStatement 2014-12-13

    在 JDBC 应用程序中,JDBC 语句对象用于将 SQL 语句发送到数据库服务器.一个语句对象与一个连接相关联,应用程序与数据库服务器之间的通信由语句对象来处理. JDBC 中有三种类型的语句对象: 1. 常规语句(General statement) 2. 预置语句(Prepared statement) 3. 可调用语句(Callable statement) 语句对象与一个连接相关联,所以要创建一个语句对象,首先应该建立一个数据库连接.连接的获取见上一篇博客. 关于这三种语句,个人觉得最

  • jdbc connect to the database Daquan 2010-09-01

    Java Database Connectivity (JDBC) by a group of Java programming language with the class and interface. JDBC as a tool / database developers with a standard API, so that they can use pure Java API to write database applications. However, the interfac

  • Achieved using dynamic proxy to operate the database with the AOP 2008-06-02

    A few days ago wrote "using dynamic proxy to achieve AOP functionality," the article, which is a two examples of a log and the other is about initialization parameters, for the practical application of little practical application today to write

  • Java wrapper classes, JDBC to connect Oracle Database 2010-03-23

    Long time no use JDBC to connect the database of Oralce; today to use the project; on to write the next review Review! ~~~~~! ! ~

  • JDBC connect to the database on skills 2010-03-29

    Java Database Connectivity (JDBC) by a group of Java programming language with the class and interface. JDBC as a tool / database developers with a standard API, so that they can use pure Java API to write database applications. However, the interfac

  • Sql Server database commonly used command statement 2010-08-20

    Sql Server database commonly used command statement 2008-09-30 17:28 1. See the version of the database select @ @ version 2. View the database where the machine operating parameters exec master .. xp_msver 3. View the database startup parameters sp_

  • Using Spring and Hibernate frameworks to operate the database partition level 2010-06-08

    Translation of an article a few years ago, thought very good. http://www.jroller.com/kenwdelong/entry/horizontal_database_partitioning_with_spring Introduction About a year ago, I decided to expand our database level. In our database we have millions

  • Spring JDBC operations on Oracle10g database issue of RowSet 2009-10-30

    The use of Spring JDBC for Oracle10g database page when the anomaly occurred, an error message is as follows: [ERROR]2971656-2009-05-1815:38:24- [com.ieslab.idp.datamaint.service.impl.DataGridServiceImpl. buildGrid(DataGridServiceImpl.java: 171)]-[]-

  • A basic JDBC connect to the database 2009-10-29

    Recently tidy java previously used method of connecting to the database, first using Properties file link SQL Server database. Database connection class: DBUtil.java package com.jdbc.jdbcConnection; import java.io.IOException; import java.io.InputStr

  • JDBC operations on the database transaction 2010-05-04

    Method one : try {conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JDBCTEST "," username","userpwd"); conn.setAutoCommit(false);// To disable the automatic submission, setting rollbac

  • With jdbc insert. Read a database kind of garbled text string solution 2010-06-24

    First of all, mysql database, what are the binary storage, support any data, of course, including Chinese. You go to the command line insert into testtable values ('Chinese'); select * from testtable; All appear normal. However, while access to the C

  • jdbc connect to the database (change) 2010-07-21

    package org.craf; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class FilterData { private Connectio

  • JDBC operation objects using PreparedStatement instead of Statement 2010-07-24

    In JDBC applications, if you have a slight level of developer, you should always PreparedStatement instead of Statement. In other words, at any time not to use Statement. Based on the following reasons: 1. Code readability and maintainability. Althou