I am trying to read a Blob from a MySQL db, containing some random text.
My code looks like this:
Connection conn = DriverManager.getConnection(connection_string);
String message = "Some message";
byte[] messageBytes = message.getBytes("UTF-8");
Blob blob = conn.createBlob();
blob.setBytes(1l,messageBytes);
String sql = "INSERT INTO db.dbname('blob') VALUES ('" + blob + "');"
PreparedStatement pat = conn.prepareStatement(sql);
pat.executeUpdate();
In another class I have the code to read the blob field from database:
//some SQL code to read the blob field here
Blob readBlob = resultSet.getBlob("blob");
byte[] bytes = readBlob.getBytes(1, (int) readBlob.length());
String str = new String(bytes);
System.out.println(str);
The output is: com.mysql.cj.jdbc.Blob@3be81fc2
My code is exactly the same as some of the working solutions on StackOverflow, so I don't know exactly what is wrong.
executeUpdate()returns a count of the number of rows that were changed. In your case, the method should return 1 (one). Did it?