如何使用JDBC+Struct2框架的poi读取Excel的数据然后插入Sql Server数据库中
2014-01-23 14:46
525 查看
Action层 package org.cupd.spdb.report.importexcel.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.cupd.spdb.report.importexcel.service.CardBinService; import org.cupd.spdb.report.importexcel.service.impl.CardBinServiceImpl; /** * * @author * @date 2014-01-21 * excel数据读取与导入后台管理 */ public class CardBinAction extends DispatchAction { public ActionForward showCardBinPage(ActionMapping mapping, ActionForm actionform, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("showCardBinPage"); } private CardBinService CardBinService=new CardBinServiceImpl(); public int insertCardBin() { int result=CardBinService.insertDB(); return result; }
service层
package org.cupd.spdb.report.importexcel.service.impl;import java.util.List;import org.cupd.spdb.report.importexcel.dao.CardBinDao;import org.cupd.spdb.report.importexcel.dao.impl.CardBinDaoImpl;import org.cupd.spdb.report.importexcel.entity.BizCardBin;import org.cupd.spdb.report.importexcel.service.CardBinService;public class CardBinServiceImpl implements CardBinService {private CardBinDao cardBinDao=new CardBinDaoImpl();public CardBinDao getCardBinDao() {return cardBinDao;}public void setCardBinDao(CardBinDao cardBinDao) {this.cardBinDao = cardBinDao;}public List<BizCardBin> getBeanList() {return beanList;}public void setBeanList(List<BizCardBin> beanList) {this.beanList = beanList;}private List<BizCardBin> beanList;public int insertDB(){int flag=0;try {cardBinDao.insertDB();flag=1;} catch (Exception e) {e.printStackTrace();}return flag;}}
Impl实现层
package org.cupd.spdb.report.importexcel.dao.impl;
import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.cupd.spdb.report.importexcel.dao.CardBinDao;import com.websurf.spdb.bo.DBConnBOImpl;public class CardBinDaoImpl implements CardBinDao {private static final Log log=LogFactory.getLog(CardBinDaoImpl.class);private static DBConnBOImpl dbConn = new DBConnBOImpl();PreparedStatement ps = null;Connection conn = null;ResultSet rs = null;private String filePath="E:\\Microsoft Excel.xlsx";/*** 插入数据*/public boolean insertDB(){boolean flag=true;try {//文件流指向excel文件// FileInputStream fin=new FileInputStream(filePath);XSSFWorkbook workbook=new XSSFWorkbook(filePath);//创建工作薄XSSFSheet sheet=workbook.getSheetAt(0);//得到工作表XSSFRow row=null;//对应excel的行XSSFCell cell=null;//对应excel的列int totalRow=sheet.getLastRowNum();//得到excel的总记录条数log.info("得到excel的总记录数"+totalRow);//以下的字段一一对应数据库表的字段String cardName="";String cardType="";String cardBin="";String cardLength="";String activityCode="";String note="";String sql ="insert into BizCardBin(cardName,cardType,cardBin,cardLength,activityCode,note) values(?,?,?,?,?,?)";for(int i=2;i<=totalRow;i++){row=sheet.getRow(i);cell=row.getCell((short) 0);cardName=cell.getStringCellValue().toString();cell=row.getCell((short) 1);cardType=cell.getStringCellValue().toString();cell=row.getCell((short) 2);cardBin=cell.getStringCellValue().toString();cell=row.getCell((short) 3);cardLength=cell.getStringCellValue().toString();cell=row.getCell((short) 4);activityCode=cell.getStringCellValue().toString();cell=row.getCell((short) 5);note=cell.getStringCellValue().toString();conn=dbConn.getConnection();log.info("获取JDBC连接完成");ps=conn.prepareStatement(sql);ps.setString(1,cardName);ps.setString(2,cardType);ps.setString(3,cardBin);ps.setString(4,cardLength);ps.setString(5,activityCode);ps.setString(6,note);ps.execute();System.out.println("preparestatement successful");}dbConnClose(conn, dbConn);psClose(ps, rs);} catch (FileNotFoundException e) {flag=false;e.printStackTrace();} catch(IOException ex){flag=false;ex.printStackTrace();} catch(SQLException exx){flag=false;exx.printStackTrace();}return flag;}public static void main(String[] args) {CardBinDaoImpl e=new CardBinDaoImpl();e.insertDB();}/*** 插入数据 只需要传入插入sql即可* 插入sql的样例:insert into t_department values('D004','金融部');* @param insert 插入语句* @return* @throws SQLException*//*public int insertCardBin(BizCardBin cardBin){int result=0;PreparedStatement ps = null;Connection conn = null;String sql ="insert into BizCardBin(cardName,cardTyoe,cardBin,cardLength,cardLength,activityCode,note) values(?,?,?,?,?,?,?)";try {conn=dbConn.getConnection();log.info("获取JDBC连接完成");ps = conn.prepareStatement(sql);ps.setString(1, cardBin.getCardName());ps.setString(2, cardBin.getCardType());ps.setString(3, cardBin.getCardBin());ps.setString(4, cardBin.getCardLength());ps.setString(5, cardBin.getActivityCode());ps.setString(6, cardBin.getNote());result = ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocklog.error("SQL Exception "+ e);e.printStackTrace();return 0;}finally{dbConnClose(conn, dbConn);psClose(ps, rs);}return result;}*/public void dbConnClose(Connection conn,DBConnBOImpl dbConn){if(conn!=null){dbConn.close(conn);}}private static void psClose(final PreparedStatement preparedStatement,final ResultSet resultSet) {try {if (resultSet != null) {resultSet.close();}if (preparedStatement != null) {preparedStatement.close();}} catch (SQLException e) {log.error("error", e);}}}
求各位大神指点 指出哪里不恰当之处
相关文章推荐
- 如何使用c#读取Excel数据?
- java中使用POI导入excel数据的过程中,遇到读取以科学计数法显示的数据
- 复制文件时,如何显示进度条(使用BlockRead函数读取数据,并插入application.ProcessMessages)
- 如何创建和读取excel文件-poi的简单使用
- POI使用案例01---读取excel的数据
- java 使用POI读取excel数据
- 使用 POI 读取 Excel 表格数据
- POI读取excel2007文件的数据,插入数据库的使用方法【经验之谈】
- 如何使用C#和ADO.NET在SQL Server数据库读取和写入blob数据
- poi 学习之使用 Apche poi 读取数据库中的数据并写入Excel
- 使用POI读取Excel时如何把数字转换成字符串
- 使用POI 读取Excel文件数据
- 如何使用POI处理Excel中的日期数据类型
- 从txt文件逐行读取数据到excel文件(包含POI的简单使用和excel文件的数据分列,出现次数统计)
- 使用poi读取Excel中的各种数据类型处理
- ZK框架简介,以及在ZK下使用JXL读取Excel模板,导出数据模型到Excel的方法
- ssm框架使用poi实现数据导出到excel,并弹框下载
- java使用poi读取excel数据(xlsx)
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 给予POI机制的把Excel文件导入Oracle,数据全部读入list,然后遍历list插入oracle