0

I am trying to run a simple Java program which fetches data from the oracle database and display it. I connected the oracle database. Here is my code:

DataHandler Class:

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.jdbc.pool.OracleDataSource;

public class DataHandler {
    public DataHandler() {
        super();
    }
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    //I already added the above line but still getting error.
    String userid = "scott";
    String password = "tiger"; 
    Connection conn;

    public void getDBConnection() throws SQLException{
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setUser(jdbcUrl);
        conn = ds.getConnection(userid,password); 
    }

    Statement stmt;
    ResultSet rset;
    String query;
    String sqlString;

    public ResultSet getAllEmployees() throws SQLException{ 
        getDBConnection(); 
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        query = "SELECT * FROM emp ORDER BY empno";
        System.out.println("\nExecuting query: " + query);
        rset = stmt.executeQuery(query); 
        return rset;
    }
}

and the JavaClient Class as JavaCLient CLass:

import java.sql.ResultSet;
public class JavaClient {
    public JavaClient() {
        super();
    }
    public static void main(String[] args) throws Exception{
        DataHandler datahandler = new DataHandler();
        ResultSet rset = datahandler.getAllEmployees();

        while (rset.next()) {
        System.out.println(rset.getInt(1) + " " +
          rset.getString(2) + " " + 
          rset.getString(3) + " " + 
          rset.getString(4));
        }
    }
}

I get no compilation error but while running it I get following exception error

Error:

Exception in thread "main" java.sql.SQLException: Invalid Oracle URL specified: OracleDataSource.makeURL
at oracle.jdbc.pool.OracleDataSource.makeURL(OracleDataSource.java:1277)
at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:185)
at student_attendence_iem.DataHandler.getDBConnection(DataHandler.java:22)
at student_attendence_iem.DataHandler.getAllEmployees(DataHandler.java:31)
at student_attendence_iem.JavaClient.main(JavaClient.java:9)

Process exited with exit code 1.

Please help me. Thanks in advance. :)

1 Answers1

1

You have not set URL of your database.
Add setURL(url) method which takes URL of database as parameter. Below is the code.

OracleDataSource ds;
    ds = new OracleDataSource();
    ds.setURL(jdbcUrl);

Also, with ds.setUser(jdbcUrl); you are trying to setUser with the URL of database which is wrong.
You don't have to setUser like this as you are already doing that in the following line of code
conn = ds.getConnection(userid,password);

Deepika Rajani
  • 564
  • 5
  • 15