none
SQL JDBC connection cannot be established

    Question

  • I try to establish the connection to a SQL Server 2008 through sqljdbc4.jar file. But I always get following exception. 

    Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerException (no security manager: RMI class loader disabled)
        at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:373)
        at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:163)
        at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:620)
        at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:247)
        at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:197)
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1574)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:225)


    In jre java.policy file, I have enable all permission as below. The sqljdbc4.jar file truly is in the classpath. I have tried the lastest version of jdbc driver. It still cannot work. Please give me some suggestion, thanks!!
    ...
    grant {
      permission java.security.AllPermission;
    };

    **********************************************************************

    Following is my code to establish connection. It can dynamically load jdbc driver and establish connection:

    /**
    * Get JDBC Connection for the current database connection instance.
    */
    public Connection getJDBConnection() throws Exception
    {
    DBDriver dbDriver = getDriver();
    try
    {
    Class.forName(dbDriver.getClassName());
    }
    catch (ClassNotFoundException cnf)
    {
    ClassPathHacker.addFile(dbDriver.getFilePath());
    Class.forName(dbDriver.getClassName());
    }


    try
    {
    Connection connection =  DriverManager.getConnection(getUrl(), getUserId(), getOrigiPassword());
    System.out.println("build conenction done");
    return connection;
    }
    catch (SQLException e)
    {
    // Client side does not have JDBC jar, so the SQLException thrown
    // from the Oracle/SQL Server JDBC driver will be unknown on client side.
    // So we need to do following conversion.
    SQLException sqlExp = new SQLException(e.getMessage());
    sqlExp.setStackTrace(e.getStackTrace());
    throw sqlExp;
    }
    }

    Thanks,

    Alvin

    Thursday, August 14, 2014 8:52 AM

Answers

  • I found the reason. 

    The exception is shown at the Client side. Client side has no sqljdbc4.jar, So once  Server Side throws a com.microsoft.sqlserver.jdbc.SQLServerException to client side, the SQLServerException will be converted to ClassNotFoundException.

    So we need to convert SQLServerException to normal Exception at server side, and then, throw it to client.
    Friday, August 15, 2014 2:15 AM