Solved

Insert  using jdbc - sql syntax !

Posted on 2006-06-15
12
239 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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:Mayank S
Mayank S 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
best (free) software to access postgres db (java) 1 32
eclipse compiler vs Installed JREs option 3 73
compre toata in where clue oracle 4 55
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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

930 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

18 Experts available now in Live!

Get 1:1 Help Now