Note that there are some explanatory texts on larger screens.

plurals
  1. POCan we fetch an added image from MySql database by using java?
    primarykey
    data
    text
    <p>I have added an image into a MySql database. And I am trying to fetch the image from the database by creating a new file by using java. The problem is that everything goes fine and image file has been created but the image file doesn't contain the image, it has just nothing and the size of the new created image file differs from the original one..... The Sql Code:</p> <pre><code>CREATE TABLE `pictures` ( `id` int(11) NOT NULL auto_increment, `description` varchar(20) default NULL, `photo` blob, PRIMARY KEY (`id`) ); insert into pictures values('1','pic1','D:\java.jpg'); </code></pre> <p>And the java code:</p> <pre><code>public class ReadBlobPicture { static String url = "jdbc:mysql://localhost:3306/imgdatabase"; static String username = "root"; static String password = "tiger"; public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, username, password); String sql = "SELECT photo FROM pictures where id=1"; Statement stmt = conn.prepareStatement(sql); ResultSet resultSet = stmt.executeQuery(sql); Object obj = resultSet; while (resultSet.next()) { File image = new File("E:\\java12.jpg"); FileOutputStream fos = new FileOutputStream(image); System.out.println(resultSet.getString(1)); byte[] buffer = new byte[1]; InputStream is2 = resultSet.getBinaryStream(1); System.out.println(is2.available()); while (is2.read() &gt; 0) { fos.write(buffer); fos.flush(); } fos.close(); } conn.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload