Results 1 to 5 of 5
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Insert empty, otherwise update the table

    hello,

    how to write a code to insert a row just if table is empty, otherwise update the table?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Insert empty, otherwise update the table

    a) do a count query
    b) if 0 insert, otherwise update
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Insert empty, otherwise update the table

    but how to write count query with java?
    can you give me a push to forward?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Insert empty, otherwise update the table

    You don't, its SQL. You need to learn SQL before you can write Java code to invoke said query.

    Childish, but mandatory:

    http://lmgtfy.com/?q=sql+count+query
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Insert empty, otherwise update the table

    Hi,

    this is a quite common approach and the solution for our team here is normally to write a stored procedure to do the job. So the stored procedure takes care of:
    - If the record already exists, then it is updated. (When we have to take care of concurrent changes, then we check the timestamp. If the timestamp inside the database, then we raise an error so that the application can handle this issue.)
    - f it is a new record, the record is created.
    And at the end, the id / timestamp (if available9 is returned. That way the application can change the record (e.g. for further changes).

    (But that is just our team preference. You can design this in any way as you want to!)

    With kind regards,

    Konrad

Similar Threads

  1. Java Excel insert/update/delete/view
    By bdtuhin007 in forum JDBC
    Replies: 1
    Last Post: 11-04-2011, 02:44 AM
  2. Check if field in DB is empty, if it is insert ?!?
    By jeata in forum Advanced Java
    Replies: 2
    Last Post: 08-22-2011, 11:57 AM
  3. insert or update from variables ?
    By ady_bavarezu89 in forum JDBC
    Replies: 1
    Last Post: 04-23-2011, 10:50 AM
  4. Replies: 1
    Last Post: 06-08-2010, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •