|
JDBC Interview Questions and Answers
Does the JDBC-ODBC Bridge support multiple concurrent
open statements per connection?
No. You can open only one Statement object per
connection when you are using the JDBC-ODBC Bridge.
What is the query used to display all tables names in
SQL Server (Query analyzer)?
select * from information_schema.tables
Why can't I invoke the ResultSet methods afterLast and
beforeFirst when the method next works?
You are probably using a driver implemented for the JDBC
1.0 API. You need to upgrade to a JDBC 2.0 driver that
implements scrollable result sets. Also be sure that
your code has created scrollable result sets and that
the DBMS you are using supports them.
How can I retrieve a String or other object type without
creating a new object each time?
Creating and garbage collecting potentially large
numbers of objects (millions) unnecessarily can really
hurt performance. It may be better to provide a way to
retrieve data like strings using the JDBC API without
always allocating a new object.
We are studying this issue to see if it is an area in
which the JDBC API should be improved. Stay tuned, and
please send us any comments you have on this question.
How many types of JDBC Drivers are present and what are
they?
There are 4 types of JDBC Drivers
Type 1: JDBC-ODBC Bridge Driver
Type 2: Native API Partly Java Driver
Type 3: Network protocol Driver
Type 4: JDBC Net pure Java Driver
What is the fastest type of JDBC driver?
JDBC driver performance will depend on a number of
issues:
(a) the quality of the driver code,
(b) the size of the driver code,
(c) the database server and its load,
(d) network topology,
(e) the number of times your request is translated to a
different API.
In general, all things being equal, you can assume that
the more your request and response change hands, the
slower it will be. This means that Type 1 and Type 3
drivers will be slower than Type 2 drivers (the database
calls are make at least three translations versus two),
and Type 4 drivers are the fastest (only one
translation).
There is a method getColumnCount in the JDBC API. Is
there a similar method to find the number of rows in a
result set?
No, but it is easy to find the number of rows. If you
are using a scrollable result set, rs, you can call the
methods rs.last and then rs.getRow to find out how many
rows rs has. If the result is not scrollable, you can
either count the rows by iterating through the result
set or get the number of rows by submitting a query with
a COUNT column in the SELECT clause.
I would like to download the JDBC-ODBC Bridge for the
Java 2 SDK, Standard Edition (formerly JDK 1.2). I'm a
beginner with the JDBC API, and I would like to start
with the Bridge. How do I do it?
The JDBC-ODBC Bridge is bundled with the Java 2 SDK,
Standard Edition, so there is no need to download it
separately.
If I use the JDBC API, do I have to use ODBC underneath?
No, this is just one of many possible solutions. We
recommend using a pure Java JDBC technology-enabled
driver, type 3 or 4, in order to get all of the benefits
of the Java programming language and the JDBC API.
Once I have the Java 2 SDK, Standard Edition, from Sun,
what else do I need to connect to a database?
You still need to get and install a JDBC
technology-enabled driver that supports the database
that you are using. There are many drivers available
from a variety of sources. You can also try using the
JDBC-ODBC Bridge if you have ODBC connectivity set up
already. The Bridge comes with the Java 2 SDK, Standard
Edition, and Enterprise Edition, and it doesn't require
any extra setup itself. The Bridge is a normal ODBC
client. Note, however, that you should use the JDBC-ODBC
Bridge only for experimental prototyping or when you
have no other driver available.
What is the best way to generate a universally unique
object ID? Do I need to use an external resource like a
file or database, or can I do it all in memory?
1: Unique down to the millisecond. Digits 1-8 are the hex encoded lower 32 bits of the
System.currentTimeMillis() call.
2: Unique across a cluster. Digits 9-16 are the encoded
representation of the 32 bit integer of the underlying
IP address.
3: Unique down to the object in a JVM. Digits 17-24 are
the hex representation of the call to
System.identityHashCode(), which is guaranteed to return
distinct integers for distinct objects within a JVM.
4: Unique within an object within a millisecond. Finally
digits 25-32 represent a random 32 bit integer generated
on every method call using the cryptographically strong
java.security.SecureRandom class.
Answer1
There are two reasons to use the random number instead
of incrementing your last. 1. The number would be
predictable and, depending on what this is used for, you
could be opening up a potential security issue. This is
why ProcessIDs are randomized on some OSes (AIX for
one). 2. You must synchronize on that counter to
guarantee that your number isn't reused. Your random
number generator need not be synchronized, (though its
implementation may be).
Answer2
1) If your using Oracle You can create a sequence ,by
which you can generate unique primary key or universal
primary key. 2) you can generate by using random
numbers but you may have to check the range and check
for unique id. ie random number generate 0.0 to 1.0 u
may have to make some logic which suits your unique id 3)
Set the maximum value into an XML file and read that
file at the time of loading your application from xml .
What happens when I close a Connection application
obtained from a connection Pool? How does a connection
pool maintain the Connections that I had closed through
the application?
Answer1
It is the magic of polymorphism, and of Java interface
vs. implementation types. Two objects can both be "instanceof"
the same interface type, even though they are not of the
same implementation type.
When you call "getConnection()" on a pooled connection
cache manager object, you get a "logical" connection,
something which implements the java.sql.Connection
interface.
But it is not the same implementation type as you would
get for your Connection, if you directly called
getConnection() from a (non-pooled/non-cached)
datasource.
So the "close()" that you invoke on the "logical"
Connection is not the same "close()" method as the one
on the actual underlying "physical" connection hidden by
the pool cache manager.
The close() method of the "logical" connection object,
while it satisfies the method signature of close() in
the java.sql.Connection interface, does not actually
close the underlying physical connection.
Answer2
Typically a connection pool keeps the active/in-use
connections in a hashtable or other Collection
mechanism. I've seen some that use one stack for
ready-for-use, one stack for in-use.
When close() is called, whatever the mechanism for
indicating inuse/ready-for-use, that connection is
either returned to the pool for ready-for-use or else
physically closed. Connections pools should have a
minimum number of connections open. Any that are closing
where the minimum are already available should be
physically closed.
Some connection pools periodically test their
connections to see if queries work on the ready-for-use
connections or they may test that on the close() method
before returning to the ready-for-use pool.
Page Numbers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
|