Sagewire Logo

Insert Blob Example

2 Message(s) by 2 Author(s) originally posted in mysql java


From: rhino1 Date:   Saturday, July 26, 2003
Can anyone sent me or point me to an example of a program that inserts a =
file like a JPEG or GIF into a Blob column of a MySQL table via a JAVA =
program?

I've several years of experience with DB2 and can do this task in a =
JAVA program that talks to DB2 but the same program, modified to insert =
data into a MySQL table, does not seem to be working. There are no error =
messages but when I look at the table row, instead of a long string of =
hex digits, I get a few characters, like =FF=D8=FF=E0, and nothing else. =
That makes me believe that my insert failed for some reason. If I look =
at the DB2 table after I insert the same Blob, I get a long string of =
hex digits like x'A9B4C3' .... etc. etc.

Can anyone help me with a working example of inserting a Blob? That =
should help me figure out what needs to be done differently for MySQL =
than DB2. Or'd you prefer that I post my code so that you can help =
me figure out what I'm doing incorrectly?

Rhino
---
rhino1 AT sympatico DOT ca
"If you want the best seat in the house, you will have to move the cat."


From: Mark Date:   Monday, August 04, 2003
I developed this sample a few days ago, and just noticed your question.
I've code that reads the BLOB back out of MySQL and puts it back
into a gif file, if you are interested.

Mark// Reads an external binary file (gif) into a byte array , and then
// load s it into a MySQL table as a BLOB.

import JAVA.io.*;
import JAVA.util.*;
import JAVA.sql.*;

public class InsertBlob2
{
// Variables...
static byte[] blobByteBuffer;

// main() to start
public static void main(String[] args)
{
// First, load in the external GIF file into a byte array.
// Check for proper number of arguments, con tinue if OK.
if (args.length != 1)
{
System.err.println("Usage: JAVA InsertBlob2 <source file>");
}
else
{
try
{
copy(args[0]);
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
// Next, load the byte array into the database
load();
}

// Copy the external file into a byte array...
public static void copy(String from_name) throws IOException
{
// Create File objects...
File from_file = new File(from_name);

// Does source file exist, is it a file, is it readable?
if (!from_file.exists())
{
abort("InsertBlob2: no such source file: " + from_name);
}
if (!from_file.isFile())
{
abort("InsertBlob2: can not copy directory: " + from_name);
}
if (!from_file.canRead())
{
abort("InsertBlob2: source file is unreadable: " + from_name);
}
long size = from_file.length();
if (!(size > 0l))
{
abort("InsertBlob2: source file is empty: " + from_name);
}

// If you got here, all is OK.
blobByteBuffer = new byte[(int)size];

FileInputStream from = null; // Stream to read binary stuff
from source.
try
{
from = new FileInputStream(from_file);
System.out.println("Reading " + from_name + " into byte
array...");
from.read(blobByteBuffer);
}
catch (Exception e)
{
abort("InsertBlob2: problem reading source file: " +
from_name);
}

finally // always close streams, even if exceptions...
{
if (from != null)
try
{
from.close();
}
catch (IOException e)
{
;
}

}
System.out.println("Done reading.");
}
public static void load()
{
System.out.println("Getting ready to insert blob...");
Connection con = null;
PreparedStatement pstmt = null;
String url = "jdbc:mysql:///accounting?user=root&password=pswd";
String sql = "INSERT INTO blob_table SET binary_stuff = ?";

try
{
Class.forName("com.mysql.jdbc.Driver"); // may throw
exception

con = DriverManager.getConnection(url); // may throw
exception
pstmt = con.prepareStatement(sql); // may throw
exception

// Load the blob...
pstmt.setBytes(1, blobByteBuffer); // may
throw exception

pstmt.executeUpdate(); // may throw
exception
System.out.println("Blob inserted!");
}

catch (Exception e)
{
System.out.println("InsertBlob2: problem inserting data: "
+ e.getMessage());
System.exit(0);
}

finally
{
if (con != null)
{
try
{
con.close();
pstmt.close();
}
catch (Exception e)
{
System.out.println("InsertBlob2: problem closing
SQL objects: " + e.getMessage());
System.exit(0);
}
}
}
}

// Static convenience method...
private static void abort(String msg) throws IOException
{
throw new IOException(msg);
}
}






Blogs related to Insert Blob Example

Introducing ErlSQL: Easy Expression and Generation of SQL ...
Here are some examples of ESQL expressions and their corresponding SQL queries: ... "INSERT INTO project(foo,baz) VALUES (5,'bob')" {insert,project,[foo,bar ... {update,project,[{started_on,{2000,21,3}}],{name,like,"blob"}} -> "UPDATE ...

Change detected in page MySQL 5.0 Reference Manual :: 13.1.2 ALTER ...
Pending INSERT DELAYED statements are lost if a table is write locked and ALTER TABLE is ... ALTER TABLE t1 CHANGE c1 c1 BLOB; ALTER TABLE t1 CHANGE c1 c1 TEXT ... Simple example to add a field to a table mysql> alter table cdrom add ...

RE: RE: Escaping PHP in an SQL BLOB of text
I have a MySql data base with large "BLOB"s of text. within those BLOBs i'd like to add php code to insert variables/values (grab - if you will) - either via url or form post. I control the database and will have no need to update it ...

NDB API and blobs: some example
I spend some time on this last night to figure it out with MySQL Cluster NDB API. ... here is the silly code inserting a record into a table with a BLOB field. ... We insert a new record oper->insertTuple(); // Even though the record ...

What Is MySQL?
DELETE, INSERT, REPLACE, and UPDATE return the number of rows that were changed (affected). ... An index may use a prefix of a column for CHAR, VARCHAR, BLOB, ... For example, you can use MS Access to connect to your MySQL server. ...

Mysql 学习
mysql>revoke insert,update,delete on test.* from test@"%" ... or BDB 的表格 上,可以在BLOB TEXT 上创建索引只有在MyISAM 类型表格上,可以在CHAR, VARCHAR, ... Following mysqldump import example for InnoDB tables is at least 100x faster than ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional