JDBC 4.0 API in java

JDBC 4.0 API in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

JDBC 4.0 API

JDBC 4.0 API is mainly divided into two package
  1. java.sql
  2. javax.sql
 JDBC 4.0 API in java

java.sql package

This package includes classes and interfaces to perform almost all JDBC operation such as creating and executing SQL Queries.

Important classes and interface of java.sql package

classes/interfaceDescription
java.sql.BLOBProvide support for BLOB(Binary Large Object) SQL type.
java.sql.Connectioncreates a connection with specific database
java.sql.CallableStatementExecute stored procedures
java.sql.CLOBProvide support for CLOB(Character Large Object) SQL type.
java.sql.DateProvide support for Date SQL type.
java.sql.Drivercreate an instance of a driver with the DriverManager.
java.sql.DriverManagerThis class manages database drivers.
java.sql.PreparedStatementUsed to create and execute a parameterized query.
java.sql.ResultSetIt is an interface that provides methods to access the result row-by-row.
java.sql.SavepointSpecify savepoint in a transaction.
java.sql.SQLExceptionEncapsulate all JDBC related exception.
java.sql.StatementThis interface is used to execute SQL statements.

javax.sql package

This package is also known as JDBC extension API. It provides classes and interfaces to access server-side data.

Important classes and interface of javax.sql package

classes/interfaceDescription
javax.sql.ConnectionEventProvide information about the occurrence of an event.
javax.sql.ConnectionEventListenerUsed to register event generated by the PooledConnection object.
javax.sql.DataSourceRepresent the DataSource interface used in an application.
javax.sql.PooledConnectionprovide an object to manage connection pools.

Steps to connect a Java Application to Database

The following 5 steps are the basic steps involve in connecting a Java application with Database using JDBC.
  1. Register the Driver
  2. Create a Connection
  3. Create SQL Statement
  4. Execute SQL Statement
  5. Closing the connection
Steps to connect a Java Application to Database


Register the Driver

Class.forName() is used to load the driver class explicitly.
Example to register with JDBC-ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Create a Connection

getConnection() method of DriverManager class is used to create a connection.
Syntax
getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)
Example establish a connection with Oracle Driver
Connection con = DriverManager.getConnection
                    ("jdbc:oracle:thin:@localhost:1521:XE","username","password");

Create SQL Statement


createStatement() the method is invoked on the current Connection object to create a SQL Statement.
Syntax
public Statement createStatement() throws SQLException
Example to create a SQL statement
Statement s=con.createStatement();

Execute SQL Statement

executeQuery() method of Statement interface is used to execute SQL statements.
Syntax
public ResultSet executeQuery(String query) throws SQLException
Example to execute a SQL statement
ResultSet rs=s.executeQuery("select * from user");
  while(rs.next())
  {
   System.out.println(rs.getString(1)+" "+rs.getString(2));
  }

Closing the connection

After executing the SQL statement you need to close the connection and release the session. The methodclose() of Connection interface is used to close the connection.
Syntax
public void close() throws SQLException
Example of closing a connection
con.close();





















































Comments