ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

来源:互联网 发布:守望先锋吧被关 知乎 编辑:程序博客网 时间:2024/06/05 04:01

详细错误:

java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
127.0.0.1:1521:orcl

问题描述:
使用navicat可以登录,代码不能。网上查阅说是服务器listener.ora配置问题。最后发现并不是。

jdbc代码:

public static void main(String[] args) {        Connection con = null;// 创建一个数据库连接        PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement        ResultSet result = null;// 创建一个结果集对象        try        {            Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序            System.out.println("开始尝试连接数据库!");            String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";// 127.0.0.1是本机地址            String user = "scott";// 用户名,系统默认的账户名            String password = "tiger";// 你安装时选设置的密码            con = DriverManager.getConnection(url, user, password);// 获取连接            System.out.println("连接成功!");            String sql = "select 2*2 from dual";// 预编译语句            pre = con.prepareStatement(sql);// 实例化预编译语句            result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数            while (result.next())                // 当结果集不为空时                System.out.println();        }        catch (Exception e)        {            e.printStackTrace();        }        finally        {            try            {                // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源                // 注意关闭的顺序,最后使用的最先关闭                if (result != null)                    result.close();                if (pre != null)                    pre.close();                if (con != null)                    con.close();                System.out.println("数据库连接已关闭!");            }            catch (Exception e)            {                e.printStackTrace();            }        }    }

生效代码:将url中port:sid修改成port/sid就成功了。

感谢stackoverflow.

阅读全文
0 0
原创粉丝点击