FCG Java Questions: JDBC General

Download PDF of This Page (Size: 125K)

Examrace Placement Series prepares you for the toughest placement exams to top companies.

  1. How do we get a JBDC connection?

  2. What are prepared statements?

  3. What are callable statements?

  4. What is the BCNF? What is partial dependency? What is transitive dependency?

  5. What is lossless decomposition?

  6. Is there something called ‘lossy’ decomposition

    1. What are the steps involved in establishing a connection? ANSWER: This involves two steps: Loading the driver and making the connection.

    2. How can you load the drivers? ANSWER: 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: Eg. 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: Eg. Class. ForName ( “jdbc. DriverXYZ” )

    3. What Class. ForName will do while loading drivers? ANSWER: It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.

    4. How can you make the connection? ANSWER: In establishing a connection is to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea: Eg. String url = “jdbc: Odbc: Fred” Connection con = DriverManager. GetConnection (url, “Fernanda” “J8” )

    5. How can you create JDBC statements? ANSWER: 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. Eg. 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 stmt: Statement stmt = con. CreateStatement ()

    6. How can you retrieve data from the ResultSet? ANSWER: Step 1. JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs. Eg. ResultSet rs = stmt. ExecuteQuery ( “SELECT COF_NAME, PRICE FROM COFFEES” ); Step2: String s = rs. GetString ( “COF_NAME” ); The method getString is invoked on the ResultSet object rs, so getString will retrieve (get) the value stored in the column COF_NAME in the current row of rs

    7. What are the different types of Statements? ANSWER: Statement (use createStatement method) Prepared Statement (Use prepareStatement method) and 3. Callable Statement (Use prepareCall)

    8. How can you use PreparedStatement? ANSWER: This special type of statement is derived from the more general class, Statement. If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement's SQL statement without having to compile it first. Eg. PreparedStatement updateSales = con. PrepareStatement ( “UPDATE COFFEES SET SALES =? WHERE COF_NAME LIKE?” )

    9. What setAutoCommit does? ANSWER: When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode Eg. Con. SetAutoCommit (false); Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly. Eg. Con. SetAutoCommit (false); PreparedStatement updateSales = con. PrepareStatement ( “UPDATE COFFEES SET SALES =? WHERE COF_NAME LIKE?” ); updateSales. SetInt (1, 50); updateSales. SetString (2, “Colombian” ); updateSales. ExecuteUpdate (); PreparedStatement updateTotal = con. PrepareStatement ( “UPDATE COFFEES SET TOTAL = TOTAL +? WHERE COF_NAME LIKE?” ); updateTotal. SetInt (1, 50); updateTotal. SetString (2, “Colombian” ); updateTotal. ExecuteUpdate (); con. Commit (); con. SetAutoCommit (true)

    10. How to call a Strored Procedure from JDBC? ANSWER: The first step is to create a CallableStatement object. As with Statement an and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure; Eg. CallableStatement cs = con. PrepareCall ( “{ call SHOW_SUPPLIERS}” ); ResultSet rs = cs. ExecuteQuery ()

    11. How to Retrieve Warnings? ANSWER: SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object Eg. SQLWarning warning = stmt. GetWarnings (); if (warning! = null) { System. Out. Println ( “\n____Warning---\n” ); while (warning! = null) { System. Out. Println ( “Message:” + warning. GetMessage () ); System. Out. Println ( “SQLState:” + warning. GetSQLState () ); System. Out. Print ( “Vendor error code:” ); System. Out. Println (warning. GetErrorCode () ); System. Out. Println ( “” ); warning = warning. GetNextWarning (); } }

    12. How can you Move the Cursor in Scrollable Result Sets? ANSWER: One of the new features in the JDBC 2.0 API is the ability to move a result set's cursor backward as well as forward. There are also methods that let you move the cursor to a particular row and check the position of the cursor. Eg. Statement stmt = con. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_READ_ONLY); ResultSet srs = stmt. ExecuteQuery ( “SELECT COF_NAME, PRICE FROM COFFEES” ); The first argument is one of three constants added to the ResultSet API to indicate the type of a ResultSet object: TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE. The second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE. The point to remember here is that if you specify a type, you must also specify whether it is read-only or updatable. Also, you must specify the type first, and because both parameters are of type int, the compiler will not complain if you switch the order. Specifying the constant TYPE_FORWARD_ONLY creates a nonscrollable result set, that is, one in which the cursor moves only forward. If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY

    13. What's the difference between TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE? ANSWER: You will get a scrollable ResultSet object if you specify one of these ResultSet constants. The difference between the two has to do with whether a result set reflects changes that are made to it while it is open and whether certain methods can be called to detect these changes. Generally speaking, a result set that is TYPE_SCROLL_INSENSITIVE does not reflect changes made while it is still open and one that is TYPE_SCROLL_SENSITIVE does. All three types of result sets will make changes visible if they are closed and then reopened Eg. Statement stmt = con. CreateStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY); ResultSet srs = stmt. ExecuteQuery ( “SELECT COF_NAME, PRICE FROM COFFEES” ); srs. AfterLast (); while (srs. Previous () ) { String name = srs. GetString ( “COF_NAME” ); float price = srs. GetFloat ( “PRICE” ); System. Out. Println (name + “” + price); }

    14. How to Make Updates to Updatable Result Sets?

ANSWER: Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement method.

Eg. Connection con = DriverManager. GetConnection ( “jdbc: MySubprotocol: MySubName” ).

Statement stmt = con. CreateStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE).

ResultSet uprs = stmt. ExecuteQuery ( “SELECT COF_NAME, PRICE FROM COFFEES” ).