HudaTutorials.com

How to store byte array in MySQL using Java

Last updated on

Java byte Array to MySQL

Storing a byte array in MySQL

How to insert a byte array in to MySQL Database

How to add byte array to MySQL Database using Java JDBC

How to insert byte array into MySQL Table

How to pass byte array to MySQL

You can store byte array to MySQL in Java using Java JDBC. To save byte array into MySQL, equivalent data type is varbinary in MySQL. You can almost save any kind of data using varbinary datatype. You need a varbinary data type column in your table to store byte array.

How to store byte array in MySQL using Java Example ?

Following example showing how to store byte array in MySQL using Java. If you run the following example you need MySQL JDBC driver jar file mysql-connector-java-5.1.35-bin.jar in your classpath. If you want to know more about Java byte Array.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ByteArrayToMySQL
{
	public static void main(String args[])
	{
		String byte_array_data = "This is Byte Array to MySQL Example";
		byte[] byte_array = byte_array_data.getBytes();
		Connection sql_connection = null;
		PreparedStatement prepared_statement = null;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			sql_connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name", "yor_db_user", "your_db_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 MySQL");
				}
				else
				{
					throw new Exception("Problem occured during Save");
				}
			}
			else
			{
				System.out.println("MySQL 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("MySQL 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 SQL Server using Java.

Useful Tutorial for Store Java byte Array to MySQL

This Java byte array to MySQL tutorial is useful for the following.

  1. java save byte array to mysql
  2. java byte array to mysql database
  3. java byte array to mysql example
  4. java byte array to mysql connection
  5. java byte array to mysql java
  6. java byte array to mysql at localhost
  7. java byte array to mysql at 127.0.0.1