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

Java Server page tutorial part 3: JSP Life Cycle

JSP Life Cycle: A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet. Following are the steps followed by a JSP Container: ◦ Compilation  ◦ Parsing the JSP.  ◦ Turning the JSP into a servlet.  ◦ Compiling  the servlet.  ◦ Initialization ◦ Execution ◦ Cleanup Architecture: 1.       JSP Compilation: When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page.  If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves three steps: ◦ Parsing the JSP. ◦ Turning the JSP into a servlet.  ◦ Compiling the servlet.  2.       JSP Initialization: When a container loads a JSP it invokes the jspInit() method before servicing any requests.  If you need to perform

OPERATION MANUAL FOR RS232 TO RS422/RS485 VICE-VERSA

INTRODUCTION: Milestone model LD-15U is a RS232,  to RS422/RS485 converter is designed for high-speed data transmission between computer system and or peripherals over Iong distance under high noise conditions. They provide dual line interface per signal. APPLICATIONS: Application for these converters can be for factory automation, Programmable logic controllers,, attendance recording systems, Barcode Readers, remote data transmission, remote terminals, EPABX etc. SPECIFICATIONS: Input: RS232-T×D, R×D, DSR (for control), GND (D9 Female connector) Output: RS422/RS485-Tx+, Tx-, Rx+. Rx-(D9 Male Connector). Each signal is protected by spike suppressor, fuse and has opto-isolation. Selection Switch: 1. Rear side has 2-way'SELECT' switch for selecting 2-Wire or 4-Wire mode in RS422/RS485 application. 2. (Optional) Rear side has 3-way'SELECT' switch for selecting AUTO Mode (No Handshake Signal), DSR+ Mode (+12V Control) and DSR-Model (-_12V Control) for co

JSTL-JSP Standard Tag Library part 1

Evolution of JSTL: Java is a flexible, general-purpose programming language, JavaServer pages(JSP) depends on java but hides some of the hard details of writing full- fledged programs.  The JSP Standards Tag Library(JSTL) builds on top of JSP, making it even easier to use. Architecture : In this figure large web applications are designed using java, JSTL, and othe components like databases. In large applications, it's common for requests from web browser to be handled by a java program called a servlet, which interacts with databases and other Java code on the server. The servlet figures out how it wants a response be printed and then forwards the user to the right JSTL page, which takes care of nothing more than presenting information. 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 c