The following code can be used to connect to Oracle database using java.

 

/**
 * ConnectOracle.java
 */
package com.javagenious;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javagenious.com
 * 
 */
public class OracleConnectionTutorial {

	public static void main(String[] args) {

		try {
			//Driver manager classes must be imported to avoid compilation issues (java.sql.DriverManager)
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			System.out.println("The connection to the database is being initialized....");
			Connection connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
			Statement statement = connection.createStatement();
			ResultSet resultset = statement.executeQuery("select 'Connected' from dual");
			resultset.next();
			String s = resultset.getString(1);
			System.out.println(s);
			statement.close();
			connection.close();
		} catch (Exception e) {
			System.out.println("The exception raised is:" + e);
		}
	}

}

Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)