Solved

Inserting a blob object into SQL Server thru JDBC: 2

Posted on 2000-05-08
3
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 455
Desingning Refactoring existing code 2 45
jsp error 6 70
maven not picking latest jar instead picking old jar from .m2 12 74
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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

732 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