Bonjour
j'ai stocké dans un JLabel une image, ou plus précisément une instance d' "Icon".
je voudrais transformer cet objet (l'icon) en blob afin de l'écrire en base de données.
voici mon code, récupéré de plusieurs site internet; je ne comprends pas trop ce que cela fait, je l'avoue mais je n'ai pas envie de comprendre cette partie plus avant; je ne veux pas perdre du temps sur une chose que je pensais simple.
donc, voici mon code :
et ça ne marche pas; voici l'erreur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 //on prepare le blob Blob blob = fen_generale.cnx.createBlob(); // ObjectOutputStream oos; // oos = new ObjectOutputStream(blob.setBinaryStream(1)); // oos.writeObject((ImageIcon) lab_image.getIcon()); // oos.close(); byte[] imgByte = null; Image img=iconToImage2(lab_image.getIcon()); try { int imgWidth = img.getWidth(null); int imgHeight = img.getHeight(null); int imgType = BufferedImage.TYPE_INT_ARGB; // you can experiment with this one BufferedImage bi = new BufferedImage(imgWidth, imgHeight, imgType); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, 0, 0, null); g2.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (ImageIO.write(bi, "JPEG", baos)) { imgByte = baos.toByteArray(); } } catch (IOException ex) { ex.printStackTrace(); } blob.setBytes(1, imgByte);
voici la ligne d'insertion en base :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 java.sql.SQLException: Java exception: ': java.lang.NullPointerException'. at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.javaException(Unknown Source) at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source) at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown Source) at org.apache.derby.impl.jdbc.EmbedPreparedStatement.execute(Unknown Source) at gui.fen_recherche$5.actionPerformed(fen_recherche.java:552) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.sql.SQLException: Java exception: ': java.lang.NullPointerException'. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 47 more Caused by: java.lang.NullPointerException at org.apache.derby.impl.jdbc.LOBStreamControl.isValidPostion(Unknown Source) at org.apache.derby.impl.jdbc.LOBStreamControl.read(Unknown Source) at org.apache.derby.impl.jdbc.LOBInputStream.read(Unknown Source) at java.io.FilterInputStream.read(Unknown Source) at org.apache.derby.iapi.services.io.LimitInputStream.read(Unknown Source) at org.apache.derby.iapi.types.RawToBinaryFormatStream.read(Unknown Source) at org.apache.derby.impl.store.raw.data.MemByteHolder.write(Unknown Source) at org.apache.derby.impl.store.raw.data.RememberBytesInputStream.fillBuf(Unknown Source) at org.apache.derby.impl.store.raw.data.StoredPage.logColumn(Unknown Source) at org.apache.derby.impl.store.raw.data.StoredPage.logRow(Unknown Source) at org.apache.derby.impl.store.raw.data.InsertOperation.writeOptionalDataToBuffer(Unknown Source) at org.apache.derby.impl.store.raw.data.InsertOperation.<init>(Unknown Source) at org.apache.derby.impl.store.raw.data.LoggableActions.actionInsert(Unknown Source) at org.apache.derby.impl.store.raw.data.BasePage.insertNoOverflow(Unknown Source) at org.apache.derby.impl.store.raw.data.BasePage.insertAtSlot(Unknown Source) at org.apache.derby.impl.store.raw.data.StoredPage.insertAtSlot(Unknown Source) at org.apache.derby.impl.store.raw.data.BasePage.insert(Unknown Source) at org.apache.derby.impl.store.access.heap.HeapController.doInsert(Unknown Source) at org.apache.derby.impl.store.access.heap.HeapController.insertAndFetchLocation(Unknown Source) at org.apache.derby.impl.sql.execute.RowChangerImpl.insertRow(Unknown Source) at org.apache.derby.impl.sql.execute.InsertResultSet.normalInsertCore(Unknown Source) at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown Source) at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source) at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
(stmt2 est un preparedStatement)
Code : Sélectionner tout - Visualiser dans une fenêtre à part stmt2.setBlob(8, blob);
ça ne vient pas du code de la base car si je remplace blob par une classe anonyme de type Blob (avec donc toutes les méthodes nécessaires redéfinies) j'arrive à exécuter ma requête sans erreur (mais sans blob!)
auriez-vous un code simple?
olivier
ps: ou la marche à suivre, je veux dire les différentes étapes (icon -> image -> .... par ex).
Partager