Solved

Inserting a blob object into SQL Server thru JDBC: 2

Posted on 2000-05-08
3
374 Views
Last Modified: 2013-11-24
This a a regenerated question sorry about the points i would raise it to 200 but at the moment i heve only 30  

james

We are developing an email system
we r using JavaMail API for sending a mail thru SMTP client

Before sending the mails we want to store it in a SQL Sever table.
we r using javax.mail.internet.MimeMessage object
to store the message we want to stote this MimeMessage object into the database into a coloumn for msg body

We r using PreparedStatement for this
we've tried
PreparedStatement.setObject()
PreparedStatement.setBinaryStream()
for inserting the message body into the table, but did'nt succeed.

This is our  code
         pstmt = conSQLServer.prepareStatement("Insert into abc values(1, ?)");

instream = mimeMsg.getInputStream();
pstmt.setBinaryStream(1,instream,instream.available());
            or  
 pstmt.setObject(1,mimeMsg);
 i = pstmt.execute();

Just tell us wheather this is a right approch or not  ???
and any other method we can use for this

Please Help
Thanks in advance
James





Using stream is the right way.

I have several questions:

- what does "didn't succeed" mean? Exception thrown, result not there...
- what JDBC driver are you using?
- SQL server is this the MS SQL server?

Some ideas to continue:

1. Comment the MimeObject stuff and try to write as a stream a byte array for example, using ByteArrayInputStream.

2. If 1 works, the problem is with the handling of the MimeObject serialization.

3. If it does not work, the problem is in your statement or server(?), since I have done stream updates and they normally work.

4. try changing the execute() to executeUpdate()

Cheers,
  Nik
Hi
1.Does your message body conatin any attachments.
2. Are you taking the message body (only text)from any GUI

if you are taking only text you can convert that into string and store that one into varchar2 or some thing like that.
  tell me if there are any attachments in the message body.


What type of input stream you are using ? If you are using FileInputStream try putting this lines in your code :-

File f = new  File();
PrepareStatement dbstmt = con.preparestatement();
InputStream is = new FileInputStream(f);
dbstmt.setBinaryStream(int i, is, (int)f.length());

Good Luck.

Have you tried using the setBytes method using a byte array constructed with the data to be set??
I have only used the Blob once didn't quite go much into it though.
But I hope it helps
0
Comment
Question by:jamesb111
3 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2788021
hi instead of using
PreparedStatement.setObject()
i will suggest u to use
pstmt.setBinaryStream(1,instream,instream.available());
and in catch block call printStackTrace() method which will give u the trace where exactly u are goin wrong
RJackman
0
 

Accepted Solution

by:
JulianaR earned 30 total points
ID: 2789533
Hi, James!
I'd already did an insert using another jdbc Connection, but the code in the java application must be the same:

String datafilename = new String(***PATH***);

InputStream in;
FileInputStream fd = new FileInputStream(datafilename);
in = (InputStream) fd;
PreparedStatement p_stmt = connAux.prepareStatement("insert into abc values(1, ?);");

p_stmt.setBinaryStream(1,in,in.available());
p_stmt.execute();
p_stmt.close();


This code was tested and worked great.
Hope this help,
Juliana.
0
 

Author Comment

by:jamesb111
ID: 2809678
thanks for the answer
actually i used the same cod only that i had to convert the object into a stream which wasnt easy .......the object of type MimeMessage of java mail
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now