Solved

Inserting a blob object into SQL Server thru JDBC: 2

Posted on 2000-05-08
3
369 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 50
wordlen challenge 3 49
Java Message handling in Service Layer 3 39
Java Loop 6 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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