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

JDBC Programming

This document discusses JDBC programming and connecting to databases in Java. It covers: - What JDBC is and the different types of JDBC drivers - The steps to connect to a database using JDBC, including importing packages, registering the driver, formulating the database URL, and creating a connection object - The basic steps to access and retrieve information from a database via JDBC - Methods like executeQuery(), executeUpdate(), and execute() to execute SQL statements on the database connection

Uploaded by

Prince Jutt
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)
48 views4 pages

JDBC Programming

This document discusses JDBC programming and connecting to databases in Java. It covers: - What JDBC is and the different types of JDBC drivers - The steps to connect to a database using JDBC, including importing packages, registering the driver, formulating the database URL, and creating a connection object - The basic steps to access and retrieve information from a database via JDBC - Methods like executeQuery(), executeUpdate(), and execute() to execute SQL statements on the database connection

Uploaded by

Prince Jutt
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

www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.

com/Gctpak

CIT-303/Web development with Java By:Instructor IT Miss.Saba

Chp.08 JDBC Programming


8.1.Introduction to 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. It is like Open Database Connectivity (ODBC) provided by Microsoft.

The current version of JDBC is 4.3. It is the stable release since 21st
September, 2017. Sun Microsystems released JDBC as part of Java
Development Kit (JDK)

What is 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.
An API can be created for applications, libraries, operating systems, etc.
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
Installing the JDBC Driver for MySQL
www.SalmanAdeeb.wixsite.com/DAE-CIT-books Databases www.facebook.com/Gctpak

To install the JDBC driver for MySQL databases, complete the following steps.
1. Download the latest version of Connector/J for your platform
from https://dev.mysql.com/downloads/connector/j/ and install it.
2. Locate the mysql-connector-java-<version>-bin.jar file among the files
that were installed.
For example, on Windows: C:\Program Files (x86)\MySQL\MySQL
Connector J\mysql-connector-java-5.1.30-bin.jar.
3. Rename the file to mysql-connector.jar.
4. Copy the file to <ECloud install>/<arch>/lib/.
o On Linux, the default location is /opt/ecloud/i686_Linux/lib/.
o On Windows, the default location is C:\ECloud\i686_win32\lib\.

5. Restart the Cluster Manager service.

8.2.Connecting to Database in JDBC Programming:


After you've installed the appropriate driver, it is time to establish a database
connection using JDBC.
The programming involved to establish a JDBC connection is fairly simple. Here
are these simple four steps −
 Import JDBC Packages: Add import statements to your Java program to
import required classes in your Java code.
 Register JDBC Driver: This step causes the JVM to load the desired driver
implementation into memory so it can fulfill your JDBC requests.
 Database URL Formulation: This is to create a properly formatted address
that points to the database to which you wish to connect.
 Create Connection Object: Finally, code a call to the DriverManagerobject's
getConnection( ) method to establish actual database connection.
Connecting to Database Example:
In this example, sonoo is the database name, root is the username and
password both.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
Class.forName("com.mysql.jdbc.Driver");
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
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);}
}
}

8.3.Accessing a Database:
The process of retrieving information from a database via JDBC involves these
five basic steps:
1. Register the JDBC driver with the driver manager.
2. Establish a database connection.
3. Execute an SQL statement.
4. Process the results.
5. Close the database connection

8.4.The execute Query Method & execute Update Method:


executeQuery(), executeUpdate() and execute() are the methods
of java.sql.Statement interface of JDBC API which are used to execute the SQL
statements.

www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak

www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab

You might also like