0% found this document useful (0 votes)
10 views

Connect To Database in Java

working or conection of data with dbms

Uploaded by

singhamrao1236
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Connect To Database in Java

working or conection of data with dbms

Uploaded by

singhamrao1236
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

Home Java Programs OOPs String Exception Multithreading

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 1/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

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:

Register the Driver class


Create connection
Create statement
Execute queries
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.

Syntax of forName() method

public static void forName(String className)throws ClassNotFoundException

Note: Since JDBC 4.0, explicitly registering the driver is optional. We just need to put vender's Jar

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 2/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

in the classpath, and then JDBC driver manager can detect and load the driver automatically.

Example to register the OracleDriver class

Here, Java program is loading oracle driver to esteblish database connection.

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.

Syntax of getConnection() method

1) public static Connection getConnection(String url)throws SQLException


2) public static Connection getConnection(String url,String name,String password)
throws SQLException

Example to establish connection with the Oracle database

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.

Syntax of createStatement() method

public Statement createStatement()throws SQLException

Example to create the statement object

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 3/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

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.

Syntax of executeQuery() method

public ResultSet executeQuery(String sql)throws SQLException

Example to execute query

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.

Syntax of close() method

public void close()throws SQLException

Example to close connection

con.close();

Note: Since Java 7, JDBC has ability to use try-with-resources statement to automatically close
resources of type Connection, ResultSet, and Statement.

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 4/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

It avoids explicit connection closing step.

← Prev Next →

AD

For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials

Splunk SPSS Swagger Transact-SQL

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 5/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

Tumblr ReactJS Regex Reinforcement


Learning

R Programming RxJS React Native Python Design


Patterns

Python Pillow Python Turtle Keras

Preparation

Aptitude Reasoning Verbal Ability Interview Questions

Company Questions

Trending Technologies

Artificial AWS Selenium Cloud Computing


Intelligence

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 6/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

Hadoop ReactJS Data Science Angular 7

Blockchain Git Machine Learning DevOps

B.Tech / MCA

DBMS Data Structures DAA Operating System

Computer Network Compiler Design Computer Discrete


Organization Mathematics

Software html tutorial


Engineering
Web Technology
Software
Ethical Hacking Computer Graphics Engineering

Cyber Security Automata C Language C++ tutorial


tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming

Java tutorial .Net Python tutorial List of


Framework Programs
Java Python
tutorial
Programs
.Net

Control Data Mining Data


Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 7/8
6/2/24, 4:07 PM How to connect to database in Java | Java Database Connectivity - javatpoint

AD

https://www.javatpoint.com/steps-to-connect-to-the-database-in-java 8/8

You might also like