khirod khirod - 8 months ago 53
Java Question

Unable to connect to oracle database using jdbc thin drivers

I am trying to connect to Oracle 10 XE database using Oracle thin drivers but I'm unable to achieve this. I even added the drivers to my classpath during compilation.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class Connectivity {
public static void main(String args[]) throws SQLException {
String url = "jdbc:oracle:thin:@localhost:1521:XE";
Properties props = new Properties();

props.setProperty("user", "SYSTEM");
props.setProperty("password", "iiita");

try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
Connection conn = DriverManager.getConnection(url,props);
String sql ="select sysdate as current_day from dual";

PreparedStatement preStatement = conn.prepareStatement(sql);

ResultSet result = preStatement.executeQuery();

System.out.println("Current Date from Oracle : " + result.getString("current_day"));



This code gives the following error during running

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521/XE
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at Connectivity.main(


You forgot to specify the ojdbc14 classpath when running the main class:

For Windows: java -cp .;<path>/ojdbc14.jar Connectivity

On Linux: java -cp .:<path>/ojdbc14.jar Connectivity

P.S. You don't need ojdbc14.jar on your classpath to compile the main class. It's a runtime dependency.