Jdbc RowSet

Jdbc RowSet

import java.sql.*;
import javax.sql.*;
import java.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.*;

public class RowSetTest {
    public static void main(String[] args) throws Exception{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger");
        Statement stmt=con.createStatement(1004,1008);
        ResultSet rs=stmt.executeQuery("select * from emp7");
        JdbcRowSet jrs=new JdbcRowSetImpl(rs);
        jrs.afterLast();
        while (jrs.previous()) {
            System.out.println(jrs.getInt(1)+"\t"+jrs.getString(2)+"\t"+jrs.getString(3));

        }
        jrs.absolute(6);
        jrs.deleteRow();
        System.out.println("6th row deleted");
        rs.close();
        stmt.close();
}
}

D:\>javac RowSetTest.java
RowSetTest.java:13: warning: com.sun.rowset.JdbcRowSetImpl is Sun proprietary AP
I and may be removed in a future release
        JdbcRowSet jrs=new JdbcRowSetImpl(rs);
                           ^
1 warning

D:\>java RowSetTest

D:\>java RowSetTest
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manage
r] Invalid cursor state
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
        at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(JdbcOdbc.java:3811)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(JdbcOdbcResultSet.java
:5638)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:583)
        at com.sun.rowset.JdbcRowSetImpl.getInt(JdbcRowSetImpl.java:946)
        at RowSetTest.main(RowSetTest.java:16)
plz find above problem............
View Answers

January 4, 2013 at 3:32 PM

hi, friend I think you are indicating an invalid row for deleting either the row is not available in the database table or you are putting the insert row. Put the valid row number or any other row number which is available in the table.

Here I am giving a Java code and hope this will helpful for you. In this code I have 4 records in my emp table and found the same error what you have founded, when I have provided the row number 5 to position the cursor. i.e. jdbcRs.absolute(5) and jdbcRs.deleteRow(). And the error will also be occurred when you will provide the insert row number in my example row number 4. So provide the valid row number.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.JdbcRowSet;

import com.sun.rowset.JdbcRowSetImpl;

public class JavaJDBCRowSetExample {

    public static void main(String[] args) throws SQLException {

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:swing");
            String sql = "select * from emp";
            Statement ps = con.createStatement(1004, 1008);
            ResultSet rs = ps.executeQuery(sql);            
            JdbcRowSet jdbcRs = new JdbcRowSetImpl(rs);
            jdbcRs.afterLast();
            System.out.println("Records before deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");               
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);            
            }
            boolean bol = jdbcRs.absolute(3);               
            int i = jdbcRs.getRow();
            System.out.println("Deleting row no. "+i+".....");
            jdbcRs.deleteRow();
            jdbcRs.refreshRow();
            System.out.println("row no. "+i+" is deleted");
            jdbcRs.afterLast();
            System.out.println("Records after deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");               
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);            
            }
            rs.close();
            ps.close();
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}









