【问题标题】:sqlException in code, but can connect to database through the database development perspective代码中出现sqlException,但可以通过数据库开发视角连接数据库
【发布时间】:2018-11-07 10:27:28
【问题描述】:

我已经为此工作了好几天,并且到处进行了研究。无论如何,尝试将我的 Java 代码连接到我的 SQL 数据库。使用数据库开发视角时,我能够连接并发出 ping 请求。 SQL Developer 等其他工具也是如此。我的连接设置与其他设置相似,但仍然出现相同的错误。关于我可能做错的任何建议?

import java.sql.*;
import java.io.*;
import oracle.jdbc.*;

public class cars 
{
    private static final String username = "SYSMAN";
    private static final String password = "*";
    public static void insertCar (String id, String name, String model, String type) throws SQLException
    {   
        String sql = "Insert into Cars Values(?,?,?,?)";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, model);
            pstmt.setString(4, type);
            pstmt.executeUpdate();
            pstmt.close();

        }catch(SQLException e) 
        {
            System.err.println(e.getMessage());
        }
    }

    public static void updateCar (String type, String id) throws SQLException
    {
        String sql = "Update cars set type = ?" + "Where id = ?";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, type);
            pstmt.setString(2, id);
            pstmt.executeUpdate();
            pstmt.close();

        }catch(SQLException e) 
        {
            System.err.println(e.getMessage());
        }
    }
    public static void deleteCar(String id) throws SQLException
    {
        String sql = "Delete car from which id = ?";
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1522:ORCLPJC", username, password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            pstmt.close();
            }catch (SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }
}

显示此错误:

java.sql.SQLException: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at OracleDBConnect.main(OracleDBConnect.java:14)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:359)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:422)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:672)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:237)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:141)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:123)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:337)
    ... 12 more

【问题讨论】:

  • 那么,您的数据库侦听器是否真的为端口1522 配置?只是问一下,因为端口1521 是默认值,大多数地方都保留它。如果你从命令行运行tnsping ORCLPJC,你会得到什么?
  • 这就是 tnsnames.ora 文件中显示的内容。我与其他有效的工具一起使用的设置相同,但不适用于此代码
  • @AnoopJ,我会调查的。看起来很相似,谢谢
  • @AnoopJ,检查了它,但第一个不适用,因为我通过 services.msc 启动并重新启动了所有 oracle 服务。第二个我想我只是不完全理解他的意思

标签: oracle jdbc sqlexception


【解决方案1】:

为什么会有两个不同的连接字符串

jdbc:oracle:thin:@localhost:1522:ORCLPJC

jdbc:oracle:thin://localhost:1522/ORCLPJC

这两者中的哪一个实际上导致了错误? 当您将其更改为使用另一个时会发生什么?

【讨论】:

  • 更改了我的代码。在高级 SQL Developer 类型上对其进行了测试,它与 JDBC URL 一起使用,在上面的代码中对其进行了更改,但与代码相同的错误。我猜 Java 与数据库的连接有问题?
  • 查看 Github 上的 DataSourceSample(github.com/oracle/oracle-db-examples/blob/master/java/jdbc/…) 以获取示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多