• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes

Problem :

I am very new to JDBC. Now I am trying to connect to the Oracle 11g database using my ojdbc14 jar on the eclipse kepler IDE with the java 8 on my windows 7 PC. But when I try to run the below code I often face the following error.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;

    public class JDBCTest {
      public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Please enter the databse details");
       System.out.println("Please enter user name");
       String userName = scan.next();
       System.out.println("Please enter password");
       String passWord = scan.next();
       try {
       } catch (ClassNotFoundException e) {
       Connection connect = null;
       try {
        connect = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1200:orcl", "scott", "tiger");
        // jdbc:oracle:thin:@server:1521:xe
       } catch (SQLException e) {
       if (connect != null) {
        System.out.println("Successfully connected to your DB !!!!");
        } else {
        System.out.println("Failed to connect to your DB");

And the encountered Error is as below :

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

by (1.6k points)  

1 Answer

0 votes

Solution :

I was going through your code which you have posted in your question after going through your jdbc url, I have found the cause behind your error.

localhost:1200/xe should be changed to localhost:1200:xe if xe is your SID for the database.

So please change below code from

connect = DriverManager.getConnection(
          "jdbc:oracle:thin:@localhost:1200/xe", "scott", "tiger");

To the following code.

connect = DriverManager.getConnection(
          "jdbc:oracle:thin:@localhost:1200:xe", "scott", "tiger");

This will fix all the errors.

by (3.1k points)