Monday, 20 June 2016

JDBC PROGRAM TO WORK WITH CachedRowSet

JDBC PROGRAM TO WORK WITH  CachedRowSet

import com.sun.rowset.*;

import java.io.*;

class ChacheRowSetImpl

{

public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");

CachedRowSetImpl cr=new CachedRowSetImpl();

cr.setUsername("root");
cr.setPassword("root");
cr.setUrl("jdbc:mysql://localhost:3306/voidmain");

cr.setCommand("select * from account");

cr.execute();

FileOutputStream fos=new FileOutputStream("D:\\cached.txt");

ObjectOutputStream os=new ObjectOutputStream(fos);

os.writeObject(cr);

}
}

=======================================================================

import java.io.*;
import com.sun.rowset.*;

class ReadChache
{
public static void main(String[] args) throws Exception
{
FileInputStream fis=new FileInputStream("D:\\cached.txt");

ObjectInputStream ois=new ObjectInputStream(fis);

CachedRowSetImpl cr=(CachedRowSetImpl)ois.readObject();

while(cr.next())
{
System.out.println(cr.getInt(1)+"\t"+cr.getString(2)+cr.getString(3));
}
}
}

Thursday, 16 June 2016

JAVA PROGRAM TO READ DATA FROM EXCEL FILE AND INSERTING INTO DATA BASE


public static void uploadExcelFile(String fileName)
{
ArrayList dataHolder = new ArrayList();
try
{
XSSFWorkbook workbook=new XSSFWorkbook(fileName);

/** Get the first sheet from workbook**/
XSSFSheet mySheet = workbook.getSheetAt(0);
                        /** We now need something to iterate through the cells.**/
Iterator rowIter = mySheet.rowIterator();

while (rowIter.hasNext())
{
XSSFRow myRow = (XSSFRow) rowIter.next();
                                 Iterator cellIter = myRow.cellIterator();
ArrayList cellStoreArrayList = new ArrayList();
                                while (cellIter.hasNext())
{
XSSFCell myCell = (XSSFCell) cellIter.next();
cellStoreArrayList.add(myCell);
}
                                dataHolder.add(cellStoreArrayList);
}
catch (Exception e) 
{
e.printStackTrace();
}

try 
{
Class.forName("Data Base Driver Class");

                Connection con =null;
con=DriverManager.getConnection("URL","DB User Name","DB Password");
ArrayList cellStoreArrayList = null;

PreparedStatement ps= null;
                        ps=con.prepareStatement("insert into registration values(?,?,?,?)");
                        for (i= 1; i < dataHolder.size(); i++) 
                       {
ps.setString(1,((XSSFCell) cellStoreArrayList.get(0)).toString());
ps.setString(2,((XSSFCell) cellStoreArrayList.get(1)).toString());
                                ps.setString(3,((XSSFCell) cellStoreArrayList.get(2)).toString());
        ps.setString(4,((XSSFCell) cellStoreArrayList.get(3)).toString());
   
ps.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
}

JDBC PROGRAM TO WORKING WITH WEBROWSET

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

class WebRowSetExample
{
public static void main(String[] args) throws Exception
{
WebRowSet wrs=new WebRowSetImpl();

Class.forName("oracle.jdbc.driver.OracleDriver");

wrs.setCommand("select * from registration");
wrs.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
wrs.setUsername("cloud");
wrs.setPassword("cloud");
wrs.execute();

FileWriter fw=new FileWriter("D:\\oracle.xml");

wrs.writeXml(fw);
}
}