Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To establish a JDBC connection between a Spring Boot application and Snowflake, you will need to follow these steps:

  1. Add the Snowflake JDBC driver to your project's classpath using Maven or Gradle.

    Maven:

    <dependency>
        <groupId>net.snowflake</groupId>
        <artifactId>snowflake-jdbc</artifactId>
        <version>version_number_here</version>
    </dependency>
    

    Gradle:

    implementation 'net.snowflake:snowflake-jdbc:version_number_here'
    
  2. Configure the Snowflake JDBC driver in your application.yml or application.properties file. You will need to provide your Snowflake account information, including the account name, username, password, and a role.

    Example configuration in application.yml:

    spring:
      datasource:
        url: jdbc:snowflake://youraccount.region.snowflakecomputing.com/?user=username&password=password&role=rolename
        driver-class-name: net.snowflake.client.jdbc.SnowflakeDriver
        username: username
        password: password
    
  3. Use Spring's JdbcTemplate or NamedParameterJdbcTemplate to execute SQL queries against your Snowflake database.

    Example usage in a Spring Boot controller:

    @RestController
    public class MyController {
    
        private final JdbcTemplate jdbcTemplate;
    
        public MyController(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        @GetMapping("/customer")
        public List<Customer> getCustomer() {
            return jdbcTemplate.query("SELECT * FROM mydatabase.customer", new BeanPropertyRowMapper<>(Customer.class));
        }
    }
    

    Note: replace "mydatabase.customer" with your actual Snowflake database.table.

  4. Confirm that your Snowflake account's IP whitelist includes the IP address where your Spring Boot application is running.