Я работал над этим в течение нескольких дней, и я исследовал везде.Во всяком случае, пытаясь подключить мой код 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