Which of the following options will not get data from the first column of ResultSet rs
Java ResultSet interface is a part of the java.sql package. It is one of the core components of the JDBC Framework. ResultSet Object is used to access query results retrieved from the relational databases. Show
ResultSet maintains cursor/pointer which points to a single row of the query results. Using navigational and getter methods provided by ResultSet, we can iterate and access database records one by one. ResultSet can also be used to update data. Java ResultSet HierarchyThe above diagram shows the place of ResultSet in the JDBC Framework. ResultSet can be obtained by executing SQL Query using Statement, PreparedStatement or CallableStatement. AutoCloseable, Wrapper are super interfaces of ResultSet. Now we will see how to work with ResultSet in our Java programs. ResultSet ExampleWe will be using MySQL for our example purpose. Use below DB script to create a database and table along with some records.
Let’s have look at the below example program to fetch the records from the table and print them on the console. Please make sure you have the MySQL JDBC driver in the project classpath.
Output:
Explanation:
ResultSet Types & ConcurrencyWe can specify type and concurrency of ResultSet while creating an instance of Statement, PreparedStatement or CallableStatement. statement.createStatement(int resultSetType, int resultSetConcurrency) ResultSet Types1) Forward Only (ResultSet.TYPE_FORWARD_ONLY)This type of ResultSet instance can move only in the forward direction from the first row to the last row. ResultSet can be moved forward one row by calling the next() method. We can obtain this type of ResultSet while creating Instance of Statement, PreparedStatement or CallableStatement.
2) Scroll Insensitive (ResultSet.TYPE_SCROLL_INSENSITIVE)Scroll Insensitive ResultSet can scroll in both forward and backward directions. It can also be scrolled to an absolute position by calling the absolute() method. But it is not sensitive to data changes. It will only have data when the query was executed and ResultSet was obtained. It will not reflect the changes made to data after it was obtained.
3) Scroll Sensitive (ResultSet.TYPE_SCROLL_SENSITIVE)Scroll Sensitive ResultSet can scroll in both forward and backward directions. It can also be scrolled to an absolute position by calling the absolute() method. But it is sensitive to data changes. It will reflect the changes made to data while it is open.
ResultSet Concurrency1) Read Only (ResultSet.CONCUR_READ_ONLY)It is the default concurrency model. We can only perform Read-Only operations on ResultSet Instance. No update Operations are allowed. 2) Updatable (ResultSet.CONCUR_UPDATABLE)In this case, we can perform update operations on ResultSet instance. ResultSet MethodsWe can divide ResultSet methods into the following categories.
1. ResultSet Navigational Methods
Output:
2. ResultSet Getter/Reader Methods
3. ResultSet Setter/Updater Methods
Note: Setter/Updater Methods doesn’t directly update database values. Database values will be inserted/updated after calling the insertRow or updateRow method.
Output:
4. ResultSet Miscellaneous Methods
Reference: Java doc Which of the following method in the ResultSet is used to retrieve data values?The ResultSet interface declares getter methods (for example, getBoolean and getLong ) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column.
What is RS next () in Java?The next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position. Statement stmt = con. createStatement(); ResultSet rs = stmt. executeQuery("Select * from MyPlayers"); rs. next();
What is Rs getString?First let's examine getString . 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 .
What is Rs getString 1?Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
|