santosh_tamse
02-24-2009, 10:59 AM
hi all
thanks for your previous support
i m writing a program in which i hav to read excel file which contains 20,000 records the code which i m sending works for small amount of records i.e for about 1500 for large amount of records it fails. while reading excel file i hav to insert records in access table . the table must contains same records which the excel file is having in the end. so plz validate my code so that it should work for 20,000 records. im great tension. plz help me out. this program is the basic of my project.soplz plz help me out.
code
<%@page language="Java"%>
<%@page import="java.io.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%@page contentType="application/vnd.ms-excel" %>
<%
HSSFRow row;
HSSFCell cell;
Connection con=null;
PreparedStatement stmt;
String parea="",esgrp="",egroup="",personelsub="",halfname="",esubgroup="",costctr="",psubarea="",designation="",position="",name="";
int bc=0,tno=0,eegrp=0,flag=0;
String name1="";
String arr[]=new String[14];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:deployment","","");
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
name1=request.getParameter("file");
out.println("Path="+name1);
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(name1));
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
out.println("Rows are"+rows);
int cols = 0;
int tmp = 0;
for(int i = 0;i < rows; i++)
{
row = sheet.getRow(i);
if(row != null)
{
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols)
cols = tmp;
}
}
out.println("Columns are="+cols);
int m=0;
for(int r = 0; r < rows; r++)
{
row = sheet.getRow(r);
if(row != null)
{
for(int c = 0; c < 15; c++)
{
m=c;
cell = row.getCell((short)c);
if(cell != null)
{
out.println("inserted====="+cell.getStringCellValue());
if(m==1||m==5||m==10)
{
int jk=Integer.parseInt(Double.toString(cell.getNumericCellValue()));
out.println("J&k="+jk);
arr[c]=Integer.toString(jk);
}
else
{
arr[c]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[c]="";
}
}
}//for
stmt=con.prepareStatement("insert into Axlemain values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1,arr[0]);
String n1=arr[1];
int m1=Integer.parseInt(n1);
stmt.setInt(2,m1);
stmt.setString(3,arr[2]);
stmt.setString(4,arr[3]);
stmt.setString(5,arr[4]);
String n2=arr[5];
int m2=Integer.parseInt(n2);
stmt.setInt(6,m2);
stmt.setString(7,arr[6]);
stmt.setString(8,arr[7]);
stmt.setString(9,arr[8]);
String n3=arr[9];
int m3=Integer.parseInt(n3);
stmt.setInt(10,m3);
stmt.setString(11,arr[10]);
stmt.setString(12,arr[11]);
stmt.setString(13,arr[12]);
stmt.setString(14,arr[13]);
int i6=stmt.executeUpdate();
if(i6>0)
{
out.println("<br><h2>record inserted successfully</h2>");
}
}//if
}//for
}
catch(Exception e)
{
%>
<script language="JavaScript">
alert("Error while inserting");
</script>
<%
out.println(e.getMessage());
}
%>
here some fields in excel file are numeric & some are string
1,5,10 numbered fields contains numeric value & rest contains i.e 0,2,3,4 to 15 contains string values
plz validate my code so that it should run according to my requirements. plz send me response as fast as possible
thanks for your previous support
i m writing a program in which i hav to read excel file which contains 20,000 records the code which i m sending works for small amount of records i.e for about 1500 for large amount of records it fails. while reading excel file i hav to insert records in access table . the table must contains same records which the excel file is having in the end. so plz validate my code so that it should work for 20,000 records. im great tension. plz help me out. this program is the basic of my project.soplz plz help me out.
code
<%@page language="Java"%>
<%@page import="java.io.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%@page contentType="application/vnd.ms-excel" %>
<%
HSSFRow row;
HSSFCell cell;
Connection con=null;
PreparedStatement stmt;
String parea="",esgrp="",egroup="",personelsub="",halfname="",esubgroup="",costctr="",psubarea="",designation="",position="",name="";
int bc=0,tno=0,eegrp=0,flag=0;
String name1="";
String arr[]=new String[14];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:deployment","","");
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
name1=request.getParameter("file");
out.println("Path="+name1);
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(name1));
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
out.println("Rows are"+rows);
int cols = 0;
int tmp = 0;
for(int i = 0;i < rows; i++)
{
row = sheet.getRow(i);
if(row != null)
{
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols)
cols = tmp;
}
}
out.println("Columns are="+cols);
int m=0;
for(int r = 0; r < rows; r++)
{
row = sheet.getRow(r);
if(row != null)
{
for(int c = 0; c < 15; c++)
{
m=c;
cell = row.getCell((short)c);
if(cell != null)
{
out.println("inserted====="+cell.getStringCellValue());
if(m==1||m==5||m==10)
{
int jk=Integer.parseInt(Double.toString(cell.getNumericCellValue()));
out.println("J&k="+jk);
arr[c]=Integer.toString(jk);
}
else
{
arr[c]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[c]="";
}
}
}//for
stmt=con.prepareStatement("insert into Axlemain values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1,arr[0]);
String n1=arr[1];
int m1=Integer.parseInt(n1);
stmt.setInt(2,m1);
stmt.setString(3,arr[2]);
stmt.setString(4,arr[3]);
stmt.setString(5,arr[4]);
String n2=arr[5];
int m2=Integer.parseInt(n2);
stmt.setInt(6,m2);
stmt.setString(7,arr[6]);
stmt.setString(8,arr[7]);
stmt.setString(9,arr[8]);
String n3=arr[9];
int m3=Integer.parseInt(n3);
stmt.setInt(10,m3);
stmt.setString(11,arr[10]);
stmt.setString(12,arr[11]);
stmt.setString(13,arr[12]);
stmt.setString(14,arr[13]);
int i6=stmt.executeUpdate();
if(i6>0)
{
out.println("<br><h2>record inserted successfully</h2>");
}
}//if
}//for
}
catch(Exception e)
{
%>
<script language="JavaScript">
alert("Error while inserting");
</script>
<%
out.println(e.getMessage());
}
%>
here some fields in excel file are numeric & some are string
1,5,10 numbered fields contains numeric value & rest contains i.e 0,2,3,4 to 15 contains string values
plz validate my code so that it should run according to my requirements. plz send me response as fast as possible