How to connect to oracle database in java using eclipse

Here we will learn to connect to MYSQL DB via Java.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;

 * Here we will learn to connect to Oracle DB using JDBC Driver.
public class ConnectOracleDB {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(;
				.println("Please provide below details to connect Oracle Database");
		System.out.println("Enter Database");
		String dbName =;
		System.out.println("Enter UserName");
		String userName =;
		System.out.println("Enter Password");
		String password =;

		try {


		} catch (ClassNotFoundException e) {

		Connection connection = null;

		try {

			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:" + dbName, userName,

		} catch (SQLException e) {


		if (connection != null) {
			System.out.println("nSuccessfullly connected to Oracle DB");
		} else {
			System.out.println("nFailed to connect to Oracle DB");



JARs Required :


Tools Used :

Eclipse Indigo

Java Version

JDK 1.6

One thought

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search