cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Databricks JDBC Insert into Array field

Dp15
Contributor

hi,

 I am trying to insert some data into a databricks table which has Array<String> fields (field1 & field2). I am using JDBC for the connection and my POJO class looks like this 

public class A{
private Long id;
private String[] field1;
private String[] field2;
}

I am using 

jdbcTemplate.batchUpdate(String sql, final List<Object[]> batchArgs, final int[] argTypes)

where I have a list of Objects for class A.

the argtype code for the fields, field1 & field2 are 2003 since I am using the java.sql.Type.Arrray for these fields.

While executing, I am getting the following error
[Databricks][JDBC](11500) Given type does not match given object: [Ljava.lang.String;@3e1346b0.

Should I be using some other code for these fields?

How do I insert data into a Array field using JDBCTemplate

1 REPLY 1

VZLA
Databricks Employee
Databricks Employee

The error you're encountering, [Databricks][JDBC](11500) Given type does not match given object: [Ljava.lang.String;@3e1346b0, indicates that the JDBC driver is not recognizing the Java String[] array as a valid SQL array type. This is a common issue when working with array types in JDBC.

To resolve this, you need to ensure that the array is properly converted to a SQL array type before passing it to the jdbcTemplate.batchUpdate method. Here are the steps you can follow:

  1. Convert Java Array to SQL Array: Use the Connection.createArrayOf method to convert your Java String[] array to a SQL array.

  2. Modify Your Code: Update your code to create SQL arrays for field1 and field2 before executing the batch update

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;

public class YourClass {
    private JdbcTemplate jdbcTemplate;

    public void insertData(List<A> dataList) throws SQLException {
        String sql = "INSERT INTO your_table (id, field1, field2) VALUES (?, ?, ?)";
        jdbcTemplate.batchUpdate(sql, dataList, dataList.size(), (PreparedStatement ps, A data) -> {
            ps.setLong(1, data.getId());
            Connection conn = ps.getConnection();
            ps.setArray(2, conn.createArrayOf("VARCHAR", data.getField1()));
            ps.setArray(3, conn.createArrayOf("VARCHAR", data.getField2()));
        });
    }
}

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group