0% found this document useful (0 votes)
9 views4 pages

Day 33 JDBC

jdbc subject

Uploaded by

mdzayed2003786
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)
9 views4 pages

Day 33 JDBC

jdbc subject

Uploaded by

mdzayed2003786
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/ 4

Day-33 Java-Database Connectivity (JDBC)

⮚ JDBC is API that helps a programmer to write a java program to connect to any Database,
retrieve the data from the Database
⮚ Java.sql or java.sqlx is a package

Oracle Database

⮚ Database: Collection of Data which logically Related are stored.

⮚ RDBMS: this will enable us to create a database and maintain the database it will provide us
an interface between the Programmer and Database
⮚ User is communicating with the Database DBMS in which Language: SQL

How to write a query:

1. Command Prompt: Sqlplus-editor


2. Credentials Username:
Password:
3. Sql:> Select * from list;
4. Result
Java-Database
Step 1: import the Packages necessary for JDBC
Import java.sql.*;
Step2:Register the JDBC Driver
Step3: open the Connection / Establish the connections
Step4:Write Query-🡪 Execute the Query
Step5: Results: Extract the Results from database
Step6:Close the connection/Clean up the Enviornment

Step 1: import the Packages necessary for JDBC


Import java.sql.*;
Step2: Registration of Driver
▪ Registration of driver in which the oracle driver class file loaded into the
memory so that it can utilise as an implementation of JDBC interface.
▪ One time process in the program.

▪ Two ways to Register.


1. Class.forName();
2. DriverManager.registerDriver()

Way1:
Class.forName(“oracle.jdbc.driver.oracleDriver”);
This will allows registration,Configurable & portable
Way2:
DriverManager.registerDriver(“oracle.jdbc.driver.oracleDriver”);

Step3: open a Connection/Establish

Invoke DriverManager.getConnection() Method to connect to the database

getConnection has 3 variants:

1. getConnection(String Url)
2. getConnection(String URL,properties prop);
3. getConnection(String URL,String Username,String Password)

URL: jdbc:oracle:thin:@hostname:port_Number:databaseName

Hostname: IP address ex: jdbc:oracle:thin:@192.168.1.2:


localhost Ex: jdbc:oracle:thin:@localhost:
port_number:1521
Databasename:orcl
localhost
url: jdbc:oracle:thin:@localhost:1521:orcl
username: system
password: bitm123(at the time of installation)

Step 4: Writing the Query and executing


a. Writing the query
1. Statement
2. Prepared statement
3. callable Statement

Statement Prepared Statement Callable Statement

Statement stmt; PreparedStatement pstmt; Used to access the database


// Writing the query // Writing the query stored procedure.
Stmt=conn.createStatement(); String query=”Select ?,? from list”;
String query=”Select name,rate pstmt=conn.prepareStatement(query);
from list”;
pstmt.setString(1,name);
pstmt.setString(2,rate);

Executing query:
1. Boolean execute (String query);
2. Int executeUpdate(String query);
3. Resultset Executequery(String query);-table

Language Query Execute Query Result Execution


Result-return datatype
statement
DDL CREATE Table created Status=
Execute(query);
ALTER 1 column altered Number Int value=
ExecuteUpdate(query);
DROP 1 column drop Number Int value=
ExecuteUpdate(query);
RENAME Alter the tablename

DML SELECT Table Rows and Resultset


columns Executequery(String
query);-table
INSERT 1 row is inserted number
UPDATE 2 rows is updated number
DELETE
TC COMMIT
ROLLBACK

Sequence

You might also like