Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 17th, 2003, 03:04 PM
mwarford mwarford is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: U.K.
Posts: 15 mwarford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old February 17th, 2003, 08:21 PM
what the what the is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 1 what the User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #3  
Old February 18th, 2003, 01:53 AM
mwarford mwarford is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: U.K.
Posts: 15 mwarford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old February 18th, 2003, 03:09 PM
mwarford mwarford is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: U.K.
Posts: 15 mwarford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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)

Reply With Quote
  #5  
Old February 19th, 2003, 10:38 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Java and MySQL Blob reading/writing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway