Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You read from the stream, but never save the read character into the buffer...</p> <pre><code>while (is2.read() &gt; 0) { fos.write(buffer); fos.flush(); } </code></pre> <p>something like:</p> <pre><code>int x; while((x = is2.read()) != -1) { fos.write(x); } fos.flush(); </code></pre> <p>(edit - from the comment on another answer...)</p> <p>For the insert statement not inserting the image see <a href="http://www.java2s.com/Code/Java/Database-SQL-JDBC/InsertpicturetoMySQL.htm" rel="nofollow noreferrer">this link</a></p> <pre><code>/* Defining the Table: Oracle and MySql create table MyPictures ( id INT PRIMARY KEY, name VARCHAR(0), photo BLOB ); */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertPictureToMySql { public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null; PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("myPhoto.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setString(1, "001"); ps.setString(2, "name"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } } } </code></pre>
 

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