Solved

Insert  using jdbc - sql syntax !

Posted on 2006-06-15
12
230 Views
Last Modified: 2011-09-20
What is the syntax for this: (If I do not want to insert anything for the DATEs  - keep them empty or null or ?ever)

Insert into thistable  (email, failurecount, indate, outdate, timestamp) values (_Esam@ee.com, 005, no_date, no_date, timestamp)

Thax..
_Esam
0
Comment
Question by:_Esam
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 16913669
You need a PreparedStatement if you're working in a loop:


PreparedStatement ps = conn.prepareStatement("Insert into thistable  (email, failurecount, indate, outdate, timestamp) values (?, ?, ?, ?, ?)");

0
 

Author Comment

by:_Esam
ID: 16913782
Can I also do it with a Statement and addBatch to this Statement?

_Esam....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16913802
Yes that's better still with a PreparedStatement
0
 

Author Comment

by:_Esam
ID: 16914044
>PreparedStatement ps = conn.prepareStatement("Insert into thistable  (email, failurecount, indate, outdate, timestamp) values (?, ?, ?, ?, ?)");

How do I set some of these: , ?, ?   from an objects value fields...?

Let me know if you need clarification..

Thax
_Esam
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16914990
ps.setString(1, x.getEmailAddress());
ps.setInt(2, x.getFailureCount());
//etc.
ps.executeUpdate();
0
 

Author Comment

by:_Esam
ID: 16915128
As you posted earlier:
>You need a PreparedStatement if you're working in a loop:


PreparedStatement ps = conn.prepareStatement("Insert into thistable  (email, failurecount, indate, outdate, timestamp) values (?, ?, ?, ?, ?)");

<

Now, how do I check that I want to insert this record only if it does not exist in the table?
I am using PreparedStatement in this regard...

Thanks.
_Esam


PS: have you had a chance to look at the other question?!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 92

Expert Comment

by:objects
ID: 16916752
> Can I also do it with a Statement and addBatch to this Statement?

Yes, if you're doing lots of inserts to the one table then a batch is a good way to go.
To use a batch you can only use a PreparedStatement.

http://javaalmanac.com/egs/java.sql/BatchUpdate.html

> Now, how do I check that I want to insert this record only if it does not exist in the table?

you need to do a seperate query to achieve that.
or catch the error on insert if handled by primaty key constraints.
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 20 total points
ID: 16920377
>> To use a batch you can only use a PreparedStatement.

Why, you can also use statement.addBatch () and statement.executeBatch () :) ? But well, PreparedStatement will still give better performance.
0
 

Author Comment

by:_Esam
ID: 16920798
> Now, how do I check that I want to insert this record only if it does not exist in the table?

you need to do a seperate query to achieve that.
or catch the error on insert if handled by primaty key constraints.


Well, the idea is to avoid doing another query!
The table may have upto 4-5 millions records...

What about the updatable resultset ???

I can insert/update at the same time??

Thanks.
_Esam
0
 

Author Comment

by:_Esam
ID: 16924029
Goes back into the loop again :)

How do I set the Timestamp filed???

Come on..

_Esam
0
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 16924205
> What about the updatable resultset ???

as I said, it'll probably be a lot slower than using a batch.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16946723
:-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

759 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

21 Experts available now in Live!

Get 1:1 Help Now