我的数据库用一个excel文件,采用odbc-DSN数据源方式进行连接。
我的应用是用户在客户端上传一个excel文件,在服务端接收这个文件,并用poi读取该文件,最后把取得的数据插入(更新)到数据库中,采用PreparedStatement语句进行操作,部份代码如下:
<%
if(!DBcon.openConnection("ty") || !boolunload){
out.print("数据库连接失败!");
response.sendRedirect("errorpage.jsp");
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(dir));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
String cellvalue="";Connection conn =DBcon.getconn();updatesql="UPDATE " + sheetname+ " SET "+xueke+ "= ? " + " WHERE 准考证号= ?";
PreparedStatement prepstmt = conn.prepareStatement(updatesql); out.println(updatesql);
//下面把从上传的excel中读取的数据插入到数据库中
try{
colj=sheet.getRow(0).getPhysicalNumberOfCells();
for(rowi=1;rowi<sheet.getPhysicalNumberOfRows();rowi++){
row = sheet.getRow(rowi);
cell = row.getCell((short)(colj-1));
if(cell == null){
//prepstmt.setString( 1, "0.0");
//cell = row.getCell((short)0);
// prepstmt.setString( 2, cell.getStringCellValue());
// prepstmt.executeUpdate();
continue;
}else{
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
cellvalue = ""+cell.getNumericCellValue();
break; case HSSFCell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
cellvalue="0";
break;
default:
}
prepstmt.setString( 1, cellvalue);
cell = row.getCell((short)0);
prepstmt.setString( 2, cell.getStringCellValue());
//prepstmt.executeUpdate();
prepstmt.addBatch();
}
//prepstmt.clearParameters();
}
prepstmt.executeBatch();
}catch(Exception e){
response.sendRedirect("errorpage.jsp");
}finally{
if(prepstmt!=null){
prepstmt.close();
}
if(DBcon!=null){
DBcon.close();
}
}
%>现在的问题是插入没报错,在JSP页面中也能显示插入后数据,但在在我关掉tomcat后,打开数据库(一个excel文件)后,却并没有看到插入的数据,不知是何原因?各位看看,是何原因,谢谢!
我的应用是用户在客户端上传一个excel文件,在服务端接收这个文件,并用poi读取该文件,最后把取得的数据插入(更新)到数据库中,采用PreparedStatement语句进行操作,部份代码如下:
<%
if(!DBcon.openConnection("ty") || !boolunload){
out.print("数据库连接失败!");
response.sendRedirect("errorpage.jsp");
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(dir));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
String cellvalue="";Connection conn =DBcon.getconn();updatesql="UPDATE " + sheetname+ " SET "+xueke+ "= ? " + " WHERE 准考证号= ?";
PreparedStatement prepstmt = conn.prepareStatement(updatesql); out.println(updatesql);
//下面把从上传的excel中读取的数据插入到数据库中
try{
colj=sheet.getRow(0).getPhysicalNumberOfCells();
for(rowi=1;rowi<sheet.getPhysicalNumberOfRows();rowi++){
row = sheet.getRow(rowi);
cell = row.getCell((short)(colj-1));
if(cell == null){
//prepstmt.setString( 1, "0.0");
//cell = row.getCell((short)0);
// prepstmt.setString( 2, cell.getStringCellValue());
// prepstmt.executeUpdate();
continue;
}else{
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
cellvalue = ""+cell.getNumericCellValue();
break; case HSSFCell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
cellvalue="0";
break;
default:
}
prepstmt.setString( 1, cellvalue);
cell = row.getCell((short)0);
prepstmt.setString( 2, cell.getStringCellValue());
//prepstmt.executeUpdate();
prepstmt.addBatch();
}
//prepstmt.clearParameters();
}
prepstmt.executeBatch();
}catch(Exception e){
response.sendRedirect("errorpage.jsp");
}finally{
if(prepstmt!=null){
prepstmt.close();
}
if(DBcon!=null){
DBcon.close();
}
}
%>现在的问题是插入没报错,在JSP页面中也能显示插入后数据,但在在我关掉tomcat后,打开数据库(一个excel文件)后,却并没有看到插入的数据,不知是何原因?各位看看,是何原因,谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货