🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #702

Guardar Blob en Base de datos MySql 5

#702
From: Guevonaso
Date: Thu, 18 Jan 2007 10:50
68 lines
3127 bytes
Hola a todos.

Bueno, tengo un problema. Intento guardar un fichero binario en base de
datos MySql 5 pero me da un error de sintaxis al ejecutar la consulta
siguiente:

// content es un InputStream

String sql = "UPDATE datos " +
	" SET imagen= ? " +
                 " WHERE id=  ?";
PreparedStatement stm = conex.prepareStatement(sql);
stm.setBinaryStream(1, content, content.available());
stm.setInt(2, codResidente);
int cont = stm.executeUpdate(sql);
stm.close();


Me da un error de sintaxis (que no es tal). Deduzco que el problema es
del preparedStatement del driver.
El caso es que no funciona.

He encontrado una solucion, pero no me gusta por lo farragoso que
resulta. ¿Alguna otra idea?

La solucion que he encontrado ha sido mas o menos esta:

String sql = "SELECT imagen FROM datos WHERE id = ?";
String UPDATE_1 =  " UPDATE  datos SET imagen='X' WHERE id = " + cod;
String UPDATE_2 =  " UPDATE  datosSET imagen=? WHERE id =  ?";

PreparedStatement stm = conex.prepareStatement(sql);
stm.setInt(1, codResidente);
ResultSet rs = stm.executeQuery();
Blob dato;
if (rs.next()) {
  dato = rs.getBlob(1);
  if (dato == null) {
     rs.close();
     stm.close();
      Statement stm2 = conex.createStatement();
      stm2.executeUpdate(UPDATE_1);
      stm2.close();
      stm = conex.prepareStatement(sql);
      stm.setInt(1, codResidente);
      rs = stm.executeQuery();
      rs.next ();
      dato = rs.getBlob(1);
      stm.close();
  }

   int length;

   byte[] buffer = new byte[1024];
   int cont = 1;
   while ((length = content.read(buffer,0, 1024)) != -1) {
      log.debug("Iteracion " + cont);
      dato.setBytes(cont,buffer, 0, length);
      cont = cont + 1024;
   }
   stm = conex.prepareStatement(UPDATE_2);
   stm.setBlob(1, dato);
   stm.executeUpdate();
   stm.close();
}


Funcionar funciona, pero obviamente no es la mejor solucion.
Technical Details
Message-ID: <1169117450.903070.325000@38g2000cwa.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!38g2000cwa.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 1116f3,f4e21fe5ae79fc18,start
X-Google-Attributes: gid1116f3,public
X-Google-Language: ENGLISH,ASCII
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!38g2000cwa.googlegroups.com!not-for-mail
From: "Guevonaso" <fernando.rosado@gmail.com>
Newsgroups: es.comp.lenguajes.java
Subject: Guardar Blob en Base de datos MySql 5
Date: 18 Jan 2007 02:50:50 -0800
Organization: http://groups.google.com
Lines: 69
Message-ID: <1169117450.903070.325000@38g2000cwa.googlegroups.com>
NNTP-Posting-Host: 80.34.137.78
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1169117457 22269 127.0.0.1 (18 Jan 2007 10:50:57 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 18 Jan 2007 10:50:57 +0000 (UTC)
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: 38g2000cwa.googlegroups.com; posting-host=80.34.137.78;
   posting-account=WICUow0AAAC-EsXMMMKn5jFCqzRetOQB