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

JDBC Steps

jdbc

Uploaded by

rejitha
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)
16 views4 pages

JDBC Steps

jdbc

Uploaded by

rejitha
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 is an acronym for Java Database Connectivity.

It’s an advancement for ODBC


( Open Database Connectivity ). JDBC is a standard API specification developed in order
to move data from the front end to the back end. This API consists of classes and
interfaces written in Java. It basically acts as an interface (not the one we use in Java) or
channel between your Java program and databases i.e it establishes a link between the
two so that a programmer can send data from Java code and store it in the database for
future use.

Steps to Connect Java Application with Database

Step 1 – Import the Packages


Step 2 – Load the drivers using the forName() method
Step 3 – Register the drivers using DriverManager
Step 4 – Establish a connection using the Connection class object
Step 5 – Create a statement
Step 6 – Execute the query
Step 7 – Close the connections

Java Database Connectivity

Step 1: Import the Packages

Step 2: Loading the drivers


In order to begin with, you first need to load the driver or register it before using it in the
program. Registration is to be done once in your program. You can register a driver in
one of two ways mentioned below as follows:
Class.forName()
Here we load the driver’s class file into memory at the runtime. No need of using new or
create objects. The following example uses Class.forName() to load the Oracle driver as
shown below as follows:
Class.forName(“oracle.jdbc.driver.OracleDriver”);
DriverManager.registerDriver()
DriverManager is a Java inbuilt class with a static member register. Here we call the
constructor of the driver class at compile time. The following example uses
DriverManager.registerDriver()to register the Oracle driver as shown below:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())

Step 3: Establish a connection using the Connection class object

After loading the driver, establish connections as shown below as follows:


Connection con = DriverManager.getConnection(url,user,password)

● user: Username from which your SQL command prompt can be accessed.

● password: password from which the SQL command prompt can be accessed.

● con: It is a reference to the Connection interface.

● Url: Uniform Resource Locator which is created as shown below:

String url = “ jdbc:mysql://localhost:3306/student”

Where mysql is used, localhost is the IP Address where a database is stored, 3306 is the
port number and student is the database name. All 3 parameters above are of String type
and are to be declared by the programmer before calling the function. Use of this can be
referred to form the final code.

Step 4: Create a statement


Once a connection is established you can interact with the database. The JDBCStatement,
CallableStatement, and PreparedStatement interfaces define the methods that enable you
to send SQL commands and receive data from your database.
Use of JDBC Statement is as follows:
Statement st = con.createStatement();

Step 5: Execute the query

Now comes the most important part i.e executing the query. The query here is an SQL
Query. Now we know we can have multiple types of queries. Some of them are as
follows:
● The query for updating/inserting a table in a database.

● The query for retrieving data.

The executeQuery() method of the Statement interface is used to execute queries of


retrieving values from the database. This method returns the object of ResultSet that can
be used to get all the records of a table.
The executeUpdate(sql query) method of the Statement interface is used to execute
queries of updating/inserting.

Step 6: Closing the connections

So finally we have sent the data to the specified location and now we are on the verge of
completing our task. By closing the connection, objects of Statement and ResultSet will
be closed automatically. The close() method of the Connection interface is used to close
the connection. It is shown below as follows:
con.close();

import java.sql.*;
class FirstApp
{
public static void main(String args[])
{
try
{
String dbUrl="jdbc:mysql://localhost:3306/student";
String username="root";
String password="stm123";
Connection myConnection=DriverManager.getConnection(dbUrl,
username, password);
Statement myStatement= myConnection.createStatement();
ResultSet myResultSet= myStatement.executeQuery("select * from
students");
while (myResultSet.next())
{
System.out.println("Student full name:
"+myResultSet.getString("first_name")+" "+myResultSet.getString("second_name"));
}
}catch(Exception e){
System.out.println(e.getMessage());
}

}
}

You might also like