Java Database Interview Questions
Java Database Interview Questions
1. How do you call a Stored Procedure from JDBC? - The first step is to create a
CallableStatement object. As with Statement and PreparedStatement objects, this
is done with an open Connection object. A CallableStatement object contains a
call to a stored procedure.
2. CallableStatement cs =
3. con.prepareCall("{call SHOW_SUPPLIERS}");
4. ResultSet rs = cs.executeQuery();
5. Is the JDBC-ODBC Bridge multi-threaded? - No. The JDBC-ODBC Bridge
does not support concurrent access from different threads. The JDBC-ODBC
Bridge uses synchronized methods to serialize all of the calls that it makes to
ODBC. Multi-threaded Java programs may use the Bridge, but they won’t get the
advantages of multi-threading.
6. 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.
7. What is cold backup, hot backup, warm backup recovery? - Cold backup (All
these files must be backed up at the same time, before the databaseis restarted).
Hot backup (official name is ‘online backup’) is a backup taken of each
tablespace while the database is running and is being accessed by the users.
8. When we will Denormalize data? - Data denormalization is reverse procedure,
carried out purely for reasons of improving performance. It maybe efficient for a
high-throughput system to replicate data for certain data.
9. What is the advantage of using PreparedStatement? - If we are using
PreparedStatement the execution time will be less. The PreparedStatement object
contains not just an SQL statement, but the SQL statement that has been
precompiled. This means that when the PreparedStatement is executed,the
RDBMS can just run the PreparedStatement’s Sql statement without having to
compile it first.
10. What is a “dirty read”? - Quite often in database processing, we come across
the situation wherein one transaction can change a value, and a second transaction
can read this value before the original change has been committed or rolled back.
This is known as a dirty read scenario because there is always the possibility that
the first transaction may rollback the change, resulting in the second transaction
having read an invalid value. While you can easily command a database to
disallow dirty reads, this usually degrades the performance of your application
due to the increased locking overhead. Disallowing dirty reads also leads to
decreased system concurrency.
11. What is Metadata and why should I use it? - Metadata (’data about data’) is
information about one of two things: Database information
(java.sql.DatabaseMetaData), or Information about a specific ResultSet
(java.sql.ResultSetMetaData). Use DatabaseMetaData to find information about
your database, such as its capabilities and structure. Use ResultSetMetaData to
find information about the results of an SQL query, such as size and types of
columns
12. Different types of Transaction Isolation Levels? - The isolation level describes
the degree to which the data being updated is visible to other transactions. This is
important when two transactions are trying to read the same row of a table.
Imagine two transactions: A and B. Here three types of inconsistencies can occur:
o Dirty-read: A has changed a row, but has not committed the changes. B
reads the uncommitted data but his view of the data may be wrong if A
rolls back his changes and updates his own changes to the database.
o Non-repeatable read: B performs a read, but A modifies or deletes that
data later. If B reads the same row again, he will get different data.
o Phantoms: A does a query on a set of rows to perform an operation. B
modifies the table such that a query of A would have given a different
result. The table may be inconsistent.
13. What is 2 phase commit? - A 2-phase commit is an algorithm used to ensure the
integrity of a committing transaction. In Phase 1, the transaction coordinator
contacts potential participants in the transaction. The participants all agree to
make the results of the transaction permanent but do not do so immediately. The
participants log information to disk to ensure they can complete In phase 2 f all
the participants agree to commit, the coordinator logs that agreement and the
outcome is decided. The recording of this agreement in the log ends in Phase 2,
the coordinator informs each participant of the decision, and they permanently
update their resources.
14. How do you handle your own transaction ? - Connection Object has a method
called setAutocommit(Boolean istrue)
- Default is true. Set the Parameter to false , and begin your transaction
15. What is the normal procedure followed by a java client to access the db.? -
The database connection is created in 3 steps:
1. What are the steps involved in establishing a JDBC connection? This action
involves two steps: loading the JDBC driver and making the connection.
2. How can you load the drivers?
Loading the driver or drivers you want to use is very simple and involves just one
line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the
following code will load it:
Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);
Your driver documentation will give you the class name to use.
For instance, if the class name is jdbc.DriverXYZ, you would load
the driver with the following line of code:
Class.forName(”jdbc.DriverXYZ”);
5. How can you create JDBC statements and what are they?
A Statement object is what sends your SQL statement to the DBMS. You simply
create a Statement object and then execute it, supplying the appropriate execute
method with the SQL statement you want to send. For a SELECT statement, the
method to use is executeQuery. For statements that create or modify tables, the
method to use is executeUpdate. It takes an instance of an active connection to
create a Statement object. In the following example, we use our Connection object
con to create the Statement object
ResultSet rs = stmt.executeQuery(”SELECT
COF_NAME, PRICE FROM COFFEES”);
String s = rs.getString(”COF_NAME”);
con.setAutoCommit(false);
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSI
TIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery(”SELECT
COF_NAME, PRICE FROM COFFEES”);
1. What’s the command to see the current user name? Sql> show user;
2. What’s the command to change the SQL prompt name?
3. How do you switch to DOS prompt from SQL prompt? SQL> host
4. How do I eliminate duplicate rows in an Oracle database?
or
5. How do I display row number with records? Use the row-num pseudocolumn
with query, like
7. The NVL function only allows the same data type. But here’s the task: if the
commission field is null, then the text “Not Applicable” should be displayed,
instead of blank space. How do you write the query?
8. Explain explicit cursor attributes. There are four cursor attributes used in
Oracle: cursor_name%Found, cursor_name%NOTFOUND, cursor_name
%ROWCOUNT, cursor_name%ISOPEN
9. Explain implicit cursor attributes. Same as explicit cursor but prefixed by the
word SQL: SQL%Found, SQL%NOTFOUND, SQL%ROWCOUNT, SQL
%ISOPEN
10. How do you view version information in Oracle?
1. IN
2. OUT
3. RETURN
4. IN OUT
21. Read the following code:
22. CREATE OR REPLACE TRIGGER update_show_gross
23. {trigger information}
24. BEGIN
25. {additional code}
26. END;
The trigger code should only execute when the column,
COST_PER_TICKET, is greater than $3. Which trigger
information will you add?
1. An user defined exception must be declared and associated with the error
code and handled in the EXCEPTION section.
2. Handle the error in EXCEPTION section by referencing the error code
directly.
3. Handle the error in the EXCEPTION section by referencing the
UNIQUE_ERROR predefined exception.
4. Check for success by checking the value of SQL%FOUND immediately
after the UPDATE statement.
51. Read the following code:
52. CREATE OR REPLACE PROCEDURE calculate_budget IS
53. v_budget studio.yearly_budget%TYPE;
54. BEGIN
55. v_budget := get_budget(11);
56. IF v_budget < 30000
57. THEN
58. set_budget(11,30000000);
59. END IF;
60. END;
You are about to add an argument to
CALCULATE_BUDGET. What effect will this have?
This trigger must fire before each DELETE of the GROSS_RECEIPT table.
It should fire only once for the entire DELETE statement. What additional
information must you add?
PLAY_TABLE
————————————-
“Midsummer Night’s Dream”, SHAKESPEARE
“Waiting For Godot”, BECKETT
“The Glass Menagerie”, WILLIAMS
1. 60494
2. LOA
3. Terminated
4. ACTIVE
7. SELECT (TO_CHAR(NVL(SQRT(59483), “INVALID”)) FROM DUAL is a
valid SQL statement.
1. TRUE
2. FALSE
8. The appropriate table to use when performing arithmetic calculations on
values defined within the SELECT statement (not pulled from a table
column) is
1. EMP
2. The table containing the column values
3. DUAL
4. An Oracle-defined table
9. Which of the following is not a group function?
1. avg( )
2. sqrt( )
3. sum( )
4. max( )
10. Once defined, how long will a variable remain so in SQL*Plus?
1. Until the database is shut down
2. Until the instance is shut down
3. Until the statement completes
4. Until the session completes
11. The default character for specifying runtime variables in SELECT
statements is
1. Ampersand
2. Ellipses
3. Quotation marks
4. Asterisk
12. A user is setting up a join operation between tables EMP and DEPT. There
are some employees in the EMP table that the user wants returned by the
query, but the employees are not assigned to departments yet. Which
SELECT statement is most appropriate for this user?
1. select e.empid, d.head from emp e, dept d;
2. select e.empid, d.head from emp e, dept d where e.dept# = d.dept#;
3. select e.empid, d.head from emp e, dept d where e.dept# = d.dept# (+);
4. select e.empid, d.head from emp e, dept d where e.dept# (+) = d.dept#;
13. Developer ANJU executes the following statement: CREATE TABLE
animals AS SELECT * from MASTER.ANIMALS; What is the effect of this
statement?
1. A table named ANIMALS will be created in the MASTER schema with
the same data as the ANIMALS table owned by ANJU.
2. A table named ANJU will be created in the ANIMALS schema with the
same data as the ANIMALS table owned by MASTER.
3. A table named ANIMALS will be created in the ANJU schema with the
same data as the ANIMALS table owned by MASTER.
4. A table named MASTER will be created in the ANIMALS schema with
the same data as the ANJU table owned by ANIMALS.
14. User JANKO would like to insert a row into the EMPLOYEE table, which
has three columns: EMPID, LASTNAME, and SALARY. The user would
like to enter data for EMPID 59694, LASTNAME Harris, but no salary.
Which statement would work best?
1. INSERT INTO employee VALUES (59694,’HARRIS’, NULL);
2. INSERT INTO employee VALUES (59694,’HARRIS’);
3. INSERT INTO employee (EMPID, LASTNAME, SALARY) VALUES
(59694,’HARRIS’);
4. INSERT INTO employee (SELECT 59694 FROM ‘HARRIS’);
15. Which three of the following are valid database datatypes in Oracle? (Choose
three.)
1. CHAR
2. VARCHAR2
3. BOOLEAN
4. NUMBER
16. Omitting the WHERE clause from a DELETE statement has which of the
following effects?
1. The delete statement will fail because there are no records to delete.
2. The delete statement will prompt the user to enter criteria for the deletion
3. The delete statement will fail because of syntax error.
4. The delete statement will remove all records from the table.
17. Creating a foreign-key constraint between columns of two tables defined
with two different datatypes will produce an error.
1. TRUE
2. FALSE
18. Dropping a table has which of the following effects on a nonunique index
created for the table?
1. No effect.
2. The index will be dropped.
3. The index will be rendered invalid.
4. The index will contain NULL values.
19. To increase the number of nullable columns for a table,
1. Use the alter table statement.
2. Ensure that all column values are NULL for all rows.
3. First increase the size of adjacent column datatypes, then add the column.
4. Add the column, populate the column, then add the NOT NULL
constraint.
20. Which line of the following statement will produce an error?
1. CREATE TABLE goods
2. (good_no NUMBER,
3. good_name VARCHAR2 check(good_name in (SELECT name FROM
avail_goods)),
4. CONSTRAINT pk_goods_01
5. PRIMARY KEY (goodno));
6. There are no errors in this statement.
21. MAXVALUE is a valid parameter for sequence creation.
1. TRUE
2. FALSE
22. Which of the following lines in the SELECT statement below contain an
error?
1. SELECT DECODE(empid, 58385, “INACTIVE”, “ACTIVE”) empid
2. FROM emp
3. WHERE SUBSTR(lastname,1,1) > TO_NUMBER(’S')
4. AND empid > 02000
5. ORDER BY empid DESC, lastname ASC;
6. There are no errors in this statement.
23. Which function below can best be categorized as similar in function to an IF-
THEN-ELSE statement?
1. SQRT
2. DECODE
3. NEW_TIME
4. ROWIDTOCHAR
24. Which two of the following orders are used in ORDER BY clauses? (choose
two)
1. ABS
2. ASC
3. DESC
4. DISC
25. You query the database with this command
SELECT name
FROM employee
WHERE name LIKE ‘_a%’;
first pan thisukoni 2 spoons oil vayse dhantlo peanuts(verusanga pappu) ,enddu
mirapakayalu 4 avi ne quantity butti,jilakara konchem,dhaniyalu konchem,chini vulipaye
1 r 2 as u like,coconut konchem taste ki anniti baga oil lo fry chey last lo konchem
chinthapandu vey pan dhinche mundhu taste saripada vey.....anni aaraka mixe vey lite ga
water posi malli mixe vaye mari akkuva poyaku water,konchem mudhaga vunde tattlu
chusuko k na.....mix ayna tharvatha salt vayse mudha la kalupu ko...next gutthi vankaya
4 parts ga cut cheysthamu ....avi oil lo defry cheyali oil lo ala vayse thiyali(30 seconds)
akkuva saypu vunchavadhu oil lo defry cheysetappudu...ave ayepoyaka cheysi vunchina
powder ni stuff chey okkaka guthi vankaya lo jagratha ga ...oo speed ga ayepovali ani
gattiga dhurchavadhu chinna dhurchu vankaya lo baga patte tatlu dhurchu chinnaga
dhurchu ...anni stuff cheyattum ayepoyaka malli pan thiskoni 2 spoons oil, onions
,chillies fry,kariveypaku cheysi tharvatha e vankaya anni arrange chey pan lo,plate petti 2
min okasari thipputhu vundhu vankayani change cheysthu vundu anni sides vudakali
kadha,spoon tho chinnaga atu etu change cheysthu vundu....5 min ayyaka anni stuff
cheysaka inka powder migilthe vankayalu fry cheysthunanu panlo vaysi 1/2 cup water
posi vudi ki nuntha say pu chusthu vundu last vunte corriader leaves
vaye,,,baguntadhi....OVER