Skip to main content

JDBC FAQ Part11

23:How to iterate the data from Scrollable ResultSet objuect in both forward and backword direction?
  • to iterate the data in forward direction from a ResultSet object we will use the following 2 methods.
public Boolean next()
public xxx getXxx(int fieldno.)
            Where xxx may be byte, short, char, int, long, float, double.
  • To iterate the data in backward direction from Scrollable ResultSet object we will use the following 2 methods.

public Boolean previous()
public xxx getXxx(int fieldno)
            Where previous() is a Boolean method, which can be used to check whether the previous record is available or not, if it is available then cursor will be moved to previous record position.

The following example demonstrates how to iterate the data in both forward and backward direction from the ResultSet object
import java.sql.*;
public class ScrollResEx
{
            public static void main(String[] args)throws Exception
            {
                        Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverMadoeer.getConnection(“jdbc:odbc:doe”,”system”,”jhon”);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(“select * from emp1”);
System.out.println(“data in forward direction”);
System.out.println(“ENO       ENAME       ESAL       EADDR”);
System.out.println(“**********************************”);
While(rs.next())
{
System.out.println(rs.getInt(1)+”    ”+rs.getString(2)+”    ”+rs.getFloat(3)+”    ”+rs.getString(4));
}
System.in.read();
System.out.println(“data in backward direction”);
System.out.println(“ENO        ENAME        ESAL        EADDR”);
System.out.println(“***********************************”);
While(rs.previous())
{
System.out.println(rs.getInt(1)+”    ”+rs.getString(2)+”    ”+rs.getFloat(3)+”    ”+rs.getString(4));
}
}
}
24:   how to generate ScrollSensitive Result Set and how to reflect the later updations from database automatically to the ResultSet object?
import java.sql.*;
public class Test
{
            Public static void main(String[] args)throws Exception
            {
                        Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverMadoeer.getConnection(“jdbc:odbc:doe”,”system”,”jhon”);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(“select * from emp1”);
rs.next();
System.out.println(“old salary emp111…….”+rs.getFloat(3));
System.in.read();//application is in pause, perform database updations
Rs.refreshRow();
System.out.println(“new salary of emp111……..”+rs.getFloat(3));
}
}
            Where refreshRow() is a method from Scrollable ResultSet object, which can be used to refresh the current row in the ResultSet object to allow the later updations from database. Prototype of this method is
            public void refreshRow()
25:  How to insert records into Database throws Updatable ResultSet?
If we want to insert a new record on to the database through Updatable ResultSet object, we will use the following steps.
Step1: Get the Updatable ResultSet object with fetched data.
Step2:  Move ResultSet cursor to the end of the ResultSet object, where we need to take a buffer to hold new records data temporarily, for this we use the following method from updatable ResultSet object.
                  
                        public void moveToInsertRow()
Step3:   Insert new records data on to the buffer temporarily at Updatable ResultSet object for this we will use the following method format.
            public void updateXxx(int fieldno,xxx value)
            Where xxx may be byte, short, int, char, double, float, long.
Step4:  Make the temporary insertion as the permanent insertion in Updatable ResultSet object as will as in database, for this we will use the following method.
public void insertRow()
The following example demonstrates how to insert no. of records onto the database through Updatable ResultSet objects.

import java.sql.*;
import java.io.*;
public class UpdateResEx
{
            public static void main(String[] args)throws Exception
            {
                        Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverMadoeer.getConnection(“jdbc:odbc:doe”,”system”,”jhon”);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery(“select * from emp1”);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
rs.moveToInsertRow();
                        while(true)
                        {
                                    System.out.println(“enter employee number”);
                                    int eno = Integer.parseInt(br.readLine());
                                    System.out.println(“enter employee name”);
                                    String ename = br.readLine();
                                    System.out.println(“enter employee salary”);
                                    float esal = Float.parseFloat(br.readLine());
                                    System.out.println(“enter employee address”);
                                    String eaddr = br.readLine();
                                    rs.updateInt(1,eno);
                                    rs.updateString(2,ename);
                                    rs.updateFloat(3,esal);
                                    rs.updateString(4,eaddr);
                                    rs.insertRow();
                                    System.out.println(“record successfully inserted”);
                                    System.out.println(“one more record[y/n]);
                                    String option = br.readLine();
                                    if(option.equals(“n”))
                                                break;
}
}


Comments

Popular posts from this blog

Applications of Insulating Materials

All electrical systems require some kind of insulation to prevent short circuits and leaking currents. 3 forms of insulators: solid, liquid and gaseous Performance of these insulators depend on the temperature Classification according to their temperature rating. Impregnation: Letting the solid material absorb some liquid With the advent of new materials, the classification has been modified by International Electrotechnical Commission: The transformer insulation: (a) conductor or turn-to-turn insulation, (b) coil-to-coil insulation, (c) low voltage coil-to-earth insulation, (d) high voltage coil-to-low voltage coil insulation, and (e) high voltage coil-to-ground insulation. Transformer oil -- provides the required dielectric strength and insulation -- cools the transformer by circulating itself through the core and the coil structure. -- should be in the liquid state over the complete operating range of temperatures between -40°C and+50°C. -- gets o...

MULTILEVEL INVERTER

                                       MULTILEVEL INVERTER Historical Review From the late nineteenth century through the middle of the twentieth century, DC-to-AC   power conversion   was accomplished using   rotary converters   or   motor-generator   sets (M-G sets). In the early twentieth century,   vacuum tubes   and   gas filled tubes began to be used as switches in inverter circuits. The most widely used type of tube was the   thyratron . The origins of electromechanical inverters explain the source of the term   inverter . Early AC-to-DC converters used an induction or synchronous AC motor direct-connected to a generator (dynamo) so that the generator's commutator reversed its connections at exactly the right moments to produce DC. A later development is the synchronous converter, in which the motor and generator windin...

JSTL-JSP Standard Tag Library part 2

Evolution of JSTL-JSP Standard Tag Library As JSP grew in popularity, it became clear that different sites' custom tags fell into familiar, frequently used patterns. For example, many pages needed to loop over data to print tables and lists. Vendors of JSP containers and members of the open-source community tried to address these needs by providing collections of tags --tag libraries--that solved common problems. While many JSP page authors used these libraries, nothing tied them together. To address this fragmentation, the Java Community Process --the same group that now maintains and standardizes JSP itself --decided to offer a standard tag library. JSTL 1.0, to include tags for the following common tasks:          •Looping over data to produce tables, lists, etc.          •Conditional operations          •Importing and processing data from other web pages         ...