|
001 |
JDBC+Struct2框架 使用poi读取Excel文件的数据 插入到数据库中 希望大家能给点指点 Impl实现层 |
| 002 |
| 003 |
| 004 | package org.cupd.spdb.report.importexcel.dao.impl; |
| 005 | import java.io.FileNotFoundException; |
| 006 | import java.io.IOException; |
| 007 | import java.sql.Connection; |
| 008 | import java.sql.PreparedStatement; |
| 009 | import java.sql.ResultSet; |
| 010 | import java.sql.SQLException; |
| 011 | import org.apache.commons.logging.Log; |
| 012 | import org.apache.commons.logging.LogFactory; |
| 013 | import org.apache.poi.xssf.usermodel.XSSFCell; |
| 014 | import org.apache.poi.xssf.usermodel.XSSFRow; |
| 015 | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| 016 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 017 | import org.cupd.spdb.report.importexcel.dao.CardBinDao; |
| 018 | import com.websurf.spdb.bo.DBConnBOImpl; |
| 019 | public class CardBinDaoImpl implements CardBinDao { |
| 020 |
| 021 | private static final Log log=LogFactory.getLog(CardBinDaoImpl.class); |
| 022 | private static DBConnBOImpl dbConn = new DBConnBOImpl(); |
| 023 | PreparedStatement ps = null; |
| 024 | Connection conn = null; |
| 025 | ResultSet rs = null; |
| 026 | private String filePath="E:\\Microsoft Excel.xlsx"; |
| 027 |
| 028 | /** |
| 029 | * 插入数据 |
| 030 | */ |
| 031 | public boolean insertDB(){ |
| 032 | boolean flag=true; |
| 033 | try { |
| 034 | //文件流指向excel文件 |
| 035 | // FileInputStream fin=new FileInputStream(filePath); |
| 036 | XSSFWorkbook workbook=new XSSFWorkbook(filePath);//创建工作薄 |
| 037 | XSSFSheet sheet=workbook.getSheetAt(0);//得到工作表 |
| 038 | XSSFRow row=null;//对应excel的行 |
| 039 | XSSFCell cell=null;//对应excel的列 |
| 040 |
| 041 | int totalRow=sheet.getLastRowNum();//得到excel的总记录条数 |
| 042 | log.info("得到excel的总记录数"+totalRow); |
| 043 | //以下的字段一一对应数据库表的字段 |
| 044 | String cardName=""; |
| 045 | String cardType=""; |
| 046 | String cardBin=""; |
| 047 | String cardLength=""; |
| 048 | String activityCode=""; |
| 049 | String note=""; |
| 050 | String sql ="insert into BizCardBin(cardName,cardType,cardBin,cardLength,activityCode,note) values(?,?,?,?,?,?)"; |
| 051 | for(int i=2;i<=totalRow;i++){ |
| 052 | row=sheet.getRow(i); |
| 053 | cell=row.getCell((short) 0); |
| 054 | cardName=cell.getStringCellValue().toString(); |
| 055 | cell=row.getCell((short) 1); |
| 056 | cardType=cell.getStringCellValue().toString(); |
| 057 | cell=row.getCell((short) 2); |
| 058 | cardBin=cell.getStringCellValue().toString(); |
| 059 | cell=row.getCell((short) 3); |
| 060 | cardLength=cell.getStringCellValue().toString(); |
| 061 | cell=row.getCell((short) 4); |
| 062 | activityCode=cell.getStringCellValue().toString(); |
| 063 | cell=row.getCell((short) 5); |
| 064 | note=cell.getStringCellValue().toString(); |
| 065 | conn=dbConn.getConnection(); |
| 066 | log.info("获取JDBC连接完成"); |
| 067 | ps=conn.prepareStatement(sql); |
| 068 | ps.setString(1,cardName); |
| 069 | ps.setString(2,cardType); |
| 070 | ps.setString(3,cardBin); |
| 071 | ps.setString(4,cardLength); |
| 072 | ps.setString(5,activityCode); |
| 073 | ps.setString(6,note); |
| 074 |
| 075 | ps.execute(); |
| 076 | System.out.println("preparestatement successful"); |
| 077 | } |
| 078 | dbConnClose(conn, dbConn); |
| 079 | psClose(ps, rs); |
| 080 |
| 081 | } catch (FileNotFoundException e) { |
| 082 | flag=false; |
| 083 | e.printStackTrace(); |
| 084 | } catch(IOException ex){ |
| 085 | flag=false; |
| 086 | ex.printStackTrace(); |
| 087 | } catch(SQLException exx){ |
| 088 | flag=false; |
| 089 | exx.printStackTrace(); |
| 090 | } |
| 091 |
| 092 | return flag; |
| 093 | } |
| 094 | public static void main(String[] args) { |
| 095 | CardBinDaoImpl e=new CardBinDaoImpl(); |
| 096 | e.insertDB(); |
| 097 | } |
| 098 |
| 099 |
| 100 | /** |
| 101 | * 插入数据 只需要传入插入sql即可 |
| 102 | * 插入sql的样例:insert into t_department values('D004','金融部'); |
| 103 | * @param insert 插入语句 |
| 104 | * @return |
| 105 | * @throws SQLException |
| 106 | */ |
| 107 | /*public int insertCardBin(BizCardBin cardBin){ |
| 108 | int result=0; |
| 109 | PreparedStatement ps = null; |
| 110 | Connection conn = null; |
| 111 | String sql ="insert into BizCardBin(cardName,cardTyoe,cardBin,cardLength,cardLength,activityCode,note) values(?,?,?,?,?,?,?)"; |
| 112 | try { |
| 113 | conn=dbConn.getConnection(); |
| 114 | log.info("获取JDBC连接完成"); |
| 115 | ps = conn.prepareStatement(sql); |
| 116 | ps.setString(1, cardBin.getCardName()); |
| 117 | ps.setString(2, cardBin.getCardType()); |
| 118 | ps.setString(3, cardBin.getCardBin()); |
| 119 | ps.setString(4, cardBin.getCardLength()); |
| 120 | ps.setString(5, cardBin.getActivityCode()); |
| 121 | ps.setString(6, cardBin.getNote()); |
| 122 | result = ps.executeUpdate(); |
| 123 | } catch (SQLException e) { |
| 124 | // TODO Auto-generated catch block |
| 125 | log.error("SQL Exception "+ e); |
| 126 | e.printStackTrace(); |
| 127 | return 0; |
| 128 | }finally{ |
| 129 | dbConnClose(conn, dbConn); |
| 130 | psClose(ps, rs); |
| 131 | } |
| 132 | return result; |
| 133 | } |
| 134 | */ |
| 135 |
| 136 | public void dbConnClose(Connection conn,DBConnBOImpl dbConn){ |
| 137 | if(conn!=null){ |
| 138 | dbConn.close(conn); |
| 139 | } |
| 140 | } |
| 141 |
| 142 | private static void psClose(final PreparedStatement preparedStatement, |
| 143 | final ResultSet resultSet) { |
| 144 | try { |
| 145 | if (resultSet != null) { |
| 146 | resultSet.close(); |
| 147 | } |
| 148 | if (preparedStatement != null) { |
| 149 | preparedStatement.close(); |
| 150 | } |
| 151 | } catch (SQLException e) { |
| 152 | log.error("error", e); |
| 153 | } |
| 154 | } |
| 155 |
| 156 | } |
| 157 |
| 158 | Action层 |
| 159 |
| 160 | public class CardBinAction extends DispatchAction { |
| 161 | public ActionForward showCardBinPage(ActionMapping mapping, ActionForm actionform, |
| 162 | HttpServletRequest request, HttpServletResponse response) |
| 163 | throws Exception { |
| 164 | return mapping.findForward("showCardBinPage"); |
| 165 | } |
| 166 |
| 167 |
| 168 | private CardBinService CardBinService=new CardBinServiceImpl(); |
| 169 |
| 170 | public int insertCardBin() |
| 171 | { |
| 172 | int result=CardBinService.insertDB(); |
| 173 |
| 174 | return result; |