oracle 10g的数据库,在连接的时候报错  Io 异常: The Network Adapter could not establish the connection;如何解决
sqlplus,PLSQL Developer都能连接,就是用代码连接的时候报错,连接类是这样的,哪位大哥知道帮帮忙!package com.cqut.tools;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {

public static final String driver="oracle.jdbc.driver.OracleDriver";
public static final String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
public static final String userName="scott";
public static final String pwd="tiger";
/**
 * 得到连接
 * @return
 */
public static Connection getconn(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,userName,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
 * 关闭连接
 * @param res
 * @param stmt
 * @param conn
 */
public static void closeDB(ResultSet res,PreparedStatement stmt,Connection conn){

try {
if(res != null){
res.close();
}
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}


}
public static void main(String[] args){
if(DBUtil.getconn()==null){
System.out.println("ERROR");
}else if(DBUtil.getconn()!=null){
System.out.println("OK");
}
}
}

解决方案 »

  1.   


    数据库连接错误The Network Adapter could not establish the connection
      

  2.   

    Io 异常: The Network Adapter could not establish the connection 
    这个异常的出现一般与数据库和你的PC的设置有关 这种异常的出现大致上有下面几种: 1。IP错误。 
    在设置URL时错误,例如:jdbc:oracle:thin:@192.168.0.36:1521:sharp 
    数据库服务器不正确:ping 服务器IP是否正确。不正确,将URL更改为正确 
    端口号错误:一般不会错误。 
    进行一下操作:在DOS上键入sqlplus,检查oracle是否开启 
    一却正常,执行下面第2步。 2。防火墙 
    如果机器上安装有放火墙,可能是服务器端口号屏蔽而造成的。关闭防火墙后,尝试重新连接。 
    仍然不行,执行第3步。 3。数据库监听未启动 修改PC上注册表中的ImagePath值。 
    我下面以现在主流的数据库ORACLE为例 
    重新手动启动数据库监听 4.你的jdbc驱动用得有问题,换个ojdbc14.jar