Skip to content
Advertisement

Snowflake exception While unwrapping snowflake objects

I am Using snowflake-jdbc-13.3.8.jar. while executing below code having unwrap call, I am getting exception. Pls guide what am I missing?

    CallableStatement st = connection.prepareCall("CALL TEST_SP(?)");
    st.setString(1, "Value1");
    ResultSet resultSet = st.executeQuery();
    String queryId = resultSet.unwrap(SnowflakeResultSet.class).getQueryId();

    Net.snowflake.client.jdbc.SnowflakeCallableStatementV1 not unwrappable from net.snowflake.client.jdbc.SnowflakeStatement

Advertisement

Answer

You’re calling getQueryId() rather than getQueryID(), observe ID is in capitals. The unwrap works, here is an example.

First I create a simple stored procedure:

CREATE or replace PROCEDURE TEST_SP(v varchar)
  RETURNS VARCHAR
  LANGUAGE javascript
  AS
  $$
  return 'Done.';
  $$;

I test it to see it works:

CALL TEST_SP('Value1');

It returns to me just the string “Done”.

Then I create a simple Java class:

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.*;
import net.snowflake.client.jdbc.*;

public class SampleUnwrap {
        public static void main(String[] args) {
                String user = "<user>";
                String password = "<pass>";
                String account = "<account>";
        String JDBC_DRIVER = "net.snowflake.client.jdbc.SnowflakeDriver";
                String warehouse = "<wh>";
        String role = "<role>";
        String db = "<db>";
        String schema = "PUBLIC";
                String queryString = "SSL=on&tracing=ALL&database=" + db + "&role=" + role +  "&warehouse=" + warehouse;
                String connectionURL = "jdbc:snowflake" + "://" + account + ".snowflakecomputing.com" + "/?" + queryString;
                Connection conn1;

                try {
                        conn1 = DriverManager.getConnection(connectionURL, user, password);
            CallableStatement stmt = conn1.prepareCall("CALL TEST_SP(?)");
            stmt.setString(1, "Value1");

            ResultSet resultSet = stmt.executeQuery();

            String queryId = resultSet.unwrap(SnowflakeResultSet.class).getQueryID();

            System.out.println(queryId);
            resultSet.close();
                        conn1.close();
                } catch (Exception e) {
                        StringWriter sw = new StringWriter();
                        e.printStackTrace(new PrintWriter(sw, true));
                        System.out.println(sw);
                }
        }

}

When I run it I get the queryID back:

java -cp .:snowflake-jdbc-3.13.8.jar SampleUnwrap.java
01a166f7-0403-5054-0000-16490d51d2c2

Post the full class and details about environment if it’s still not working for you.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement