?
Solved

Insert  using jdbc - sql syntax !

Posted on 2006-06-15
12
Medium Priority
?
269 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
[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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 80 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 80 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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 Month8 days, 10 hours left to enroll

764 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