gpt4 book ai didi

java - 获取异常 : The Network Adapter could not establish the connection in simple jdbc program

转载 作者:行者123 更新时间:2023-11-29 03:44:37 27 4
gpt4 key购买 nike

我试图运行一个简单的 jdbc 代码 [使用 jdk 1.6,oracle 10g] 作为,

package javaapplication2;
import java.text.*;
import java.sql.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
/**
* @author animark
*/
public class CallableStatementEx1 {
public CallableStatementEx1(){;}

public static void main(String s[]) throws Exception {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=null;
String url= "jdbc:oracle:thin:@//localhost:1521:orcl" ;
con = DriverManager.getConnection(url,"scott","password");

String query="update emp set HIREDATE=?,ENAME=? where empno=?";

//Step1: Get PreparedStatement
PreparedStatement ps=con.prepareStatement(query);

//Prepare java.sql.Date object
/*
This logic shows how to convert simple String that is in
dd-MM-yyyy format into Date object
*/
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
java.util.Date d=sdf.parse("26-12-2001");
java.sql.Date newdate=new java.sql.Date(d.getTime());

//Step2: set parameters
ps.setDate(1,newdate);
ps.setString(2,"animark");
ps.setInt(3,7839);

//Step3: execute the query
int i=ps.executeUpdate();

System.out.println("record updated count: "+i);
con.close();
}
catch(Exception e)
{
e.printStackTrace();

}
}//main
}//class

代码正在正确编译。但是当我尝试运行它时,出现以下异常。

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at javaapplication2.CallableStatementEx1.main(CallableStatementEx1.java:19)

我检查了 oracle 服务,它们都已启动并正在运行。

另外,请查找其他文件的内容为

============================================= ==================================tnsnames.ora:

# tnsnames.ora Network Configuration File: C:\oraclexe\app\oracle\product\10.2.0\server\BIN\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)

EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)

============================================= =====================================

sqlnet.ora


# sqlnet.ora Network Configuration File: C:\oraclexe\app\oracle\product\10.2.0\server\BIN\network\admin\sqlnet.ora
# Generated by Oracle configuration tools.

# This file is actually generated by netca. But if customers choose to
# install "Software Only", this file wont exist and without the native
# authentication, they will not be able to connect to the database on NT.

SQLNET.AUTHENTICATION_SERVICES= (NTS)

NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
================================================================================

# listener.ora Network Configuration File: C:\oraclexe\app\oracle\product\10.2.0\server\BIN\network\admin\listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = C:\oraclexe\app\oracle\product\10.2.0\server\BIN)
(PROGRAM = extproc)
)
)

LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)

============================================= ================================

我能够使用凭据“scott/password”连接到实例“orcl”,但是当我尝试使用语句连接时

SQL> 以 sysdba 身份连接 sys/password@orcl

我收到以下错误..ORA-12514: TNS: 监听器当前不知道连接中请求的服务

描述符。

我也为 orcl 运行了 LSNRCTL 并找到了

适用于 32 位 Windows 的 LSNRCTL:版本 10.2.0.1.0 - 2012 年 7 月 22 日 13:42:30 生产

版权所有 (c) 1991、2005,甲骨文。保留所有权利。

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Handler(s):
"DEDICATED" established:0 refused:0
LOCAL SERVER
The command completed successfully

如果我在这里做错了什么,请帮助我。

最佳答案

尝试从连接 URL 中取出 //。而不是

    String url= "jdbc:oracle:thin:@//localhost:1521:orcl" ;   

尝试

    String url= "jdbc:oracle:thin:@localhost:1521:orcl" ;   

关于java - 获取异常 : The Network Adapter could not establish the connection in simple jdbc program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599312/

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