I\'m trying to insert some Binary data into a MySQL database without using prepared statements. The reason for this is that I concatenate thousands of statements into a sing
A prepared statement is undoubtedly the fastest approach. The reason that you find it too slow might be because you are not using it inside a transaction. You might be able to do something cute with base 64, but it would be very slow.
Found the solution .... Although not something I saw documented anywhere .....
You can insert Binary data directly by writing the bytes converted to HEX and preceeded by 0x
For example:
INSERT INTO my_table VALUES (1,0x19c0300dc90e7cedf64703ed8ae8683b,2);
Have you tried to use PreparedStatement in Batch mode?
PreparedStatement pStmt = ...;
while(...) { // use for or whatever loop
pStmt.clearParameters();
pStmt.setBinaryStream(2, ...);
pStmt.addBatch();
}
pStmt.executeBatch();
For more detailed information on how you can make Batches efficient with JDBC and MySQL have a look here: MySQL and JDBC with rewriteBatchedStatements=true