sql异常错误

时间:2012-03-01 05:36:48

标签: java sql jdbc

我正在尝试从java程序执行sql命令..我没有关于此代码的任何错误..但我面临来自数据库的连接拒绝..

import java.sql.*;
public class DBCreateTable
{
    public static void main(String args[]) throws Exception
    {
        DriverManager.registerDriver (new Oracle.jdbc.driver.OracleDriver());
        Connection con=DriverManager.getConnection(
              "jdbc:oracle:thin:@localhost:1521:xe","lms","abc");
        Statement stmt=con.CreateStatement();
        stmt.executeUpdate("create table emp(eno number(5),name varchar2(20))");
    }
}

遇到的错误是:

Exception in thread "main" java.sql.SQLException: Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=185599488)(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.<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(DriverManager.java:579)
        at java.sql.DriverManager.getConnection(DriverManager.java:221)
        at DBCreateTable.main(DBCreateTable.java:7)

在我的sql命令中,我完成了以下操作..

    SQL> connect system/tiger;
    SQL> create user lms identified by abc;
    SQL> grant connect,resource to lms;

并告诉我什么是斯科特老虎..我在那里弄得很多......那里的用户是什么......解锁怎么样?谢谢..

5 个答案:

答案 0 :(得分:2)

你的代码

Connection con=DriverManager.getConnection(
          "jdbc:oracle:thin:@localhost:1521:xe","lms","abc");

将其更改为

Connection con=DriverManager.getConnection(
          "jdbc:oracle:thin:@//localhost:1521:xe","lms","abc");

有关详细信息,请参阅

http://www.orafaq.com/wiki/JDBC

需要另一项改变

stmt.executeUpdate("create table emp(eno number(5),name varchar2(20))");

更改为

stmt.executeUpdate("create table emp(eno number(5),name varchar2(20));");

答案 1 :(得分:1)

可能您的oracle服务已停止。 scott / tiger是oracle中的默认用户名密码之一。您应该在代码中使用try / catch。

答案 2 :(得分:1)

让我们看看错误描述的内容:

>oerr ora 12505
12505, 00000, "TNS:listener does not currently know of SID given in connect descriptor"
// *Cause:  The listener received a request to establish a connection to a
// database or other service. The connect descriptor received by the listener
// specified a SID for an instance (usually a database instance) that either
// has not yet dynamically registered with the listener or has not been
// statically configured for the listener. This may be a temporary condition
// such as after the listener has started, but before the database instance
// has registered with the listener.
// *Action:
//  - Wait a moment and try to connect a second time.
//  - Check which instances are currently known by the listener by executing:
//    lsnrctl services <listener name>
//  - Check that the SID parameter in the connect descriptor specifies
//    an instance known by the listener.
//  - Check for an event in the listener.log file.

答案 3 :(得分:0)

另一个问题是您可能在类路径中有较旧的驱动程序(因此请确保您没有class111.jar或运行时类路径中较旧的东西)。

确保在类路径中有正确的驱动程序(thin或oci)。

答案 4 :(得分:0)

试试这段代码:

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "lms", "abc");