java.sql.SQLRecoverableException: The Network Adap

2019-09-07 06:04发布

问题:

Here's the code I'm using (Oracle database connectivity):

try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@loclahost:1521:XE","system","system");
        System.out.println("connection is established");
        Statement stmt=conn.createStatement();
        int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
        System.out.println("Save Sucessfully");
        stmt.close();
        conn.close();
} catch(Exception e) {
    System.out.println(e);
}
this.dispose();

Getting following Error:

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

回答1:

You've got a typo in your connection string - use localhost instead of loclahost



回答2:

use localhost in DriverManager.getConnection

Here is the code :

try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
    System.out.println("connection is established");
    Statement stmt=conn.createStatement();
    int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
    System.out.println("Save Sucessfully");
    stmt.close();
    conn.close();
  }
  catch(Exception e) {
  System.out.println(e);
 }
  this.dispose();
}


回答3:

The error could occur if your JDBC driver is unable to connect to oracle. Check if your oracle service is running and no firewall is blocking your connection.

Check if oracle is listening in port 1521 or not, if not fix the port issue and then try connecting to your database.

Collectively the things to check ,

Check if the listener service is running

No firewall is blocking

Your service is listening on the correct port number that you specified in your code.



标签: java oracle jdbc