Integration Of Flutter With Snowflake Using Native Java

Author: Mridul Vij


This Blog provides information on the Connection between User Applications made on Flutter and Snowflake with the use of Native Java and Snowflake JDBC Driver (which can be downloaded from the official site in the help section) to GET and PUT data from one place to another with AES specification to encrypt and decrypt data along the way.


What is Flutter?

Flutter is a language used for Android and IOS Application development.

It was introduced by Google and is an open-source Application Development Platform.


Problem Connecting Flutter to Snowflake and Solution

Flutter Does not have any Direct Connection with snowflake, nor can we download Snowflake JDBC Driver and use the driver to connect with it directly.

Flutter Provides us the connection with third-party databases such as MySQL, Firebase, etc., but that could hinder data security.


So to Connect Flutter with Snowflake, we are using part of Native java, as JAVA directly connects with Snowflake using Snowflake JDBC Driver (that can be downloaded from the help section in Snowflake as shown below)



Steps To be Followed:

To make a seamless connection between Flutter and Snowflake, we have to do following things


1. Connect Native JAVA to Snowflake(Using JDBC Driver).

2. Connect Flutter to Native JAVA(Using CHANNEL Connection)


1. Connecting Native JAVA to Snowflake.


Step 1: Download Snowflake JDBC Driver from Snowflake help section.



Step 2: Add JDBC Driver in Project Structure of Application so that Application can access Snowflake JDBC Driver to make the connection between Snowflake and Application.



Step 3: Create a Class in Java and Import all the required Libraries for Database connection with a snowflake to retrieve, insert or update data.


import android.util.log;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Properties;


Step 4: Create Properties Variable as it maintains Key-Value Pair as seen in the below image. Here, we are creating key-value pairs that will be used to connect to Snowflake. User and Password are the credentials to connect to the snowflake. Also, here we can set the Role, Warehouse, Database and Schema that we want to use.


Properties prop= new Properties();

prop.put("user", "APP_USER");

prop.put("password", "********");

prop.put("account", "XXXXXXX.XXXXX.aws”);

prop.put("warehouse", "APP_USER_WAREHOUSE");

prop.put("role", "APP_ ROLE");

prop.put("db", "DEV_DB");

prop.put("schema", "USER_DETAIL_SCHEMA");


You can get the URL of Snowflake account as shown below:



Step 5: Load Snowflake JDBC Driver into Java using Class.forName


Class.forName ("net. snowflake.client.jdbc.SnowflakeDriver");


Step 6: Now, we will request a connection to Snowflake using DriverManager.getConnection and taking Snowflake URL String and Properties as defined Above as a Parameter. Snowflake URL String should be as shown below:

‘jdbc:snowflake://<account_name>.snowflakecomputing.com’


String url =

"jdbc: snowflake: //XXXXXXX.XXXXX.aws .snowflakecomputing.com/";

Connection con = DriverManage .getConnection(url,prop);


Step 7: Create Statement for executing Query on Specific Table present in Database and Schema as defined in Properties.


PreparedStatement st=con. prepareStatement (

"INSERT INTO DEV_DB.USER_DETAILS_SCHEMA.USER_DETAILS" +

“(USER_NAME, FIRST_NAME, LAST_NAME, LOGIN_PASSWORD, EMAIL_ID, FEEDBACK) VALUES(?,?,?,?,?,?)");


Step 8: Before Executing Query Alter Session to JSON format to make it work Correctly.


st.executeQuery ( "ALTER SESSION SET JDBC_QUERY_RESULT_FORMAT ='JSON'");

ResultSet rs=st executequery();


2. Connect Flutter to Native JAVA

Step 1: Create a Platform MethodChannel in Flutter and Similarly a Channel String in Native Java. This is used to let Application know that both of them work on the same things.


Flutter:

const platform = const MethodChannel ("com. flutter epic/epic");


Native Java:

Private static final String CHANNEL = “com. flutter epic/epic”;


Step 2: Now, in Flutter,

A. Create an Asynchronous Function to call Native Java from flutter.

B. To call Java, we also need to Define MethodChannel Call, which is checked in Java, and if it matches, Connect to a particular code in java and execute it.

[Also, we can send Parameter while calling Java Code as shown below]


// Function that Calls Native Java For Snowflake Connection

void ProfileCheck() async {

String? value;

try{

value=

await platform.invokeMethod("ProfileCheck",{“user”: user.toString()});

} catch (e){

print(e);

}

Result=value.toString();

print(value)

}


Step 3: Now in Java, Create a MethodChannel to handle all the MethodCall


new MethodChannel (getFlutterEngine(). getDartExecutor().

getBinaryMessenger(), CHANNEL)

.setMethodCallhandler (new MethodChannel.MethodCallHander() {

@Override

public void onMethodCall(@NonNull MethodCall call, @NonNull

MethodChannel.Result result){


Step 4: In Java, Check for MethodChannel Call. If it matches with what flutter sends for channel connection requests, then execute or perform a particular task like calling Snowflake, as explained earlier.


if (call.method .equals ("ProfileCheck")){


Step 5: Also, to call the Parameter to send by Flutter in java, we need to use call.argument function of MethodChannel


user= call.argument ( key: "user");


Step 6: Now, after executing all things in Java, we need to send back the result and access to flutter. For this, we use result.success() function of MethodChannel and send result as a Parameter to function.


result. success (Result);


Conclusion

A successful connection between Snowflake and its application is established. Now we can use any SQL command in the application in the same way as we do directly in the snowflake worksheet. Also, we can also set the warehouse, user, account, database, schema, role, etc., that we want to use in the application.


30 views0 comments

Recent Posts

See All