代码:
import java.sql.*;
class test {
public static void main(String args[])
throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@202.203.187.13:1521:ynau";
String uid="scott";
String pwd="test";
Connection conn = DriverManager.getConnection(url,uid,pwd);
Statement stmt = conn.createStatement( );
ResultSet rset = stmt.executeQuery("select * from dept");
while(rset.next( ))
System.out.println(rset.getString(1));
rset.close( );
stmt.close( );
conn.close( );
}
}
错误:
Exception in thread "main" java.sql.SQLException: Io 异常: Connection refused(DE
SCRIPTION=(TMP=)(VSNNUM=153092352)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EM
FI=4))))
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:404) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja
va:468)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test.main(test.java:9)
监视器打开后显示:
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SUNSERVER1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Solaris: Version 9.2.0.1.0 - Production
Start Date 08-DEC-2005 10:51:21
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security OFF
SNMP OFF
Listener Parameter File /raid5/oracle/app/oracle/product/9.2.0.1/network/admin/listener.ora
Listener Log File /raid5/oracle/app/oracle/product/9.2.0.1/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=SUNSERVER1)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "ynau" has 1 instance(s).
Instance "ynauora", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
import java.sql.*;
class test {
public static void main(String args[])
throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@202.203.187.13:1521:ynau";
String uid="scott";
String pwd="test";
Connection conn = DriverManager.getConnection(url,uid,pwd);
Statement stmt = conn.createStatement( );
ResultSet rset = stmt.executeQuery("select * from dept");
while(rset.next( ))
System.out.println(rset.getString(1));
rset.close( );
stmt.close( );
conn.close( );
}
}
错误:
Exception in thread "main" java.sql.SQLException: Io 异常: Connection refused(DE
SCRIPTION=(TMP=)(VSNNUM=153092352)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EM
FI=4))))
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:404) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja
va:468)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test.main(test.java:9)
监视器打开后显示:
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SUNSERVER1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Solaris: Version 9.2.0.1.0 - Production
Start Date 08-DEC-2005 10:51:21
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security OFF
SNMP OFF
Listener Parameter File /raid5/oracle/app/oracle/product/9.2.0.1/network/admin/listener.ora
Listener Log File /raid5/oracle/app/oracle/product/9.2.0.1/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=SUNSERVER1)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "ynau" has 1 instance(s).
Instance "ynauora", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
把URL改成这个试试
看了一下,形式上没有错误,再从错误提示推断出,也不是形式上的错误。
在sql*plus下或SQLPlus Worksheet下,用scott test还有你给出的ip连接你需要连接的数据库ynau,看能不能连接上。主要看一下你的ip 和数据库名字ynau 、用户名和密码确实是这个吗。还有scott的默认密码是tiger,你确认改成test了吗
总之,你想在java中连接数据库,你首先要确保在oracle本身的登陆中能登陆。
"jdbc:oracle:thin:@(description=(address=(host=202.203.187.13)(protocol=tcp)(port=1521))(connect_data=(service_name = ynau)))"试试看~
YNAU =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 202.203.187.13)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ynau)
)
)
"jdbc:oracle:thin:@(description=(address=(host=202.203.187.13)(protocol=tcp)(port=1521))(connect_data=(service_name = ynau)))"
可以了,这是为什么啊