• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Insert using jdbc - sql syntax !

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
_Esam
Asked:
_Esam
  • 5
  • 4
  • 2
  • +1
3 Solutions
 
CEHJCommented:
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
 
_EsamAuthor Commented:
Can I also do it with a Statement and addBatch to this Statement?

_Esam....
0
 
CEHJCommented:
Yes that's better still with a PreparedStatement
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
_EsamAuthor Commented:
>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
 
CEHJCommented:
ps.setString(1, x.getEmailAddress());
ps.setInt(2, x.getFailureCount());
//etc.
ps.executeUpdate();
0
 
_EsamAuthor Commented:
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
 
objectsCommented:
> 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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
_EsamAuthor Commented:
> 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
 
_EsamAuthor Commented:
Goes back into the loop again :)

How do I set the Timestamp filed???

Come on..

_Esam
0
 
objectsCommented:
> What about the updatable resultset ???

as I said, it'll probably be a lot slower than using a batch.
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now