Thursday, 5 December 2013

RowSets

•       We can send only those java objects over the network that are Serializable.
•       In order to make object as Serializable object, the class of the object must implement java.io.Serializable interface.
•       In order to store data of the object in a file, the object must be serializable object.
•       ResultSet object is not Serializable object, so we can not send this object over the network directly.
•       To overcome this problem, there are two solutions
•       1. Use RowSets in the place of ResultSets.
•       2. copy data of ResultSet object to RowSet
•       Rowsets are Serializable objects and given as enhancement of ResultSet
•       All collection framework dataStructures are Serializable objects by default.
•       RowSet object means it is the object of a class that implements javax.sql.RowSet interface
•       RowSets are introduced to JDBC from JDBC 2.0
TYPES OF ROWSETS
•       There are 3 types of RowSets
•       1. Cached RowSet
•       Disconnected Rowset, like insensitive ResultSet.
•       2.JDBC Rowset: like sensitive ResultSet
•       3.Web Rowset: connected RowSet->like Sensitive ResultSet, userful to store xml data collected from the table
•       Cached RowSet, JDBC RowSet can not deal with xml data as column values. But web RowSet can do this work.
•       The oracle thin driver, OCI driver is giving support for two types of RowSets. (catched RowSet, JDBC RowSet).
•       The oracle corporation supplies two classes having the functionality of cached, JDBC RowSet.
•       They are :
•       1. oracle.jdbc.RowSet.OracleCachedRowSet.
•       2.
•       These two classes are there oracle-home\ora92\jdbc\lib\ocrs12.jar file
•       So we need to add it in classpath.
•       If one jar file classes are using the classes of other jar files then other jar files are called dependent jar files to main jar file.
•       When java application is using classes of main jar file and these classes are having dependent classes in other jar files, then we need to execute both main and dependent jar files in the classpath.
•       Ojdbc14.jar file is not having dependent jar files. But this ojdbc14.jar file is the dependent jar file for ocrs12.jar file.
•       In order to work with Oracle corporation supplied RowSets, we need to add ocrs12.jar file, ojdbc14.jar files in claspath environment variable.
Advantages of RowSets
•       1. RowSet  object are Serializable objects. So we can send them directly over the network.
•       2. we can perform select operations on the table by simply working with setXXX(), getXXX() methods and there is no need of working with the regular JDBC objects like Connection, ResultSet etc.
Limitations of RowSets
•       1. RowSets are given only for select operations. So non-select operations are not possible.
RowSet example program
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);
        }
    }
}

Wednesday, 7 August 2013

How to start Oracle10g

How to start Oracle10g?
steps:

start->programs->Oracle Database 10g Express Edition -> Run SQL command


  • It opens a window at DOS prompt where SQL> prompt appears. 
  • type connect and, it asks to enter user-name and password.



What is Database?

What is Database?
A Database is a repository of data. We can store data permanently in a database and retrieve it later whenever needed by using query commands.

List of Databases:
  1. Oracle
  2. Sybase
  3. MySQL
  4. SQL Server, and so..on


  • Data is stored in the form of tables in the database.
  • to get the data from the tables and handover to the users, we have to write programs. This programs are known as 'database client' program.
Database servers


Saturday, 1 June 2013

jdbc interview questions


4.       List out the JSP tags?
5.      

6.       Hibernate relation ships?
1.       Statements in JDBC
2.       Who will do the designing part?

Friday, 3 June 2011

Write a java program which illustrates the concept of updatable ResultSet?

Write a java program which illustrates the concept of updatable ResultSet?
import java.sql.*;
class UpdateResultSet {
 public static void main(String[] args) {
   try {
    Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = st.executeQuery("select * from emp1");
    rs.next();
    rs.updateInt(2, 8000);
    rs.updateRow();
    System.out.println("1 ROW UPDATED...");
    rs.moveToInsertRow();
    rs.updateInt(1, 104);
    rs.updateInt(2, 2000);
    rs.insertRow();
    System.out.println("1 ROW INSERTED...");
    rs.absolute(2);
    rs.deleteRow();
    System.out.println("1 ROW DELETED...");
    con.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } // main
}   // UpdateResultSet

Write a java program which illustrates the concept of Batch processing?

Write a java program which illustrates the concept of Batch processing?
import java.sql.*;
class BatchProConcept {
 public static void main(String[] args) throws Exception {
   Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
   System.out.println("DRIVERS LOADED...");
   Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
   System.out.println("CONNECTION ESTABLISHED...");
   con.setAutoCommit(false);
   Statement st = con.createStatement();
   st.addBatch("insert into student values (3, 'j2ee')");
   st.addBatch("delete from student where sno=1");
   st.addBatch("update student set sname='java' where sno=2");
   int res[] = st.executeBatch();
   for (int i = 0; i < res.length; i++) {
    System.out.println("NUMBER OF ROWS EFFECTED : " + res[i]);
   }
   con.commit();
   con.rollback();
   con.close();
  } // main
}   // BatchProConcept

Write a java program to create a table through frontend application?

Write a java program to create a table through frontend application?
import java.sql.*;
class CreateTable {
 public static void main(String[] args) {
   try {
    Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement();
    int i = st.executeUpdate("create table kalyan (eno number (4), ename varchar2 (15))");
    System.out.println("TABLE CREATED...");
    con.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } // main
}  // CreateTable