3

I am trying to establish a connection with Salesforce via JDBC driver which I got from https://github.com/ascendix/salesforce-jdbc

I have set up this driver in SQLWorkbench tool. The connection is successful. enter image description here But when I execute any query like SELECT Id from Account it fails with error

Detail Log:

2019-08-14 00:03:14 INFO  Creating new connection for [{Default group}/SFJDBC  -- https://github.com/ascendix/salesforce-jdbc] for driver=com.ascendix.jdbc.salesforce.ForceDriver and URL=[jdbc:ascendix:salesforce://https://login.salesforce.com/services/Soap/u/37.0;user=xyz;password=xyz]
2019-08-14 00:03:16 INFO  WbWin-1 TAB-1: Using DBID=ascendix_jdbc_driver_for_salesforce 
2019-08-14 00:03:16 INFO  WbWin-1 TAB-1: Using identifier quote character: " 
2019-08-14 00:03:16 INFO  WbWin-1 TAB-1: Using search string escape character:  
2019-08-14 00:03:16 WARN  WbWin-1 TAB-1: The driver did not return any table types using getTableTypes(). Using default values: [TABLE, VIEW] 
2019-08-14 00:03:16 INFO  WbWin-1 TAB-1: Using catalog separator: . 
2019-08-14 00:03:16 INFO  Connected to: [Ascendix JDBC driver for Salesforce], Database version info: [39], Database version number: [39.0], Driver version: [1.1], JDBC version: [4.0], ID: [WbWin-1 TAB-1] 
2019-08-14 00:03:19 WARN  The JDBC driver does not support the setMaxRows() function! (null) 
2019-08-14 00:03:19 ERROR Error executing:
SELECT Id from Account
   java.lang.NullPointerException
java.lang.NullPointerException
    at workbench.sql.commands.SelectCommand.execute(SelectCommand.java:131)
    at workbench.sql.StatementRunner.runStatement(StatementRunner.java:552)
    at workbench.gui.sql.SqlPanel.displayResult(SqlPanel.java:3468)
    at workbench.gui.sql.SqlPanel.runStatement(SqlPanel.java:2199)
    at workbench.gui.sql.SqlPanel$16.run(SqlPanel.java:2137)

I checked the Database Explorer-> Objects tab has a list of all objects (Account, Contact, etc.) But when I clicked on Data tab to check the data there also got the same above error.

So I even tried to write JDBC code in eclipse there also connection was successful but on executing code I got below error

 Connected to Salesforce.
com.ascendix.jdbc.salesforce.ForceConnection createStatement
INFO: createStatement
java.lang.NullPointerException
    at SFJDBC.main(SFJDBC.java:77)

Code for Reference:

try 
    {

        final String UserName = "user";

        final String Password = "password";

        String dbURL = "jdbc:ascendix:salesforce://login.salesforce.com";
        String dbUsername = "xyz";
        String dbPassword = "xyz";

        java.util.Properties connProperties = new java.util.Properties();
        connProperties.put(UserName, dbUsername);
        connProperties.put(Password, dbPassword);

        Class.forName("com.ascendix.jdbc.salesforce.ForceDriver");
        Properties connectionProps = new Properties();
        connectionProps.put(UserName,dbUsername);
        connectionProps.put(Password,dbPassword);
        Connection connection = DriverManager.getConnection(dbURL, connectionProps);
        System.out.println(connection.toString());

        if(!connection.isClosed())
        {
            System.out.println("Connected to Salesforce.");

            Statement stmt = null;
            ResultSet rs = null;
            stmt = connection.createStatement();
            rs = stmt.executeQuery("SELECT Id FROM ACCOUNT");
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }

        }   

    } 



    catch (Exception e) {
        e.printStackTrace();

    }

I am more concerned with making driver work for SQL Workbench. Any help to resolve this will be really appreciated.

**** UPDATE ****

Thanks to @Mark Rotteveel based on his suggestion made changes to the code and post that code was successful. But need to make this driver work in SQL Workbench as well.

PreparedStatement preparedStatement = connection.prepareStatement("SELECT Id FROM Account limit 3");
            preparedStatement = connection.prepareStatement("SELECT Id FROM Account limit 3");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next())
            {
                 System.out.println(resultSet.getString(1));
            }
Vikas J
  • 795
  • 3
  • 14
  • 31
  • Which line is line 77 in your code where the NPE is thrown? It sounds like that JDBC driver is not compliant with the JDBC specification and returns `null` at points where a well-behaved JDBC driver would throw a SQLException or return a non-null object. You will need to report a bug to whoever wrote that driver. – Mark Rotteveel Aug 13 '19 at 09:24
  • Line 77 -> rs = stmt.executeQuery("SELECT Id FROM ACCOUNT"); – Vikas J Aug 13 '19 at 09:31
  • 1
    That would suggest `connection.createStatement()` returns `null`, which is a violation of the JDBC specification: report a bug with the author of this driver. See also [this line in the implementation](https://github.com/ascendix/salesforce-jdbc/blob/master/sf-jdbc-driver/src/main/java/com/ascendix/jdbc/salesforce/connection/ForceConnection.java#L76). Workaround seems to be to use a prepared statement instead. – Mark Rotteveel Aug 13 '19 at 09:42
  • Thanks a lot, @MarkRotteveel changing to PreparedStatment worked. But like I had said my actual use case is to use this JDBC Driver(Jar) in SQL Workbench tool. Any idea on how I can do that? – Vikas J Aug 13 '19 at 10:00
  • You probably can't, other than reporting a bug to the author of that JDBC driver and hoping they fix it. – Mark Rotteveel Aug 13 '19 at 10:04
  • Please send the complete (!) logfile to the SQL Workbench support address (Help --> Contact the author). This wouldn't be the first JDBC driver that isn't implementing everything I expect in SQL Workbench. But usually there is a workaround that I can implement –  Aug 13 '19 at 17:25
  • @SQLWorkbenchJ I have sent mail to support. And also updated my post with detail log for your reference. – Vikas J Aug 13 '19 at 19:07
  • Looking at the logfile and your sample code, it seems pretty clear that `Connection.createStatement()` simply does not work with that driver. That is a deficiency that I did not expect. I am sorry, but there is nothing you can do with the SQL Workbench configuration to workaround that bug. –  Aug 13 '19 at 19:45
  • Thanks for looking into the log. So is there any other Open Source JDBC driver for Salesforce ? – Vikas J Aug 14 '19 at 16:31

0 Answers0