使用java连接oracle数据库

ydww 发布于 2013/09/17 10:45
阅读 573
收藏 0

直接上代码:

<%!
 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
 public static final String DBURL = "jdbc:oracle:thin: @localhost :1521:yangyihong" ;
 public static final String DBUSER = "scott" ;
 public static final String DBPASSWORD = "tiger" ;
%>
<%
 Connection conn = null ;
 PreparedStatement pstmt = null ;
 ResultSet rs = null ;
%>
<%
 Class.forName(DBDRIVER) ;
 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
 String sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp ORDER BY empno" ;
 pstmt = conn.prepareStatement(sql) ;
 rs = pstmt.executeQuery() ;
%>

而此时,会报错,提示如下

java.sql.SQLException: Listener refused the connection with the following error:
ORA-12518, TNS:listener could not hand off client connection
The Connection descriptor used by the client was:
localhost:1521:yangyihong

小弟已经进入到D:\oracle\product\10.2.0\db_1\NETWORK\ADMIN\tnsnames.ora目录下,确认了我的文件内容为

# tnsnames.ora Network Configuration File: D:\oracle\product\10.2.0\db_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

YANGYIHO =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = LBDZ-03081718)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = yangyihong)
    )
  )

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

按照上面内容,localhost:1521:yangyihong这个写法应该没错啊。另外,各种服务也都已经启动了,通过sqlplusw可以正常启动数据库。求大神解救。

加载中
0
夏夜流星
夏夜流星
Oracle中的 tnsnames.ora文件内YANGYIHO和你的java的连接的URL中的yangyihong对不上
ydww
ydww
谢了!果真是这原因!
0
IdleMan
IdleMan

jdbc:oracle:thin: @YANGYIHO

或者

jdbc:oracle:thin: @LBDZ-03081718:1521:yangyihong

返回顶部
顶部