|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Java and MySQL Blob reading/writing
I am an inexperienced programmer that is having to come to terms with programming in Java interfacing with a MySQL db.
I need to know - and examples would be good - how I can get a .doc or .pdf stored as a blob and then read back out to the relevant format. I have tried reading a file in char by char and using .setBytes but to no avail. I think I need to attach some kind of reader to the blob but I am unsure how to proceed. Thanks in advance. M. |
|
#2
|
|||
|
|||
|
hi m,
im not an expert at java by any stretch of the imagination, but i have previously dealt with this issue in PHP. My advice would be to try not to do it this way. Efficiency wise you are far better of transfering the pdf or doc into a folder in ur hosting account that has the appropriate permissions set- and then saving a link to it in the database. Hope this helps- if not then i'm sure there's someone here that will know how to help you what the ![]() |
|
#3
|
|||
|
|||
|
Thanks 'what the',
Unfortunately I am going for a solely Java and MySQL application where the user/administrator does not have to worry about file permissions. Hence the use of blobs. In the program, only the administrator can insert and update and the users can only select - therefore no file permissions are needed, nice and simple in theory. M. |
|
#4
|
|||
|
|||
|
For anyone else that might be able to help, I have been messing around with this code.
It compiles but exceptions when it is called. Any ideas? THE CODE: public void importToBlob() { BufferedInputStream in = null; //declare buffered reader OutputStream out = null; long length; final int EOF = -1; int c; File original = new File("c:\\original.txt"); File copy = new File("c:\\copy.txt"); Blob testBlob = null; rs = runSelect("select document from docs where docid = 13"); try { if (rs.next()) { testBlob = rs.getBlob(1); FileInputStream reader = new FileInputStream(original); FileWriter writer = new FileWriter(copy); /** while((c = reader.read()) !=EOF) { writer.write(c); }*/ out = testBlob.setBinaryStream(0); byte[] buffer = new byte[(int)original.length()]; while ((length = reader.read(buffer)) != -1) { out.write(buffer); } System.out.println(testBlob.toString()); reader.close(); writer.close(); System.out.println("Copied \"original.txt\" to \"copy.txt\""); } } catch(FileNotFoundException fnfe){System.out.println("File not found: " +fnfe.getMessage());} catch(IOException ioe){System.out.println("IOException: " +ioe.getMessage());} catch(java.sql.SQLException e){System.out.println("SQL exception: " +e.getMessage());} } THE ERROR: java.lang.AbstractMethodError: com.mysql.jdbc.jdbc2.Blob.setBinaryStream(J)Ljava/io/OutputStream; at ReportsUI.importToBlob(ReportsUI.java:500) at ReportsUI$ButtonHandler.actionPerformed(ReportsUI.java:457) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245) at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:227) at java.awt.Component.processMouseEvent(Component.java:5093) at java.awt.Component.processEvent(Component.java:4890) at java.awt.Container.processEvent(Container.java:1566) at java.awt.Component.dispatchEventImpl(Component.java:3598) at java.awt.Container.dispatchEventImpl(Container.java:1623) at java.awt.Component.dispatchEvent(Component.java:3439) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095) at java.awt.Container.dispatchEventImpl(Container.java:1609) at java.awt.Window.dispatchEventImpl(Window.java:1585) at java.awt.Component.dispatchEvent(Component.java:3439) at java.awt.EventQueue.dispatchEvent(EventQueue.java:450) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136) at java.awt.EventDispatchThread.run(EventDispatchThread.java:99) |
|
#5
|
|||
|
|||
|
Looking at the api docs, there is no "setBinaryStream(int)" method for the Blob interface. There is a "getBinaryStream()". Is that what you are trying to get? It returns an inputStream so you can read the blob in, which makes sense since you are getting a blob from a resultset. An outputstream would be writing TO the blob you got from the resultset. I dont think that is what you want to do.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Java and MySQL Blob reading/writing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|