The following exception occurs when connecting to the database using JDBC: Java. SQL. sqlexception: Io exception: Connection refused (description = (TMP =) (vsnnum = 169869568) (ERR = 12505) (error_stack = (error = (code = 12505) (emfi = 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. (oracleconnection. java: 404) at oracle. JDBC. driver. oracledriver. getconnectioninstance (oracledriver. java: 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 XX. testcon. main (testcon. java: 17)
This indicates thatSid_name Error: JDBC: oracle: thin :@192.168.1.2: 1521:Sid_name, Please checkSid_name,
Because my database is oracleservicedhy, we can see that the SID set for installation at that time is dhy. Now, change the Code as follows:
Try {
// Step1.add drives
Class. forname ("oracle. JDBC. Driver. oracledriver ");
// Step2.get connection
Connection conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 127.0.0.1: 1521:Dhy"," Scott "," Tiger ");
// Step3.creat statement
Statement Sm = conn. createstatement ();
// Step4.result set
Resultset rs1_sm.exe cutequery ("select * from EMP ");
While (Rs. Next () {// has next
System. Out. println ("username:" + Rs. getstring (2 ));
}
}
Catch (exception e ){
E. printstacktrace ();
}
Test Run, OK!