/*
 * AQUAFOLD CONFIDENTIAL
 * Unpublished Copyright (c) 2002-2020 AquaFold Incorporated, All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property of AquaFold Incorporated.  The intellectual
 * and technical concepts contained herein are proprietary to AquaFold Incorporated and may be covered by U.S.
 * and Foreign Patents, patents in process, and are protected by trade secret or copyright law. Dissemination of this
 * information or reproduction of this material is strictly forbidden unless prior written permission is obtained from
 * AquaFold Incorporated.
 *
 * The copyright notice above does not evidence any actual or intended publication or disclosure of this source code,
 * which includes  information that is confidential and/or proprietary, and is a trade secret, of AquaFold Incorporated.
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE, OR PUBLIC DISPLAY OF OR
 * THROUGH USE  OF THIS  SOURCE CODE  WITHOUT  THE EXPRESS WRITTEN CONSENT OF COMPANY IS
 * STRICTLY PROHIBITED, AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.  THE
 * RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR
 * IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE,
 * OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 */

package standalone;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created by abdullah on 1/22/20.
 */
public class TestBlob {
	public static void main(String[] args) {
		Connection con = null;
		try {
			com.microsoft.sqlserver.jdbc.SQLServerDataSource ds
					= new com.microsoft.sqlserver.jdbc.SQLServerDataSource();
			ds.setServerName("172.24.1.145");
			ds.setPortNumber(1433);
			ds.setDatabaseName("AdventureWorks2014");
			ds.setUser("sa");
			ds.setPassword("Am@z*n7");
			con = ds.getConnection();

			// Deleting the record for re-testing
			String name = "TEST-INSERT";
			Statement sta = con.createStatement();
			sta.executeUpdate("DELETE FROM dbo.test_blob WHERE name = '"
					+ name + "'");

			// Inserting BLOB value with a regular insert statement
			sta = con.createStatement();
			String s = "0xC9CBBBCCCEB9C8CABCCCCEB9C9CBBB"; //SQL Server format
			//String s = "x'C9CBBBCCCEB9C8CABCCCCEB9C9CBBB')"); // MySQL format
			int count = sta.executeUpdate(
					"INSERT INTO dbo.test_blob"
							+ " (name, baddress)"
							+ " VALUES ('" + name + "', "
							+ s
							+ ")");

			// Retrieving BLOB value with getBytes()
			ResultSet res = sta.executeQuery(
					"SELECT * FROM dbo.test_blob WHERE name = '" + name + "'");
			res.next();
			System.out.println("The inserted record: ");
			System.out.println("   Name = " + res.getString("name"));
			System.out.println("   Blob = "
					+ new String(res.getBytes("baddress")));
			res.close();
			sta.close();

			con.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}