0

I am trying to save an image to PostgreSQL with byte data type. I am using JSP to convert image -> bytes according to the documentation. All syntax in Java is correct but when I execute

ps.executeUpdate();

PostgreSQL returns

syntax error at or near "\" at character 823

at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:94) at org.postgresql.Connection.ExecSQL(Connection.java:398) at org.postgresql.jdbc2.Statement.execute(Statement.java:130) at org.postgresql.jdbc2.Statement.executeUpdate(Statement.java:73) at org.postgresql.jdbc2.PreparedStatement.executeUpdate(PreparedStatement.java:113) at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) at obj.util.Imagen.addImage(Imagen.java:86) at obj.util.FileUploadHandler.doPost(FileUploadHandler.java:44) at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

My code is:

  File img = new File(UPLOAD_DIRECTORY + File.separator + name);
  byte [] byteImage = ImageToByte(img);
  //Conexion
  java.sql.Connection conDot11q     = null;

  Conectar c                        = new Conectar();
  conDot11q                         = c.conDot11qPostgres();
  addImage(byteImage, conDot11q);

And the methods:

public static byte [] ImageToByte(File file) throws FileNotFoundException{
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);      
           // System.out.println("read " + readNum + " bytes,");
        }
    } catch (IOException ex) {
    }
    byte[] bytes = bos.toByteArray();

 return bytes; 
}

public void addImage(byte[] img, Connection conn) {
    try {

        PreparedStatement ps = null;
        ps = conn.prepareStatement("INSERT INTO IMAGENES (IMAGEN, DESCRIPCION) VALUES (?, ?)");
        ps.setBytes(1, img);
        ps.setString(2, "Test");

        ps.executeUpdate();
        ps.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            conn.close();

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

}

I have tried everything without success. Thanks for the help!

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
  • you said you have tried everything. Could you elaborate on this? – Arnaud Potier May 20 '15 at 14:08
  • examples: [save images - postgres](http://jeebestpractices.blogspot.mx/2011/03/save-images-into-database-postgres-with.html), [java converting an image to an input stream](http://stackoverflow.com/questions/4853910/java-converting-an-image-to-an-input-stream-without-creating-a-file), [image from postgresql bytea](http://stackoverflow.com/questions/6078525/displaying-image-from-postgresql-database-bytea), [webdocs](http://webdocs.cs.ualberta.ca/~yuan/servlets/UploadImage.java), [guardar y leer imagenes en postgressql](http://www.jc-mouse.net/base-de-datos/guardar-y-leer-imagenes-en-postgressql) – Angiello Rivas May 20 '15 at 14:32

0 Answers0