Scroll to top

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(System.in);
		System.out
				.println("Please provide below details to connect Oracle Database");
		System.out.println("Enter Database");
		String dbName = scanner.next();
		System.out.println("Enter UserName");
		String userName = scanner.next();
		System.out.println("Enter Password");
		String password = scanner.next();

		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		Connection connection = null;

		try {

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

		} catch (SQLException e) {

			e.printStackTrace();
		}

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

}

 

JARs Required :

ojdbc14.jar

Tools Used :

Eclipse Indigo

Java Version

JDK 1.6

Author avatar
JBT
https://javabeginnerstutorial.com/

1 comment

  1. Namrata

    Thank you this was help full for me……

Post a comment

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.