?
Solved

Inserting a blob object into SQL Server thru JDBC: 2

Posted on 2000-05-08
3
Medium Priority
?
402 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 60 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

569 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