Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>a) What type of field should I put in the database? (e.g VARCHAR)</p> </blockquote> <p>An image is binary data. Just use a binary field. In PostgreSQL it's <a href="http://www.postgresql.org/docs/8.4/static/datatype-binary.html" rel="nofollow noreferrer"><code>bytea</code></a>.</p> <blockquote> <p>b) What type of object should I use for storing and manipulating the image (at an object layer)? (e.g java.awt.Image)</p> </blockquote> <p>Use an <a href="http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html" rel="nofollow noreferrer"><code>InputStream</code></a> or <code>byte[]</code> to store it. The Java 2D API has classes/methods which can take/return either of those types.</p> <blockquote> <p>c) How do I create an object of the type selected (answer of question b) from the data obtained from the database?</p> </blockquote> <p>Use <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getBinaryStream%28java.lang.String%29" rel="nofollow noreferrer"><code>ResultSet#getBinaryStream()</code></a> to get an <code>InputStream</code> of it or <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getBytes%28java.lang.String%29" rel="nofollow noreferrer"><code>ResultSet#getBytes()</code></a> to get a <code>byte[]</code> of it.</p> <blockquote> <p>d) How do I save an object of the type selected (answer of question b) to the database?</p> </blockquote> <p>Use <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBinaryStream%28int,%20java.io.InputStream%29" rel="nofollow noreferrer"><code>PreparedStatement#setBinaryStream()</code></a> or <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBytes%28int,%20byte[]%29" rel="nofollow noreferrer"><code>PreparedStatement#setBytes()</code></a>. Note: if you're new to JDBC/<code>PreparedStatement</code> as well, then I recommend you to get yourself through the <a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html" rel="nofollow noreferrer">Sun basic JDBC tutorial</a>.</p> <blockquote> <p>e) How do I draw the image on a web page?</p> </blockquote> <p>Use a HTML <code>&lt;img&gt;</code> element whose <code>src</code> points to an URL matching the <code>url-pattern</code> of a <code>Servlet</code>. You can pass the image identifier as request parameter or pathinfo. Inside the <code>Servlet</code> you just write the obtained <code>InputStream</code> to the <code>OutputStream</code> of the response the usual Java IO way. You can find a code example in <a href="https://stackoverflow.com/questions/2340406/retrieve-multiple-images-from-mysql/2341322#2341322">this answer</a> I posted some time ago.</p> <hr> <p>That said, storing <em>only</em> raw images in a database is generally not a good idea. You should preferably also store some metadata along it, such as the <a href="http://www.iana.org/assignments/media-types" rel="nofollow noreferrer">content type</a> (e.g. <code>image/jpeg</code>, <code>image/gif</code>, etc) which you after all need in the response header so that the browser knows what to do with it.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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