日期:2014-05-19  浏览次数:20750 次

找不到原因请高手指教

我链接数据库的代码如下:
package connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager 
{
private static Connection conn;
public static Connection getConnection()
{


try 
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();


catch (InstantiationException e) 
{

e.printStackTrace();
}
catch (IllegalAccessException e) 
{
e.printStackTrace();


catch (ClassNotFoundException e) 
{

e.printStackTrace();
}
//加载驱动
String url="jdbc:oracle:thin:@localhost:1521:mldn"; //1521是端口 orcl是数据库名称
String user="mldn";String password="oracleadmin"; //用户名 密码
try
{
Connection conn= DriverManager.getConnection(url,user,password);
} catch (SQLException e)
{

e.printStackTrace();
} //创建链接 



return conn;
}

public static void destory()
{
try
{
conn.close();//关闭数据库

}catch(SQLException e)
{

e.printStackTrace();

}


}












}

查询代码:
package connection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ResultSetTest 
{
public static void main(String[]args)
{
Connection conn = ConnectionManager.getConnection();
Statement st=null;
try
{
st= conn.createStatement();
//执行查询操作并将结果存入ResultSet中
ResultSet rs=st.executeQuery("select * from emp");
//判断rs是否有下一笔记录
while(rs.next())
{
String name = rs.getString("ename");

System.out.println("雇员的姓名是:" + name);

}

}
catch(SQLException e)
{

e.printStackTrace();

}
finally
{
try
{
st.close();
conn.close();

}
catch(SQLException e)
{
e.printStackTrace();

}


}








}




}

启动后总是报这样的错误,不知道原因,请牛人,高手指教!多谢多谢!

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:
localhost:1521:mldn

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:260)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:386)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:413)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:164)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:3