Inserting Binary data into MySQL (without PreparedStatement's)

前端 未结 3 1298
猫巷女王i
猫巷女王i 2021-01-12 16:05

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

相关标签:
3条回答
  • 2021-01-12 16:21

    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.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-12 16:36

    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

    0 讨论(0)
提交回复
热议问题