0% found this document useful (0 votes)
27 views14 pages

JDBC

JDBC is a Java API that allows Java programs to connect to databases. It uses JDBC drivers to establish this connection. There are generally 5 steps to using JDBC - 1) register the driver, 2) create a connection, 3) create SQL statements, 4) execute queries/updates, and 5) close the connection. JDBC allows performing basic CRUD (create, read, update, delete) operations on databases.

Uploaded by

Nepal Mens Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

JDBC

JDBC is a Java API that allows Java programs to connect to databases. It uses JDBC drivers to establish this connection. There are generally 5 steps to using JDBC - 1) register the driver, 2) create a connection, 3) create SQL statements, 4) execute queries/updates, and 5) close the connection. JDBC allows performing basic CRUD (create, read, update, delete) operations on databases.

Uploaded by

Nepal Mens Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

JDBC

Introduction
 JDBC stands for Java Database Connectivity.
 JDBC is a Java API to connect and execute the query with the
database. It is a part of JavaSE (Java Standard Edition).
 JDBC API uses JDBC drivers to connect with the database. There
are four types of JDBC drivers:
Why Should We Use JDBC
1. Connect to the database
2. Execute queries and update statements to the
database
3. Retrieve the result received from the database.
Java Database Connectivity with 5 Steps

1) Register the driver class


 The forName() method of Class class is used to register the driver class.
This method is used to dynamically load the driver class
 Syntax:
 public static void forName(String className)throws ClassNotFoundExcepti
on  
2) Create the connection object
 The getConnection() method of DriverManager class is used to establish connection with the
database.
1. public static Connection getConnection(String url)throws SQLException  
2. public static Connection getConnection(String url,String name,String password)  throws SQLExceptio
n  

Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","
password");  
3) Create the Statement object
The createStatement() method of Connection interface is used to create statement.
The object of statement is responsible to execute queries with the database.
 public Statement createStatement()throws SQLException  
 Syntax:
 Statement stmt=con.createStatement();  
4) Execute the query
 The executeQuery() method of Statement interface is used to execute queries to
the database. This method returns the object of ResultSet that can be used to get
all the records of a table.

 Syntax:
 ResultSet rs=stmt.executeQuery("select * from emp");    
while(rs.next()){  
System.out.println(rs.getInt(1)+" "+rs.getString(2));  
}  
5) Close the connection object
 By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close the
connection.
 Syntax:
public void close()throws SQLException  
 con.close();  
CREATE DATABASE
 Syntax;

 CREATE DATABASE DATABASE_NAME


CREATE TABLE
 Syntax;

 CREATE TABLE TABLE_NAME


SELECT RECORD
 Syntax;

 SELECT * FROM TABLE_NAME


DELETE RECORD
 Syntax;

 DELETE FROM TABLE_NAME WHERE col_name = value


UPDATE RECORD
 Syntax;

 UPDATE TABLE_NAME SET col_name = value WHERE col_name =


value

You might also like