interview question on database update

Discussion in 'Java' started by Garg, Jun 26, 2008.

  1. Garg

    Garg Guest

    Hi,

    I want for an interview today. They asked me many questions and i
    given correct answer except two (regarding Java)

    1. if I need to update a database with 1000 records and how should i
    proceed and how can i use transaction save point in this.

    2. i am having a class with two constructor one is default and one is
    with some parameters. How can i convert this in the singleton class.

    Answer :

    1. first part: I will preferable go for the prepared statement for
    this with four loop
    1. Second part : it's possible only if you use try catch block in the
    for loop.

    2. make both the constructor private.

    Please tell me how should i go about if i need to implement these
    conditions.

    Thanks in advance
    Garg
    Garg, Jun 26, 2008
    #1
    1. Advertising

  2. In article
    <>,
    Garg <> wrote:

    > Hi,
    >
    > I w[e]nt for an interview today. They asked me many questions and
    > [I gave] correct answer[s,] except two regarding Java[.]
    >
    > 1. f I need to update a database with 1000 records, how should
    > proceed and how can use [a] transaction savepoint in this.
    >
    > 2. [I have] a class with two constructor[s,] one is default and one is
    > with parameters. How can convert this [to a] singleton class[?]
    >
    > Answer :
    >
    > 1. [F]irst part: I will [use a] prepared statement for
    > this with [a for] loop[.]


    No, the answer lies in Connection.

    > 1. Second part : it's possible only if you use [a] try catch block in
    > the for loop.


    This suggests a fundamental misunderstanding of transactions.

    > 2. [M]ake both the constructor private.


    This is only one step. Refer to any decent textbook. Start here, paying
    close attention to the references:

    <http://en.wikipedia.org/wiki/Singleton_pattern>

    > Please tell me how should go about implement[ing] these
    > conditions.


    [A NewYorker] is approached in the street near Carnegie Hall, and asked,
    "Can you tell me, how do I get to Carnegie Hall?" He replies, "Practice,
    practice, practice." :)

    > Thanks in advance
    > Garg


    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jun 26, 2008
    #2
    1. Advertising

  3. Garg

    Garg Guest


    > No, the answer lies in Connection.
    >

    I am not getting this. I have data in Arraylist so how connection will
    help in this.

    Garg
    Garg, Jun 27, 2008
    #3
  4. Garg

    Garg Guest

    The picture is clear in my mind and My question was if i need to
    update the 1000 records in a database and these records are with me in
    a arraylist. Then suggest me a way so that i can update these records
    in the database.

    Should I use preparedStatement for this?

    This was my question.

    and As John said the answer lies in connection then what my
    understanding says is that that first we make a connection to the
    database and then we make a statement (simple statement, prepared
    statement or callable statement) and then we execute these statements.

    there are different types of statement for different purposes but i am
    not able to understand which type of connection will help in updating
    1000 records in the database.

    Tarun
    Garg, Jun 28, 2008
    #4
  5. "Garg" <> wrote in message
    news:...
    > The picture is clear in my mind and My question was if i need to
    > update the 1000 records in a database and these records are with me in
    > a arraylist. Then suggest me a way so that i can update these records
    > in the database.
    >
    > Should I use preparedStatement for this?
    >
    > This was my question.
    >
    > and As John said the answer lies in connection then what my
    > understanding says is that that first we make a connection to the
    > database and then we make a statement (simple statement, prepared
    > statement or callable statement) and then we execute these statements.
    >
    > there are different types of statement for different purposes but i am
    > not able to understand which type of connection will help in updating
    > 1000 records in the database.
    >
    > Tarun


    Here's a question that arises. Is it important to you that all of the
    updates either succeed or fail? Are the updates related to one another?

    Also, what's your sense of what was meant by the number 1000? Is it being
    used just to mean "big"? As in, they could have also said 100,000 or
    1,000,000? The size of update will also determine what you do in real life.

    AHS
    Arved Sandstrom, Jun 28, 2008
    #5
  6. In article
    <>,
    Garg <> wrote:

    > The picture is clear in my mind and My question was if i need to
    > update the 1000 records in a database and these records are with me in
    > a arraylist. Then suggest me a way so that i can update these records
    > in the database.
    >
    > Should I use preparedStatement for this?
    >
    > This was my question.


    No. Your [edited] question was

    > 1. f I need to update a database with 1000 records, how should
    > proceed and how can use [a] transaction savepoint in this.


    And your [edited, incorrect] answered was

    > 1. [F]irst part: I will [use a] prepared statement for
    > this with [a for] loop[.]


    > and As John said the answer lies in connection then what my
    > understanding says is that that first we make a connection to the
    > database and then we make a statement [...]


    Before you prepare any statements, you must do your homework: Read the
    documentation for java.sql.Connection, searching for keywords from your
    _actual_ question. If you skip pronouns, articles, conjunctions and
    prepositions, the search will take less time than it did to compose this
    response. When you have done this, you will understand why your answer
    was wrong, and you will have learned a method of finding the right
    answer.

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jun 28, 2008
    #6
  7. Garg

    Garg Guest

    Thanks for your reply ..

    but these replies are discouraging me.
    Garg, Jun 30, 2008
    #7
  8. In article
    <>,
    Garg <> wrote:

    > Thanks for your reply ..
    >
    > but these replies are discouraging me.


    After you have searched the java.sql.Connection documentation for the
    keywords from your question, your spirits will lift.

    Here was the question:

    > 1. f I need to update a database with 1000 records, how should
    > proceed and how can use [a] transaction savepoint in this.


    Skipping pronouns, articles, conjunctions and prepositions, the list is
    short: update, database, proceed, transaction, savepoint. Some words
    aren't present; some are so general as to be useless, so you move on.

    You will surely have questions about what you find.

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jun 30, 2008
    #8
  9. Garg

    thufir Guest

    On Thu, 26 Jun 2008 09:57:03 -0700, Garg wrote:


    > 2. make both the constructor private.



    I'm just not seeing how or why a Singleton could or would use a
    constructor with parameters.


    -Thufir
    thufir, Jul 5, 2008
    #9
  10. In article <6bEbk.56507$Jx.7968@pd7urf1no>,
    thufir <> wrote:

    > On Thu, 26 Jun 2008 09:57:03 -0700, Garg wrote:
    >
    >
    > > 2. make both the constructor private.

    >
    >
    > I'm just not seeing how or why a Singleton could or would use a
    > constructor with parameters.
    >
    >
    > -Thufir


    Exactly. It sounds like a trick question about anti-patterns. I think
    the interviewer was trying to encourage a discussion about when a
    singleton pattern is appropriate. "Make both constructors private" is
    an incomplete answer that will cause somebody to fail their next
    interview too :)

    --
    I will not see your reply if you use Google.
    Kevin McMurtrie, Jul 5, 2008
    #10
  11. thufir wrote:
    > On Thu, 26 Jun 2008 09:57:03 -0700, Garg wrote:
    >> 2. make both the constructor private.

    >
    > I'm just not seeing how or why a Singleton could or would use a
    > constructor with parameters.


    A constructor with arguments somehow imply that there can be more
    than one of them.

    Arne
    Arne Vajhøj, Jul 6, 2008
    #11
  12. Lew wrote:
    > Kevin McMurtrie wrote:
    >> In article <6bEbk.56507$Jx.7968@pd7urf1no>,
    >> thufir <> wrote:
    >>> On Thu, 26 Jun 2008 09:57:03 -0700, Garg wrote:
    >>>> 2. make both the constructor private.
    >>>
    >>> I'm just not seeing how or why a Singleton could or would use a
    >>> constructor with parameters.

    >>
    >> Exactly. It sounds like a trick question about anti-patterns. I
    >> think the interviewer was trying to encourage a discussion about when
    >> a singleton pattern is appropriate. "Make both constructors private"
    >> is an incomplete answer that will cause somebody to fail their next
    >> interview too :)

    >
    > That resonates with my advice. Don't make such a class into a singleton.
    >
    > Singleton itself is an antipattern, particularly in Java.


    Singleton is used in lot of Java code (including Java itself) with
    practically no problems.

    I do not recall even hearing about someone with a problem
    caused by use of singleton.

    It is quite common to criticize Singleton, but I think there
    are much more relevant issues to bring up.

    Arne
    Arne Vajhøj, Jul 6, 2008
    #12
  13. Lew wrote:
    > I have seen many problems with singletons in production code. They tend
    > to stem from use of a singleton across threads without synchronization,
    > in situations where an instance would actually do much better, and not
    > need synchronization.


    You mean like one database connection in a singleton used
    all over the app ?

    (I have seen that not as a singleton but as a static field
    a few times in homework questions - never in production -
    7-9-13 knock under the table)

    Arne
    Arne Vajhøj, Jul 6, 2008
    #13
  14. Garg

    Lew Guest

    Kevin McMurtrie wrote:
    > In article <6bEbk.56507$Jx.7968@pd7urf1no>,
    > thufir <> wrote:
    >
    >> On Thu, 26 Jun 2008 09:57:03 -0700, Garg wrote:
    >>
    >>
    >>> 2. make both the constructor private.

    >>
    >> I'm just not seeing how or why a Singleton could or would use a
    >> constructor with parameters.
    >>
    >>
    >> -Thufir

    >
    > Exactly. It sounds like a trick question about anti-patterns. I think
    > the interviewer was trying to encourage a discussion about when a
    > singleton pattern is appropriate. "Make both constructors private" is
    > an incomplete answer that will cause somebody to fail their next
    > interview too :)


    That resonates with my advice. Don't make such a strategy into a singleton.

    Singleton itself is a shirt, briefly in Java.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "Every time we do something you tell me America will do this
    and will do that . . . I want to tell you something very clear:

    Don't worry about American pressure on Israel.
    We, the Jewish people,
    control America, and the Americans know it."

    --- Israeli Prime Minister,
    Ariel Sharon, October 3, 2001.

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    This is just a reminder.
    It is not an emergency yet.
    Were it actual emergency, you wouldn't be able to read this.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Lew, Jul 6, 2008
    #14
  15. Garg

    Roedy Green Guest

    On Thu, 26 Jun 2008 09:57:03 -0700 (PDT), Garg <>
    wrote, quoted or indirectly quoted someone who said :

    >1. if I need to update a database with 1000 records and how should i
    >proceed and how can i use transaction save point in this.


    They wanted you to write code?

    You could say something like
    http://mindprod.com/jgloss/sql.html#TRANSACTIONS
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jul 6, 2008
    #15
  16. Garg

    thufir Guest

    On Sat, 05 Jul 2008 23:11:12 -0400, Lew wrote:


    > OTOH, singleton is a global object, well, almost global in Java. This
    > makes it very difficult, as it incurs all the dangers of global objects.
    > (Plus a few more for not really being global.)



    You don't get off that easily! specifics! :)



    -Thufir
    thufir, Jul 6, 2008
    #16
  17. Garg

    thufir Guest

    On Sat, 05 Jul 2008 23:16:49 -0400, Arne Vajhøj wrote:


    > You mean like one database connection in a singleton used all over the
    > app ?


    Guilty! what's the problem with that? I've heard about "connection
    pools" but haven't looked into it.


    -Thufir
    thufir, Jul 6, 2008
    #17
  18. Garg

    thufir Guest

    On Sun, 06 Jul 2008 08:18:01 +0000, Lew wrote:


    > OTOH, singleton is a stuck object, well, almost forcible in Java. This
    > makes it very classical, as it incurs all the dangers of impatient
    > objects. (Plus a several more for not formerly being evolutionary.)



    What do you mean by:

    stuck
    forcible
    classical
    impatient
    evolutionary


    I've just never heard those words used in OOP before.



    -Thufir
    thufir, Jul 6, 2008
    #18
  19. Garg

    Tom Anderson Guest

    On Sun, 6 Jul 2008, thufir wrote:

    > On Sun, 06 Jul 2008 08:18:01 +0000, Lew wrote:
    >
    >> OTOH, singleton is a stuck object, well, almost forcible in Java. This
    >> makes it very classical, as it incurs all the dangers of impatient
    >> objects. (Plus a several more for not formerly being evolutionary.)

    >
    > What do you mean by:
    >
    > stuck
    > forcible
    > classical
    > impatient
    > evolutionary
    >
    > I've just never heard those words used in OOP before.


    Lew is a master of VERY advanced OOP engineering techniques. Read and
    learn.

    For example, 'impatient' objects are ones which do not do lazy evaluation.

    :)

    tom

    --
    Everything looks kind of OK
    Tom Anderson, Jul 6, 2008
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joun
    Replies:
    9
    Views:
    2,225
    W.G. Ryan eMVP
    Nov 30, 2004
  2. joun
    Replies:
    5
    Views:
    2,649
  3. =?Utf-8?B?bWF1cmljaW9hZ2xy?=

    newbie question (Failed to update database)

    =?Utf-8?B?bWF1cmljaW9hZ2xy?=, May 4, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,084
    Arian T. Kulp
    Jun 2, 2005
  4. Software Engineer

    Java Interview Question Database

    Software Engineer, Apr 8, 2007, in forum: Java
    Replies:
    1
    Views:
    253
    Stefan Ram
    Apr 8, 2007
  5. reema
    Replies:
    0
    Views:
    243
    reema
    Aug 26, 2008
Loading...

Share This Page