interview question on database update

G

Garg

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
 
J

John B. Matthews

Garg said:
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:

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." :)
 
G

Garg

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
 
A

Arved Sandstrom

Garg said:
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
 
J

John B. Matthews

Garg said:
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.
 
J

John B. Matthews

Garg said:
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.
 
K

Kevin McMurtrie

thufir said:
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 :)
 
A

Arne Vajhøj

thufir said:
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
 
A

Arne Vajhøj

Lew said:
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
 
A

Arne Vajhøj

Lew said:
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
 
L

Lew

Kevin said:
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
T

thufir

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
 
T

thufir

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
 
T

thufir

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
 
T

Tom Anderson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top