代码中的sqlException,但可以通过数据库开发透视图连接到数据库

时间:2018-05-28 20:13:58

标签: oracle jdbc sqlexception

我已经做了好几天了,我到处研究过。无论如何,尝试将我的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

1 个答案:

答案 0 :(得分:0)

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

  

JDBC:预言:瘦:@localhost:1522:ORCLPJC

  

JDBC:预言:瘦://本地主机:1522 / ORCLPJC

这两个中哪一个实际上是导致错误的? 当你改变它以使用另一个时会发生什么?