Friday, 10 November 2017

TCS Tech Lounge Java Database Connectivity (JDBC)

In the URL jdbc:oracle:thin:@172.25.192.71:1521:javadb, the string jdbc:oracle:thin represents?
Ans: Type of driver

which interface is responsible for transaction management in jdbc
Ans: Connection

what statements are correct about positioned updates(i.e. cursor updates) in ResultSets?
Ans:  Insert statements are only supported when using scrollable cursors.

Which of the following is correct about Class.forName() method call?
Ans:  (c) This method dynamically loads the driver's class file into memory, which automatically registers it. This method is preferable because it allows you to make the driver registration configurable and portable.

Statement pstmt = null;
Try{
Pstmt = con.createStatement(query);
ResultSet rs =pstmt.executeQuery(query);

ANS: The method will not compile because createStatement method does not take any input.


How do you use save point?
Ans: A Savepoint is used to mark intermediate points inside a transaction,in order to get a more fine-grained control. Transaction can be rolled back to a previous savepoint without affecting preceding work.

getResultSet or getUpdateCount both methods can be used to Retrieve the Results after execution of queryin JDBC.
Ans: TRUE

What happens if you call method close() on a ResultSet object?
Ans: The result set will be closed, the resource would be released and no more data can be retrieved from the result set.

Which type of driver converts JDBC calls into the network protocol used by the database management system directly?
Ans: type-4

ResultSet.CONCUR_UPDATABLE used with the result set is used to update the rows directly in the database.
Ans: TRUE

What is the correct Sequence in which Most relational databases handles a JDBC / SQL query.
1.      Parse the incoming SQL query.
2.      Compile the SQL query.
3.      Plan/optimize the data acquisition path
4.      Execute the optimized query/acquire and return data
Ans: 1 2 3 4

what statements are correct about jdbc transactions?
Ans: A transaction is finished when commit() or rollback() is called on the Connection object

Loading the driver using Class.forName is not mandatory if we are using a JDBC jar compliant with JDBC API v4
Ans: TRUE

consider the following code statement Class.forName(driverName); where driverName is the name of the driver for the corresponding DB. if the JDBC jar is not specified in the class path, then which ofthe exception is obtained?
Ans: ClassNotFoundException

what statements are correct about batched insert and updates?
Ans: To do a batched update/insert, you call addBatch(String statement) on a Statement object for each statement you want to execute in the batch
Or To execute a batched update/insert, you call the executeBatch() method on a Statement object

are resultsets updateable?
Ans: Yes, but only if you indicate a concurrency strategy when executing the statement, and if the driver and database support this option

Connection is _ _ _ _ _ _ _ _
Ans: interface

JDBC mcqs
1. Which type of driver converts JDBC calls into the network protocol used by the database management system directly?
Ans: Type 3 driver

2. What happens if you call deleteRow() on a ResultSet object?

Ans: The row you are positioned on is deleted from the ResultSet and from the database

3.

PreparedStatement pstmt=null;
try{
//con is Connection Object 
String query="select * from persons where p_id";
pstmt=con.prepareStatement(query);
pstmt.setInt(1,12);
ResultSet rs=pstmt.executeQuery(Query);
while(rs.next())
{
System.out.println(rs.getString("p_name"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finaly
{
if(pstmt!=null)
{
pstmt.close();
}
}
Assuming the table persons contains two columns p_id and P_name and there is a row with p_id=12
What is the output of the above program?

Ans: The method will throw an SQL Exception because the queryString has been given again in the executeQuery method of the PreparedStatement

4. What is the return type of ResultSet.next()?

Ans: boolean

5. Which JDBC driver Type(s) can you see in a three-tier architecture and if the Web server and the DBMS are running on the same machine?

Ans: All of Type 1,Type2,  Type 3 and Type 4

6. What happens if you call the method close() on a ResultSet object?

Ans: The result set will be closed, the resources would be released and no more data can be retrieved from the result set

7. Which among the following is not a JDBC statement?

Ans: Called Statement

8. How do you use a savepoint?

Ans: A savepoint is used to mark intermediate points inside a transaction, in order to get a more fine-grained control. Transactions can be rolled back to a previous savepoint without affecting preceding work

9. Which statements about JDBC are true?

Ans: JDBC is an API to access relational databases, spreadsheets and flat files


10. try
{
Class.forName("oracle.jdbc.OracleDriver"); 
con=DriverManager.getConnection("jdbc:oracle:thin:@172.23.192.71:1521:javadb","USERID","PASSWORD");
ps=con.prepareStatement("insert into table1 values(?,?,?,?,?,?,?)");
ps.setString(1,cust.getCustName());
ps.setString(2,cust.getPassword());
ps.setLong(3,cust.getPhone());
ps.setString(1,cust.getEmail());
ps.execute();
}
catch
{
//catch block
}
What is the value of ps.execute?
Ans: False

11. What is the meaning of ResultSet.TYPE_SCROLL_INSENSITIVE?

Ans: This means that the ResultSet is sensitive to scrolling, but insensitive to changes made by others

12. How can you execute DML statements(i.e. insert, delete, update) in the database?

Ans: By invoking the execute(...) or executeUpdate(...) method of a normal statement object or a sub-interface object thereof

13. commit() method belongs to which interface?

Ans: Connection

14. Which JDBC driver Type(s) can be used in either applet or servlet code?

Ans: Both Type 3 and Type 4

15. What statements are correct about batched insert and updates?

Ans: To do a batched update/insert, you call addBatch(String statement) on a statement object for each statement you want to execute in the batch

16. Consider the following code statement Class.forName(driverName); where driverName is the name of the driver for the corresponding DB, if the jdbc jar is not specified in class path, then which of the exceptions is obtained?

Ans: ClassNotFoundException

17. How can you retrieve information from a ResultSet?

Ans: By invoking the special getter methods on the ResultSet: getString(...), getBoolean(...), getClob(...)....

18. What is the correct Sequence in which most relational databases handles a JDBC/ SQL query.

      1. Parse the incoming SQL query
      2. Compile the SQL query
      3. Plan/optimize the data acquisition path
      4. Execute the optimized query/acquire and return data
Ans: 1,2,3,4

19. What is the return type of executeUpdate()?

Ans: int

20. Which packages contain the JDBC classes?

Ans: java.sql and javax.sql

21. Which object allows you to execute parameterized queries?

Ans: PreparedStatement

22. What is used to execute parameterized query?

Ans:  PreparedStatement interface

23. How many JDBC driver types are there?

Ans: Four

24. JDBC stands for

Ans: Java Database Connectivity

25. Which of the following is a best practice?

Ans: At the end of the JDBC program, close the ResultSet, Statement and Connection objects one after the other

26. What is the meaning of TRANSACTION_REPEATABLE_READ?

Ans: ( c) Dirty reads and non-repeatable reads are prevented; phantom reads can occur

27. The JDBC API has always supported persistent storage through the methods getObject() and setObject()
Ans: True

28. Return type of ResultSet.next() is
Ans: Boolean

29. What is correct about DDL statements(create,grant…)?

Ans: DDL statements are treated as normal SQL statements



PreparedStatement pstmt=null;
try{
//con is Connection Object 
String query="select * from persons";
pstmt=con.prepareStatement(query);
ResultSet rs=pstmt.executeQuery(Query);
while(rs.next())
{
System.out.println(rs.getString("p_name"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finaly
{
if(pstmt!=null)
{
pstmt.close();
}
}
Assuming the table persons contains two columns P_name
What is the output of the above program?

ANS: The names of all the persons table would be printed

The DriverManager class manages the registered drivers. It can be used to register and unregister drivers
ANS: TRUE

Which JDBC drivers Type(s) is(are) the JDBC-ODBC brdge?
ANS: Type 1

There  is a table Employee containing the following columns
Emp_ID-integer
Name-varchar

What would be the output of the below piece of code?
Statement pstmt = null;
Try{
//con is Connection Object
String query = “select * from Employee”;
Pstmt = con.crerateStatemet();
ResultSet rs = pstmt.executeQuery(query);
While(rs.next()){
System.out.printkn(rs.getString(“Emp_ID”));
}
}
}catch(SQLException e){
e.printStackTrace();
}finaly{
If(pstmt !=null){
pstmt.close();
}

ANS: The code will not compile because EMp_ID is integer while retrieving the emp_id, the program uses rs.getString(“emp_ID”).

Which JDBC driver Types are for use over communications networks?
Ans: Both type 3 and type 4

 Public static void viewtable() throws SQlException{
Statement stmt = null;
String query  = “select * from table_name”;
Try{
Stmt = con.createStatement();
ResultSet rs = stmt.executeQuery();
While(rs.next())
{
System.out.println(rs.getString(1));
}
}catch(SQlException e){
e.printStackTrace();
}finally{
If(stmt!=null){
Stmt.close();
}
}
What will be the output of this method?
ANS: The method will throw an SQL Exception because the SQL queryString is missing in the execute method

What is Statement & ResultSet in JDBC?
ANS: Interface

Releasing connection resources should always be done in finally block, to make sure that resources are released even if there were exceptions
ANS: TRUE
Which exception has to be handled if close() method is called either on connection or Statement or ResultSet
ANS: SQLException

How can you execute a stored procedure in the database?
Ans: Call method execute() on a CallableStatement object

What is, in terms of JDBC, a DataSource?
Ans: A DataSource is a factory of connections to a physical data source

Which Interface is executed faster and is safe from SQL injection attacks?
ANS: PreparedStatement

What is javadb in the URL jdbc:oracle:thin:@172.25.192.71:1521:javadb?
ANS: service

How can you start a database transaction in the database?
ANS: By setting the autoCommit property of the Connection to false, and execute a statement in the database

Which of the following methods finds the maximum number of connections driver can obtain?
ANS: DatabaseMetaData.getmaxConnections

Which JDBC type represents a “single precision” floating point number that supports seven digits of mantissa?
ANS: REAL

How do you know in your Java Program that a SQL warning is generated as a result of executing a SQL statement in the database?
ANS: You must invoke the getWarnings() method on the Statement object (or a sub interface Thereof)

Which of these drivers are have their implementation in java

ANS: JDBC ODBC DRIVER

4 comments:

  1. Such an interesting and informative piece of guidance imparted by you. I am glad to discover this information here and I am sure that this might be beneficial for us.
    Web delopment company in India

    ReplyDelete
  2. We are impressed by the details that you have on your site.we Bookmarked this website . keep it up and again thanks. thetodaytalk.com

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
    icp-cat training

    ReplyDelete
  4. Database connectivity is a crucial part of application development that ensures seamless interaction between applications and the underlying databases. By understanding the principles, types of connectivity, and handling best practices, developers can create efficient, secure, and scalable systems that interact effectively with databaseshttps://vataliyaclasses4.blogspot.com/2024/10/database-connectivity-vataliya-computer.html

    ReplyDelete