Home > database >  How to write SQL query in Java to get data from Snowflake
How to write SQL query in Java to get data from Snowflake

Time:10-20

I am trying to write this Snowflake query into Java code:

copy into s3://snowflake171
  from USER_TABLE
  storage_integration = s3_int
  file_format = CSV_TEST;

I am writing it like this:

 String q ="COPY INTO s3://snowflake171\n"  
                    "  FROM \"TEST\".\"PUBLIC\".\"USER_TABLE\"WHERE\"ID\\\"=?\"\n"  
                    "  storage_integration = s3_int\n"  
                    "  file_format = CSV_TEST";

But I am getting this error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [COPY INTO s3://snowflake171
  FROM "TEST"."PUBLIC"."USER_TABLE"WHERE"ID\"=?"
  storage_integration = s3_int
  file_format = CSV_TEST]; nested exception is net.snowflake.client.jdbc.SnowflakeSQLException: SQL compilation error:
syntax error line 1 at position 5 unexpected '<EOF>'.

What am I missing?

CodePudding user response:

As the latest error message is "SQL access control error: Insufficient privileges to operate on integration 'S3_INT'", can you try to grant USAGE permission on s3_int to your role? Are you sure you set the correct role when connecting?

String url = "jdbc:snowflake://<account_identifier>.snowflakecomputing.com";
Properties prop = new Properties();
prop.put("user", "<user>");
prop.put("privateKey", PrivateKeyReader.get(PRIVATE_KEY_FILE));
prop.put("db", "<database_name>");
prop.put("schema", "<schema_name>");
prop.put("warehouse", "<warehouse_name>");
prop.put("role", "<role_name>");  
  • Related