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

JDBC

Uploaded by

Aryan yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

JDBC

Uploaded by

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

JDBC

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:

o JDBC-ODBC Bridge Driver,


o Native Driver,
o Network Protocol Driver, and
o Thin Driver

We can use JDBC API to access tabular data stored in any relational database. By the help of
JDBC API, we can save, update, delete and fetch data from the database.
The java.sql package contains classes and interfaces for JDBC API. API (Application
programming interface) is a document that contains a description of all the features of a
product or software. It represents classes and interfaces that software programs can follow to
communicate with each other.

Why Should We Use JDBC


Before JDBC, ODBC API was the database API to connect and execute the query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses
JDBC drivers (written in Java language).

We can use JDBC API to handle database using Java program and can perform the following
activities:

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
There are 5 steps to connect any java application with the database using JDBC. These steps
are as follows:

o Register the Driver class

o Create connection

o Create statement

o Execute queries

o Close connection

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.

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

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.

public static Connection getConnection(String url,String name,String password


) throws SQLException

Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdata","root","");
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.

1. 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.

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


2.
3. while(rs.next()){
System.out.println(r.getString("name")+" "+r.getString("class"));
4. }

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();

*******************************************************************************
FirstJDBC.java

You might also like