Professional Documents
Culture Documents
Database
Introduction To JDBC ODBC
Java Database Connectivity(JDBC) is an Application Programming
Interface(API) used to connect Java application with Database.
JDBC is used to interact with various type of Database such as Oracle, MS Access,
My SQL and SQL Server. It allows java program to execute SQL statement and
retrieve result from database.
The JDBC API consists of classes and methods that are used to perform various
operations like: connect, read, write and store data in the database.
The JDBC API uses a driver manager and database-specific drivers to provide
transparent connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used to access each data
source. The driver manager is capable of supporting multiple concurrent drivers
connected to multiple heterogeneous databases.
The JDBC architecture consists of two-tier and three-tier processing models to access
a database.
Two-tier model
A java application communicates directly to the data source. The JDBC driver
enables the communication between the application and the data source. When a user
sends a query to the data source, the answers for those queries are sent back to the
user in the form of results.
The Java application/applet and the database can be on the same machine, or the
database can be on a server and the Java application/applet can be on a client machine
using any network protocol.
Three-tier model
In this, the user’s queries are sent to middle-tier services, from which the commands
are again sent to the data source. The results are sent back to the middle tier, and from
there to the user.
In a three-tier model, a Java application/applet communicates with a middle tier
component that functions as an application server. The application server talks to a
given database using JDBC.
There are 5 steps to connect any java application with the database using JDBC.
These steps are as follows:
Register the Driver class
Create connection
Create statement
Execute queries
Close connection
Example:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
2) Create the connection object
import java.sql.*;
class Myconnectivity
{
public static void main(String args[])
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
//here emp is database name, root is username and blank password
Statement stmt=con.createStatement();
con.close();
}
catch(Exception e)
{ System.out.println(e);}
}
}
Output:
PreparedStatement interface
As you can see, we are passing parameter (?) for the values. Its value will be set by
calling the setter methods of PreparedStatement.
Method Description
public void setInt(int paramIndex,
sets the integer value to the given parameter index.
int value)
public void setString(int
sets the String value to the given parameter index.
paramIndex, String value)
public void setFloat(int
sets the float value to the given parameter index.
paramIndex, float value)
public void setDouble(int
sets the double value to the given parameter index.
paramIndex, double value)
executes the query. It is used for create, drop,
public int executeUpdate()
insert, update, delete etc.
executes the select query. It returns an instance of
public ResultSet executeQuery()
ResultSet.
ResultSet MetaData
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name,
column type etc. ,
Method Description
public int getColumnCount() throws It returns the total number of columns in the
SQLException ResultSet object.
public String getColumnName(int it returns the column name of the specified
index) throws SQLException column index.
public String getColumnTypeName it returns the column type name for the
(int index) throws SQLException specified index.
public String getTableName(int index) it returns the table name for the specified
throws SQLException column index.