Can't insert byte[] into MySQL using java

前端 未结 3 749
夕颜
夕颜 2021-01-13 08:59

Following is the code I have used:

byte[] bkey = key.getEncoded();
String query = \"INSERT INTO keytable (name, key) VALUES (?,?)\";
PreparedStatement pstmt          


        
3条回答
  •  旧时难觅i
    2021-01-13 09:39

    Try using "setBinaryStream()" instead of "setBytes()", and pass it a ByteArrayInputStream constructed on your byte array. This, of course, assumes that the data type assigned to the column can store bytes... Make sure it is a BLOB, BINARY, or VARBINARY.

    Also, use backticks to enclose your objects. "key" is a SQL keyword, and other than that it is just a good habit:

    String query = "INSERT INTO `keytable` (`name`, `key`) VALUES (?,?)";
    

提交回复
热议问题