0% found this document useful (0 votes)
65 views8 pages

JDBC

The JDBC API allows Java programs to connect to and interact with relational databases. It defines interfaces for connecting to a database like DriverManager and Connection, creating statements like Statement and PreparedStatement, executing queries, processing result sets, and closing connections. A simple example shows how to use the DriverManager to get a connection, create a statement to execute a query, process the result set, and close the connection. JDBC includes drivers, driver manager, and interfaces for connecting to databases and executing SQL statements from Java programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

JDBC

The JDBC API allows Java programs to connect to and interact with relational databases. It defines interfaces for connecting to a database like DriverManager and Connection, creating statements like Statement and PreparedStatement, executing queries, processing result sets, and closing connections. A simple example shows how to use the DriverManager to get a connection, create a statement to execute a query, process the result set, and close the connection. JDBC includes drivers, driver manager, and interfaces for connecting to databases and executing SQL statements from Java programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JDBC Introduction

The JDBC API is a Java API that can access any kind of
tabular data, especially data stored in a Relational Database.
JDBC helps you to write Java applications that manage these
programming activities:
Import Package: import java.sql;
Load and Register the driver
Establish the connection
Create the statement
Execute the quesry
Process result
Close the connection
The following simple code fragment gives a simple example
of these three steps:
public void connectToAndQueryDatabase(String username,
String password) {

Connection con = DriverManager.getConnection(

"jdbc:myDriver:myDatabase,username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c
FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
}
This short code fragment instantiates a DriverManager
object to connect to a database driver and log into the
database, instantiates a Statement object that carries your
SQL language query to the database; instantiates a ResultSet
object that retrieves the results of your query, and executes a
simple while loop, which retrieves and displays those
results. It's that simple.

JDBC Product Components


JDBC includes four components:
The JDBC API(Application programming interface)
JDBC Driver Manager
JDBC Test Suite
JDBC-ODBC Bridge
This Trail uses the first two of these four JDBC components
to connect to a database and then build a java program that
uses SQL commands to communicate with a test Relational
Database. The last two components are used in specialized
environments to test web applications, or to communicate
with ODBC-aware DBMS.

JDBC Architecture
The JDBC API supports both two-tier and three-tier
processing models for database access.

Two tier model


Some Interface:
Driver interface
Connection interface
Statement interface
PreparedStatement interface
CallableStatement interface
ResultSet interface
ResultSetMetaData interface
DatabaseMetaData interface
RowSet interface

Some Classes:
DriverManager class

JDBC Driver: There are 4 types of driver


JDBC-ODBC bridge driver

Native-API driver : The Oracle Call Interface (OCI)


driver is an a Type 2 driver.
Network Protocol driver

Thin driver : MySQL's Connector/J driver is a Type 4


driver.
Connectivity with Mysql: Need to do following thing to connect
database:

Driver class: com.mysql.jdbc.Driver.

Connection URL: jdbc:mysql://localhost:2204/database_name(employee).

Username: The default username ->root.

Password: Given by the user at the time of installing the mysql database.

Note:- first create database:

create database employee;

use employee;

create table emp(id int(5),name varchar(40),age int(2));

Example:

import java.sql.*;

class mysqlConnection{

public static void main(String args[]){


try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(

"jdbc:mysql://localhost:2204/employee","root","root");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

You might also like