Insert binary data
You can use one of two methods to insert binary data with
the binary data types: an SQL INSERT statement that uses the ASCII
representation of the binary data type or an SQL INSERT statement
from a Java™ or C program that
treats the column as a byte stream. For example, given the following
table:
CREATE TABLE network_table (
mac_address binaryvar NOT NULL,
device_name varchar(128),
device_location varchar(128),
device_ip_address binaryvar,
date_purchased date,
last_serviced date)
Using an SQL INSERT statement
that uses the ASCII representation of the binaryvar or binary18 column:
INSERT INTO network_table VALUES ( '000012DF4F6C', 'Network Router 1',
'Basement', 'C0A80042', '01/01/2001', '01/01/2006');
Using
an SQL INSERT statement from a Java program
that treats the column as a byte stream, such as the JDBC setBytes() method:
String binsqlstmt = "INSERT INTO network_table (mac_address, device_name,
device_location, device_ip_address) VALUES ( ?, ?, ?, ? );
PreparedStatement stmt = null;
byte[] maddr = new byte[6];
byte[] ipaddr = new byte[4];
try
{
stmt = conn.prepareStatement(binsqlstmt);
maddr[0] = 0;
maddr[1] = 0;
maddr[2] = 18;
maddr[3] = -33;
maddr[4] = 79;
maddr[5] = 108;
stmt.setBytes(1, maddr);
stmt.setString(2, "Network Router 1");
stmt.setString(3, "Basement");
ipaddr[0] = -64;
ipaddr[1] = -88;
ipaddr[2] = 0;
ipaddr[3] = 66;
stmt.setBytes(4,ipaddr);
stmt.executeUpdate();
stmt.close()
}
catch
{
System.out.println("Exception: " + e);
e.printStackTrace(System.out);
throw e;
}