Pages

Saturday, November 12, 2011

"java.sql.SQLException: Io exception: The Network Adapter could not establish the connection" - A simple way to handle it


Following is a most common exception that one would face if connection to Oracle server is denied for some reason.

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


All you got to do is ensure that you add classes12.jar to the Java build path of you Java project. You can navigate to this point by right clicking on your project and clicking on properties



Following is a sample class that I used to connect to Oracle

import java.sql.*;

public class OracleConnectionUrl {
       public static void main(String[] args) {
              Connection con = null;

              System.out.println("about to load");
              // Load the Oracle JDBC driver
              try {
                     Class.forName("oracle.jdbc.OracleDriver");
              } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
              System.out.println("Oracle JDBC driver loaded ok.");

              try {
                     con = DriverManager
                                  .getConnection("jdbc:oracle:thin:karthick/password@10.116.51.215:1521:XHF");
                     con.close();
              } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
}
}

Hope this helps :)

No comments:

Post a Comment