您的位置:首页 > 数据库

如何使用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);}}}
求各位大神指点 指出哪里不恰当之处

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