Solved

Insert  using jdbc - sql syntax !

Posted on 2006-06-15
12
268 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 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

719 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