我需要将数据从excelsheet上传到数据库。
我使用Apache POI概念来实现此功能。
try {
Workbook wb = new HSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0); // first sheet
List excelData = new ArrayList();
System.out.println("sheet.getFirstRowNum() "+sheet.getFirstRowNum()+" sheet.getLastRowNum() "+sheet.getLastRowNum());
int rowStart = Math.min(1, sheet.getFirstRowNum());
int rowEnd = Math.max(10, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
if (r != null) {
int lastColumn = Math.max(r.getLastCellNum(), 22);
List cellData = new ArrayList();
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
cellData.add(c);
} else {
cellData.add(c);
}
}
excelData.add(cellData);
}
}
for (int rowNum = 1; rowNum < excelData.size(); rowNum++) {
/*provide hssfEmployeeClass variables as null*/
List list = (List) excelData.get(rowNum);
HSSFCell[] EmployeeListCell = new HSSFCell[list.size()];
for (int i = 0; i < list.size(); i++) {
try {
EmployeeListCell[i] = (HSSFCell) list.get(i);
//EmployeeListCell.set(i, ((HSSFCell) list.get(i)).toString());
} catch (NullPointerException e) {
EmployeeListCell[i] = (HSSFCell) null;
} catch (IndexOutOfBoundsException e) {
EmployeeListCell[i] = (HSSFCell) null;
}
}
ArrayList EmployeeData = new ArrayList(list.size());
long temp;
java.sql.Date[] temp_date = new java.sql.Date[10];
for (int i = 0; i < (EmployeeListCell.length-1); i++) {
if(EmployeeListCell[i]==null)
{}
else if (EmployeeListCell[i].getCellType() == Cell.CELL_TYPE_FORMULA) {
switch (EmployeeListCell[i].getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(EmployeeListCell[i])) {
EmployeeData.add(i,new java.sql.Date(EmployeeListCell[i].getDateCellValue().getTime()));
} else {
temp = (int) EmployeeListCell[i].getNumericCellValue();
//EmployeeData[i] = (Long.toString(temp));
EmployeeData.add(i,(Long.toString(temp)));
}
break;
case Cell.CELL_TYPE_STRING:
EmployeeData.add(i,EmployeeListCell[i].getStringCellValue());
break;
}
} else if (EmployeeListCell[i] != null) {
if (EmployeeListCell[i].getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(EmployeeListCell[i])) {
// EmployeeData[i] = (java.sql.Date) EmployeeListCell[i].getDateCellValue();
EmployeeData.add(i,new java.sql.Date(EmployeeListCell[i].getDateCellValue().getTime()));
}
else
{
temp = (int) EmployeeListCell[i].getNumericCellValue();
EmployeeData.add(i,(Long.toString(temp)));
}
}
if (EmployeeListCell[i].getCellType() == HSSFCell.CELL_TYPE_STRING) {
System.out.println("Ith position "+i);
EmployeeData.add(i,EmployeeListCell[i].getStringCellValue());
}
}
}
for(int i=0;i<EmployeeData.size();i++)
{
System.out.println("Employee data values"+ i + EmployeeData.get(i));
}
app=getApp();
System.out.println("Return val is " + app.insertData(rowNum, EmployeeData));
}
} catch (Exception e) {
e.printStackTrace();
}
}
它将从Employee Data Array列表中的excel表加载数据。
有一个java类,创建了EmployeeObj,它实现了sqlData以通过可调用的Statement传递数据。
public class EmployeeObj implements SQLData,Serializable {
public long EMP_ID;
public String FIRST_NM;
public String MIDDLE_NM;
public String DOJ;
public String GENDER;
public String STATUS;
public String REPORTING_MGR;
public String LAST_NM;
public String EMAIL_ID;
public String STREAM_ID;
public String DESIGN_ID;
public String EMP_LOCATION;
public String GEO_CODE;
public String EMP_NO;
public String LEVEL_2_MGR;
public String TENTATIVE_LAST_DAY;
private String sql_type="EMP_DATA_OBJ";
static int count=0;
public EmployeeObj() {
super();
}
public EmployeeObj(String sql_type,Object[] empObj)
{
this.sql_type = sql_type;
count=count+1;
System.out.println("empObj[1].toString(); "+ empObj[1].toString());
this.EMP_NO=empObj[1].toString();
this.EMP_ID=Long.parseLong(empObj[2].toString());
this.FIRST_NM=empObj[3].toString();
this.LAST_NM=empObj[4].toString();
this.DOJ=empObj[5].toString();
this.GENDER=empObj[6].toString();
this.STATUS=empObj[7].toString();
this.REPORTING_MGR=empObj[9].toString();
this.LEVEL_2_MGR=empObj[11].toString();
this.EMAIL_ID=empObj[12].toString();
this.STREAM_ID=empObj[13].toString();
this.DESIGN_ID=empObj[14].toString();
this.EMP_LOCATION=empObj[15].toString();
this.GEO_CODE=empObj[16].toString();
try
{
this.TENTATIVE_LAST_DAY=empObj[17].toString();
}
catch(NumberFormatException n)
{
System.out.println("Not a number in employee obj");
}
}
@Override
public String getSQLTypeName() throws SQLException {
// TODO Implement this method
return sql_type;
}
@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
// TODO Implement this method
sql_type=typeName;
this.EMP_ID=stream.readLong();
this.FIRST_NM=stream.readString();
//this.MIDDLE_NM=stream.readString();
this.DOJ=stream.readString();
this.GENDER=stream.readString();
this.STATUS=stream.readString();
this.REPORTING_MGR=stream.readString();
this.LAST_NM=stream.readString();
this.EMAIL_ID=stream.readString();
this.STREAM_ID=stream.readString();
this.DESIGN_ID=stream.readString();
this.EMP_LOCATION=stream.readString();
this.GEO_CODE=stream.readString();
this.EMP_NO=stream.readString();
this.LEVEL_2_MGR=stream.readString();
this.TENTATIVE_LAST_DAY=stream.readString();
}
@Override
public void writeSQL(SQLOutput stream) throws SQLException {
// TODO Implement this method
stream.writeLong(EMP_ID);
stream.writeString(FIRST_NM);
//stream.writeString(MIDDLE_NM);
stream.writeString(DOJ);
stream.writeString(GENDER);
stream.writeString(STATUS);
stream.writeString(REPORTING_MGR);
System.out.println("write "+REPORTING_MGR);
stream.writeString(LAST_NM);
stream.writeString(EMAIL_ID);
stream.writeString(STREAM_ID);
stream.writeString(DESIGN_ID);
stream.writeString(EMP_LOCATION);
stream.writeString(GEO_CODE);
stream.writeString(EMP_NO);
stream.writeString(LEVEL_2_MGR);
stream.writeString(TENTATIVE_LAST_DAY);
}
}
CallableStatement pstmt =
getDBTransaction().createCallableStatement("begin ?:= emp_dml.Main(?,EMP_OBJ_DT_ARR(?),?); end;",
0);
EmployeeObj eob=null;
Object[] obj = EmployeeD.toArray(new Object[EmployeeD.size()]);
for(int i=0;i<EmployeeD.size();i++)
{
obj[i]=EmployeeD.get(i).toString();
System.out.println("i "+i+" obj[i] "+obj[i]);
}
eob=new EmployeeObj("EMP_DATA_OBJ",obj);
pstmt.setInt(2,row_id);
pstmt.setObject(3, (Object) eob);
pstmt.setString(4,user);
pstmt.registerOutParameter(1, Types.BIGINT);
try
{
pstmt.executeUpdate();
}
catch(SQLException e1)
{
System.out.println("ExecuteUpdate number format exception");
e1.printStackTrace();
}
System.out.println("After Update app insert");
dbTransaction.commit();
}
在pl / sql中为EmployeeData创建的类型是:
create or replace type EMP_OBJ_DT_ARR as table of EMP_DATA_OBJ;
create or replace TYPE EMP_DATA_OBJ AS OBJECT
(
EMP_NO VARCHAR2(10),
EMP_ID NUMBER,
LAST_NM VARCHAR2(100),
FIRST_NM VARCHAR2(100),
DOJ VARCHAR2(100),
GENDER VARCHAR2(50),
STATUS VARCHAR2(100),
REPORTING_MGR NUMBER,
LEVEL_2_MGR NUMBER,
EMAIL_ID VARCHAR2(200),
STREAM_ID VARCHAR2(100),
DESIGN_ID VARCHAR2(100),
EMP_LOCATION VARCHAR2(100),
GEO_CODE VARCHAR2(100),
TENTATIVE_LAST_DAY CHAR(8));
每当我执行代码时。我得到例外
java.sql.SQLException: setObject, Exception = For input string: "Pr"
at weblogic.jdbc.wrapper.JDBCWrapperImpl.invocationExceptionHandler(JDBCWrapperImpl.java:142)
at weblogic.jdbc.wrapper.PreparedStatement.setObject(PreparedStatement.java:378)
at model.AppModuleImpl.insertData(AppModuleImpl.java:940)
at view.Parse_employee.upload(Parse_employee.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:46)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)
at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
at oracle.adf.view.rich.event.ProxyEvent.broadcastWrappedEvent(ProxyEvent.java:72)
at oracle.adf.view.rich.component.fragment.UIXRegion.broadcast(UIXRegion.java:124)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._invokeApplication(LifecycleImpl.java:1074)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:402)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:225)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:192)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:105)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:502)
at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:502)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:327)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:229)
at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.adf.library.webapp.LibraryFilter.doFilter(LibraryFilter.java:202)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:137)
at java.security.AccessController.doPrivileged(Native Method)
at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:460)
at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:120)
at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:217)
at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:81)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:220)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
Caused by: java.lang.NumberFormatException: For input string: "Pr"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at oracle.sql.NUMBER.toBytes(NUMBER.java:1917)
at oracle.sql.NUMBER.stringToBytes(NUMBER.java:3448)
at oracle.sql.NUMBER.<init>(NUMBER.java:317)
at oracle.jdbc.oracore.OracleTypeNUMBER.toNUMBER(OracleTypeNUMBER.java:285)
at oracle.jdbc.oracore.OracleTypeNUMBER.toDatum(OracleTypeNUMBER.java:60)
at oracle.jdbc.oracore.OracleType.toDatumInternal(OracleType.java:149)
at oracle.sql.StructDescriptor.toOracleArray(StructDescriptor.java:945)
at oracle.sql.StructDescriptor.toArray(StructDescriptor.java:1930)
at oracle.sql.STRUCT.<init>(STRUCT.java:165)
at oracle.sql.OracleSQLOutput.getSTRUCT(OracleSQLOutput.java:122)
at oracle.sql.STRUCT.toSTRUCT(STRUCT.java:875)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:13219)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:12597)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:13744)
at oracle.jdbc.driver.OracleCallableStatement.setObject(OracleCallableStatement.java:11812)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:260)
at weblogic.jdbc.wrapper.PreparedStatement.setObject(PreparedStatement.java:363)
... 62 more
这里的名字我得到数字格式异常。我没有以任何数字格式转换它。所以,请建议我。
答案 0 :(得分:0)
我很少理解为什么我会收到numberFormatException。
因为我开发了实现SqlData的java类。
SqlData有2个方法需要在类中实现:
readSQL(SQLInput stream, String typeName)
writeSQL(SQLOutput stream)
这里writeSQL将数据作为流。所以无论我将通过什么,它都将附加到流。 假设我将数据传递为
id name age
1 xyz 26
java中的数据类型:
id - Integer
name - String
age - Integer
在Pl / sql中,数据类型是
id -number
name -varchar2
age -number
如果我将尝试按名称,身份和年龄的顺序检索数据(例如:xyz 1 26) 然后它将使用sqlException抛出numberFormatException。所以需要传递我们所拥有的相同顺序。