How to store byte array in SQL Server using Java

Last updated on

Java byte Array to SQL Server

Storing a byte array in MS-SQL Server

How to insert a byte array in to Microsoft SQL Server Database

How to add byte array to SQL Database using Java JDBC

How to insert byte array into SQL Table

How to pass byte array to SQL Server

You can store byte array to SQL Server in Java using Java JDBC. To save byte array into sql server, equivalent data type is varbinary in SQL Server. You can almost save any kind of data using varbinary datatype and even though we have image datatype, varbinary is the recommended datatype to store image in sql server instead of image datatype. You need a varbinary data type column in your table to store byte array.

Following example showing how to store byte array in SQL Server using Java. If you run the following example you need sql server JDBC driver in your classpath. If you want to know more about Java byte Array.

How to store byte array in SQL Server using Java ?

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ByteArrayToSQLServer
{
  public static void main(String args[])
  {
    String byte_array_data = "This is Byte Array to SQL Server Example";
    byte[] byte_array = byte_array_data.getBytes();
    Connection sql_connection = null;
    PreparedStatement prepared_statement = null;
    try
    {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      sql_connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=your_database_name;user=sa;password=your_user_password");
      if(sql_connection != null)
      {
        sql_connection.setAutoCommit(false);
        prepared_statement = sql_connection.prepareStatement("INSERT INTO your_byte_array_table(your_byte_array_column) VALUES(?)");
        prepared_statement.setBytes(1, byte_array);
        if (prepared_statement.executeUpdate() == 1)
        {
          sql_connection.commit();
          System.out.println("Byte Array Stored Successfully in SQL Server");
        }
        else
        {
          throw new Exception("Problem occured during Save");
        }
      }
      else
      {
        System.out.println("SQL Server Database Connection Failed");
      }
    }
    catch(Exception e)
    {
      if (sql_connection != null)
      {
        try
        {
          sql_connection.rollback();
        }
        catch(Exception rollback_err)
        {
          System.out.println("Roll Back Error Occured : " + rollback_err.getMessage());
        }
      }
      System.out.println("Error Occured : " + e.getMessage());
    }
    finally
    {
      try
      {
        if (prepared_statement != null)
        {
          prepared_statement.close();
          prepared_statement = null;
        }
        if (sql_connection != null)
        {
          if (!sql_connection.isClosed())
          {
            if (!sql_connection.getAutoCommit())
            {
              sql_connection.setAutoCommit(true);
            }
            sql_connection.close();
          }
          sql_connection = null;
        }
      }
      catch(Exception ex)
      {
        System.out.println("SQL Server Database Connection Close Error");
      }
    }
  }
}

In the above example in the connection string you can use 127.0.0.1 against localhost or your domain name. You can also learn similar tutorial How to store byte array in MySQL using Java.

Share This Page On :

More Related Tutorials