程序如下:
package mypackage;
import java.sql.*;
import java.sql.SQLException.*;
import oracle.jdbc.driver.*;
public class AccessingClass
{
Connection conn;
public void initialize() throws SQLException
{
/*//加载方法(1)
Class.forName("oracle.jdbc.driver.OracleDirver");
//加载方法(2)
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDirver());*/
//加载方法(3)
OracleDriver driver = new OracleDriver();
conn = DriverManager.getConnection
("jdbc:oracle:thin:@simon:1521:oracledb1","sysman","orcl");
conn.setAutoCommit(false);
System.out.println("连接成功!"); //在initialize()方法中:
}
public AccessingClass()
{
}
public static void main(String args[] ) throws SQLException
{
AccessingClass accessingClass = new AccessingClass( );
System.out.println("The arguments are : " + args[0]
+ ", " + args[1] + ", " + args[2]);
accessingClass.initialize();
}
}
出错提示:
Exception in thread main
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
simon:1521:oracledb1
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:279)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:327)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:365)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:150)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:31)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:563)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at mypackage.AccessingClass.initialize(AccessingClass.java:16)
at mypackage.AccessingClass.main(AccessingClass.java:31)
Process exited with exit code 1.