SlideShare a Scribd company logo
3
Most read
4
Most read
6
Most read
Java Database Connectivity
with 5 Steps
By,
R. Punithavel, AP/MCA
Java Database Connectivity with 5 Steps.pptx
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.
Ex:
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
• The getConnection() method of DriverManager class is used to
establish connection with the database.
Ex:
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.
EX:
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.
EX:
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.
con.close();
• OWL 2 ontology can be used
to describe Database
Management Systems (DBMS)
in a machine-processable way.
• This description can help with
software engineering
processes and database
administration tasks, and can
help decide which DBMS is
best for a given set of
requirements.
EXAMPLE
import java.sql.*;
class OracleCon
{
public static void main(String args[])
{
Try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
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);
}
}
}
My SQL Connection Example
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
sonoo","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);}
}
}
Java Database Connectivity with 5 Steps.pptx

More Related Content

PPSX
JDBC Part - 2
PPTX
Session 24 - JDBC, Intro to Enterprise Java
PPTX
Jdbc ppt
PDF
PDF
22jdbc
PPTX
jdbc concepts in java jdbc programming- programming
PPTX
JDBC2 JDBCJDBCJDBCJDBCJDBCJDBCJDBCJDBCJDBC
JDBC Part - 2
Session 24 - JDBC, Intro to Enterprise Java
Jdbc ppt
22jdbc
jdbc concepts in java jdbc programming- programming
JDBC2 JDBCJDBCJDBCJDBCJDBCJDBCJDBCJDBCJDBC

Similar to Java Database Connectivity with 5 Steps.pptx (20)

PPT
Jdbc
PPTX
Database connect
PPT
JDBC Connecticity.ppt
PDF
Core Java Programming Language (JSE) : Chapter XIII - JDBC
PDF
Introduction to JDBC and database access in web applications
PDF
Lecture17
PPTX
jdbc programs are useful jdbc programs are useful
PPTX
PDF
Jdbc[1]
PDF
JDBC programming
PPT
jdbc_presentation.ppt
PPTX
Database Programming Techniques
PDF
Jdbc tutorial
PPTX
Jdbc Java Programming
PPTX
Lecture 1. java database connectivity
PDF
Advance Java Practical file
PPT
4. Database Connectivity using JDBC .ppt
PPTX
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
PPTX
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
PDF
JDBC in Servlets
Jdbc
Database connect
JDBC Connecticity.ppt
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Introduction to JDBC and database access in web applications
Lecture17
jdbc programs are useful jdbc programs are useful
Jdbc[1]
JDBC programming
jdbc_presentation.ppt
Database Programming Techniques
Jdbc tutorial
Jdbc Java Programming
Lecture 1. java database connectivity
Advance Java Practical file
4. Database Connectivity using JDBC .ppt
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
JDBC in Servlets
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Orientation - ARALprogram of Deped to the Parents.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
Chinmaya Tiranga quiz Grand Finale.pdf
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
01-Introduction-to-Information-Management.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Ad

Java Database Connectivity with 5 Steps.pptx

  • 1. Java Database Connectivity with 5 Steps By, R. Punithavel, AP/MCA
  • 3. 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. Ex: Class.forName("oracle.jdbc.driver.OracleDriver");
  • 4. 2) Create the connection object • The getConnection() method of DriverManager class is used to establish connection with the database. Ex: Connection con=DriverManager.getConnection( "jdbc:oracle:thin: @localhost:1521:xe","system","password");
  • 5. 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. EX: Statement stmt=con.createStatement();
  • 6. 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. EX: ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) { System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
  • 7. 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. con.close();
  • 8. • OWL 2 ontology can be used to describe Database Management Systems (DBMS) in a machine-processable way. • This description can help with software engineering processes and database administration tasks, and can help decide which DBMS is best for a given set of requirements.
  • 9. EXAMPLE import java.sql.*; class OracleCon { public static void main(String args[]) { Try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 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); } } }
  • 10. My SQL Connection Example
  • 11. import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ sonoo","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);} } }