JDBC : Java DataBase Connectivity, commonly referred to as JDBC, is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment.
JDBC allows multiple implementations to exist and be used by the same application. The API provides a mechanism for dynamically loading the correct Java packages and registering them with the JDBC Driver Manager. The Driver Manager is used as a connection factory for creating JDBC connections.
JDBC connections support creating and executing statements. These may be update statements such as SQL's CREATE, INSERT, UPDATE and DELETE, or they may be query statements such as SELECT. Additionally, stored procedures may be invoked through a JDBC connection. JDBC represents statements using one of the following classes:
Statement –Statement is object which send SQL query to database. Statement sends each and every time SQL statement to database server for execution. These Statements can be insert, update, delete, create table, select or any. Statement object can be created by connection objects with createStatement(). Statement is in java.sql.Statement.
PreparedStatement – Statement is simple SQL statement and takes no parameters, execute and compile every time when request is generated to database server.PreparedStatement is precompiled SQL statement and reside in PreparedStatement object. This PreparedStatement object executes multiple times SQL statement without compiling it again and again. This is kind of caching SQL statement and execute on parameters specification. First time when it executes, it runs slow but after that it runs much faster than simple Statement Object. Sometimes it is called dynamic statement because it takes parameter setter option.A SQL statement is given inside when PreparedStatement is creating. PreparedStatement is in java.sql.PreparedStatement
The main feature of a
PreparedStatement
object is that, unlike a
Statement
object, it is given a SQL statement when it is created. The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the
PreparedStatement
object contains not just a SQL statement, but a SQL statement that has been precompiled. This means that when the
PreparedStatement
is executed, the DBMS can just run the
PreparedStatement
SQL statement without having to compile it first.
Although
PreparedStatement
objects can be used for SQL statements with no parameters, you probably use them most often for SQL statements that take parameters. The advantage of using SQL statements that take parameters is that you can use the same statement and supply it with different values each time you execute it.
Refer : http://www.jdbc-tutorial.com/jdbc-prepared-statements.htmCallableStatement – used for executing stored procedures on the database.
Update statements such as INSERT, UPDATE and DELETE return an update count that indicates how many rows were affected in the database. These statements do not return any other information.
Query statements return a JDBC row result set. The row result set is used to walk over the result set. Individual columns in a row are retrieved either by name or by column number. There may be any number of rows in the result set. The row result set has metadata that describes the names of the columns and their types.
Choosing an approach for JDBC database access :You can choose among several approaches to form the basis for your JDBC database access. In addition to three flavors of the JdbcTemplate, a new SimpleJdbcInsert and SimplejdbcCall approach optimizes database metadata, and the RDBMS Object style takes a more object-oriented approach similar to that of JDO Query design.
JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.
NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.
SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.
SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.
RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.
ResultSet : A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
Statement stmt = connection.createStatement();
String selectquery = "select * from user";
ResultSet rs = stmt.executeQuery(selectquery);
while(rs.next()){
System.out.print("User ID :" + rs.getInt(1)+ " ");
System.out.println("User Name :" + rs.getString(2));
An interface used by JdbcTemplate for processing rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of processing each row but don't need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.