gpt4 book ai didi

java - 使用 JDBC 在 localhost 连接 oracle 11G 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:55 25 4
gpt4 key购买 nike

import java.sql.*;

public class one {

public static void main(String[] args)

{
Connection conn=null;
Statement stmt=null;

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/ecom","system","manager");
stmt=conn.createStatement();
String str="insert into country values(3,'bangladesh');";
int k=stmt.executeUpdate(str);
System.out.println(k);
}
catch(Exception e)
{
e.printStackTrace();

}

}
}

我添加到类路径中的唯一 jar 文件是 ojdbc6.jaroracle 11g(11.2.0.1.0) 连接还需要其他 jar 文件吗? 我正在使用 eclipse 作为 IDE

这里是错误

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 one.main(one.java:17)
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.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocket`Impl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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)

最佳答案

1) 请确保数据库主机名、端口号或数据库实例名称正确。2) 确保 TNSListener 已启动(运行 lsnrctl start)

关于java - 使用 JDBC 在 localhost 连接 oracle 11G 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15461866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com