0% found this document useful (0 votes)
12 views6 pages

Java Database Connectivity Guide

Java Database Connectivity (JDBC) is an API that allows Java applications to interact with various databases. The process of using JDBC involves five steps: registering the driver class, creating a connection object, creating a statement object, executing queries, and closing the connection. The document also provides an example program demonstrating how to connect to a MySQL database and retrieve data using JDBC.

Uploaded by

lg netpoint
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)
12 views6 pages

Java Database Connectivity Guide

Java Database Connectivity (JDBC) is an API that allows Java applications to interact with various databases. The process of using JDBC involves five steps: registering the driver class, creating a connection object, creating a statement object, executing queries, and closing the connection. The document also provides an example program demonstrating how to connect to a MySQL database and retrieve data using JDBC.

Uploaded by

lg netpoint
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

Java Database Connectivity:

Introduction
1. The term JDBC stands for Java Database Connectivity and it is a
specification from Sun microsystems.
2. JDBC is an API (Application programming interface) in Java that helps
users to interact or communicate with various databases.
3. The classes and interfaces of JDBC API allow the application to send
the request to the specified database.
4. Using JDBC, we can write programs required to access databases.
JDBC and the database driver are capable of accessing databases and
spreadsheets.
5. The [Link] package contains classes and interfaces for JDBC
API. A list of popular interfaces of JDBC API are given below:
o Driver interface
o Connection interface
o Statement interface
o PreparedStatement interface
o CallableStatement interface
o ResultSet interface
o ResultSetMetaData interface
o DatabaseMetaData interface
o RowSet interface
6. 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
1. 5 Steps to connect to the database in java
1. Register the driver class
2. Create the connection object
3. Create the Statement object
4. Execute the query
5. Close the connection object

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.

· For MySQL, the driver class is "[Link]".

· For Oracle, it’s "[Link]"

Syntax:----[Link]("[Link]");
2. Create the connection object

The getConnection() method of DriverManager class is used to establish


connection with the database.

Syntax:-------Connection con=[Link](

"jdbc:oracle:thin:@localhost:1521:xe","system","passw
ord");

jdbc:mysql://localhost:3306/sonoo

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.

Syntax:---- Statement stmt=[Link]();

 The Statement interface provides methods to execute queries with


the database.
 ResultSet executeQuery(String sql): is used to execute SELECT
query. It returns the object of ResultSet.
 int executeUpdate(String sql): is used to execute specified query, it
may be create, drop, insert, update, delete etc.

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=[Link]("select * from emp");


2.
3. while([Link]()){
4. [Link]([Link](1)+" "+[Link](2));
5. }

Result Set
The object of ResultSet maintains a cursor pointing to a row of a table.
Initially, cursor points to before the first row.

You can use methods of ResultSet to move through the rows and
retrieve column values.

1) public boolean next(): is used to move the cursor to the one row next from
the current position.

2) public boolean previous(): is used to move the cursor to the one row previous
from the current position.

3) public boolean first(): is used to move the cursor to the first row in result
set object.

4) public boolean last(): is used to move the cursor to the last row in result set
object.

5) public boolean absolute(int is used to move the cursor to the specified row
row): number in the ResultSet object.

7) public int getInt(int is used to return the data of specified column index of
columnIndex): the current row as int.

8) public int getInt(String is used to return the data of specified column name
columnName): of the current row as int.

9) public String getString(int is used to return the data of specified column index of
columnIndex): the current row as String.

10) public String is used to return the data of specified column name
getString(String columnName): of the current row as String.

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.

[Link]();
Example program:-

import [Link].*;

public class ResultSetExample {


public static void main(String[] args) {
try {
// 1. Load driver and connect
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/schooldb", "root", "password");

// 2. Create statement
Statement stmt = [Link]();

// 3. Execute SELECT query


ResultSet rs = [Link]("SELECT id, name FROM
students");

// 4. Process ResultSet
while ([Link]()) { // moves cursor to next row
int id = [Link]("id"); // get by column name
String name = [Link](2); // get by column index (1-based)
[Link](id + " " + name);
}

// 5. Close resources
[Link]();
[Link]();
[Link]();

} catch (Exception e) {
[Link](e);
}
}
}

You might also like