java 获取异常:网络适配器无法在简单的 jdbc 程序中建立连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11599312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting exception: The Network Adapter could not establish the connection in simple jdbc program
提问by animark
I was trying to run a simple jdbc code [ using jdk 1.6, oracle 10g] as,
我试图运行一个简单的 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
The code is getting compiled properly. But when I'm trying to run it, i'm getting the following exception..
代码正在正确编译。但是当我尝试运行它时,我收到以下异常..
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)
I've checked the oracle services and all of them are up and running.
我检查了 oracle 服务,它们都已启动并正在运行。
Also, please find the contents of other files as
另外,请查找其他文件的内容为
================================================================================= tnsnames.ora:
================================================== ================================ tnsnames.ora:
# tnsnames.ora Network Configuration File: C:\oraclexe\app\oracle\product.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.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.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.2.0\server\BIN)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
===============================================================================
================================================== ==============================
I'm being able to connect to the instance "orcl" using credentials "scott/password", but when i'm trying to connect using statement
我能够使用凭据“scott/password”连接到实例“orcl”,但是当我尝试使用语句进行连接时
SQL> connect sys/password@orcl as sysdba
SQL> 将 sys/password@orcl 连接为 sysdba
I'm getting the following error.. ORA-12514: TNS:listener does not currently know of service requested in connect
我收到以下错误.. ORA-12514: TNS:listener 目前不知道连接中请求的服务
descriptor.
描述符。
I've also run LSNRCTL for orcl and found
我还为 orcl 运行了 LSNRCTL 并发现
LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 22-JUL-2012 13:42:30
用于 32 位 Windows 的 LSNRCTL:版本 10.2.0.1.0 - 2012 年 7 月 22 日 13:42:30 生产
Copyright (c) 1991, 2005, Oracle. All rights reserved.
版权所有 (c) 1991、2005,Oracle。版权所有。
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
Please help me if I'm doing anything wrong here.
如果我在这里做错了什么,请帮助我。
回答by Luke Woodward
Try taking the //
out of the connection URL. Instead of
尝试去掉//
连接 URL。代替
String url= "jdbc:oracle:thin:@//localhost:1521:orcl" ;
try
尝试
String url= "jdbc:oracle:thin:@localhost:1521:orcl" ;