Java Program to connect Oracle Database with Example - JDBC Tutorial Sample Code (original) (raw)
How to connect to Oracle database from Java Program using JDBC API is common to need for many Java programmer, though there is a lot of framework available which has simplified JDBC development e.g Hibernate, Spring JdbcTempate and many more, creating Java program to connect to oracle database from plain old Java is still the easiest and quickest method for testing and debugging database connectivity. The database connection program is also a common Java programming exercise in many Java programming courses in schools, colleges, and various training institutes.
We have been exploring some advanced concepts and best practices on JDBC in my previous articles like Why should you use PreparedStatement in Java and 4 JDBC performance tips for Java application, which you may like if you are on a more advanced level. This simple java program is intended for beginners in Java who have just started learning JDBC API.
How to connect Oracle Database from Java Program using JDBC - code example
Here is a complete code example of a Java program to connect the Oracle database using JDBC. This Java program will connect to the Oracle database and print the current date from the "dual" system table. By the way before running this Java program to connect the Oracle database make sure your Oracle database server is running and you have JDBC thin driver in your classpaths like ojdbc6.jar or ojdbc6_g.jar.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* Simple Java Program to connect Oracle database by using Oracle JDBC thin driver
* Make sure you have Oracle JDBC thin driver in your classpath before running this program
* @author
*/
public class OracleJdbcExample {
public static void main(String args[]) throws SQLException {
//URL of Oracle database server
String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
//properties for creating connection to Oracle database
Properties props = new Properties();
props.setProperty("user", "scott");
props.setProperty("password", "tiger");
//creating connection to Oracle database using JDBC
Connection conn = DriverManager.getConnection(url,props);
String sql ="select sysdate as current_day from dual";
//creating PreparedStatement object to execute query
PreparedStatement preStatement = conn.prepareStatement(sql);
ResultSet result = preStatement.executeQuery();
while(result.next()){
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
System.out.println("done");
}
}
Output:
Current Date from Oracle : 2012-04-12 17:13:49
done
Error and Exception while connecting Oracle Database from Java Program:
- Invalid Username and Password
Exception in thread "main" java.sql.SQLException: ORA-01017: invalid username/password; logon denied
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:452)
This Error comes when username and password provided to Java program connecting to Oracle database is not correct.
2)No suitable driver found
jdbc:oracle:thin:@localhost:1632:DEVROOT32
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
This Error comes when JDBC thin driver for relevant Oracle version is not in Classpath. e.g. ojdbc6.jar or ojdbc6_g.jar (compiled with javac -g with debug information) for Oracle 11g.
In this Java program Example, we have seen how to connect to Oracle database using JDBC thin driver, with a thin driver it's much easier to connect to oracle database as you don’t need to create data sources like you do if you use JDBC ODBC Driver. Let me know if you face any issues while connecting to the Oracle database from Java Program. Another worth noting point is connecting the Oracle database using SSL from Java Program, which may see in another java tutorial.
Other Java Programming tutorial you may like