Related Tutorials/Questions & Answers:
Jdbc RowSet
Jdbc RowSet  import java.sql.*; import javax.sql.*; import...=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger"); Statement stmt..."); Connection con = DriverManager.getConnection("jdbc:odbc:swing
JDBC RowSet Example
; } JDBC RowSet Example JDBC RowSet is an interface of javax.sql.rowset... JDBC connection to the database. Another advantage of JDBC RowSet... the RowSet object are scrollable and updateable. An Example of Row Set Event
Advertisements
what is meant by rowset and process row set
what is meant by rowset and process row set  Hi To all, I am woking on social networking site by using php and mysql.And i am beginner to php When i go through some doc through net i have observed rowset and processrowset in php
jdbc interview question
of columns. JDBC ResultSet Example RowSet: A RowSet object contains a set of rows from.... JDBC RowSet Example ResultSetMetaData: ResultSetMetaData is a class which...what is the difference between resultset and rowset?  what
JDBC
JDBC  why we use batch in jdbc
jdbc
jdbc  display the records using index in jdbc
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
JDBC
JDBC  How to add set of queries in a single query in JDBC
jdbc
jdbc  please tell me sir.i dont know JDBC connection and how to create table in database
jdbc
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc
jdbc  why do we need to load jdbc drivers before connecting to database
JDBC
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
JDBC
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
jdbc - JDBC
Why JDBC   JDBC used for what
JDBC - JDBC
JDBC - limitations of jdbc  What are the limitations of JDBC
jdbc
jdbc define batch updates  define batch updates?exp   JDBC... links: http://www.roseindia.net/jdbc/Jdbc-batch-update.shtml http://www.roseindia.net/tutorial/java/jdbc/batchupdateresultsetexample.html
jdbc
jdbc  Write a web based student registration application where the students can register online with their enrollment no. You are required to use JSP, Servelet and JDBC
JDBC
JDBC save a data in the database  I need a code to save a data... between java and mysql using JDBC and saves the data into the database. import... con = null; String url = "jdbc:mysql://localhost:3306/"; String db
JDBC
JDBC code to save a data in the database  I need a code to save... the connection between java and mysql using JDBC and saves the data into the database...!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
how can we call stored procedure using jdbc  how can we call stored procedure using jdbc   Call a Stored Procedure using...(); For more information, visit the following link: JDBC call Stored Procedure
jdbc - JDBC
Java JDBC application  Database Application in Java JDBC
JDBC - JDBC
JDBC Select Count Example   Need an example of count in JDBC
JDBC - JDBC
JDBC -statement types in jdbc  statement types in jdbc
jdbc - JDBC
Loading JDBC driver at runtime  How to load JDBC Driver at runtime
jdbc
jdbc  hey sir i just wanna have some ppt on jdbc coz have my exams next week and i have not attended any classes coz of job... I m studyng frm niit
jdbc
jdbc  I can't run my jdbc program because it has error in this line: public static void main(String[] args) the error is:illegal static declaration in inner class would you please let me whats the problem? regards
jdbc
using jdbc connection   import java.sql.*; public class CreateTable... = DriverManager.getConnection("jdbc:odbc:student"); Class.forName
jdbc
jdbc  i had written jdbc connection in method and i need to get connection in another class?   import java.sql.*; class... driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test
jdbc
(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost... information, visit the following link: JDBC Tutorials
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
jdbc  how to update int values of ms-access in jdbc program?   In MS Access database, use Number data type for int values. import... =DriverManager.getConnection("jdbc:odbc:access","",""); Statement st=null; st
JDBC
retrieve the value from database into dropdown list using JDBC SQL 2005  How to retrieve the value from database into dropdown list using JDBC &...").newInstance(); String connectionURL = "jdbc:mysql://localhost:3306/test";; Connection
JDBC
JDBC  write a JDBC program to display the result of any query on a student table in a JTable component.   import java.awt....").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost
jdbc
define transactions  define transactions   Whenever a connection is created by using the JDBC, then by default it is in auto- commit mode. This means that SQL statement will be automatically committed immediately after
jdbc
the Connection.setTransactionIsolation(int level) method isolation level can be set in JDBC. the method can
jdbc
oracle.jdbc.driver.OracleDriver()); Connection con=DriverManager.getConnection("jdbc:oracle:thin
jdbc
logical group of data with a number of columns. JDBC ResultSet Example Stored
jdbc
level can be set in JDBC. the method can accept any of the arguments listed below
jdbc
); } try{ Connection con; con=DriverManager.getConnection("jdbc:odbc:student
JDBC
= DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root
jdbc
what are different type of locks  what are different type of locks   Types of locks in JDBC: Row and Key Locks:: It is useful when updating the rows (update, insert or delete operations), as they increase concurrency
jdbc
("jdbc:odbc:student"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver
jdbc - JDBC
JDBC statement example in java  Can anyone explain me ..what is statement in JDBC with an example
jdbc
= DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root
JDBC
("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql
jdbc - JDBC
JDBC Query to Connect Database  JDBC Query to connect to database  will u supply me the block of code where it throws the exception

Ads