001
|
JDBC+Struct2框架 使用poi讀取Excel檔案的資料 插入到資料庫中 希望大家能給點指點
Impl實現層
|
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 {
|
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;
|
026
|
private String filePath="E:\\Microsoft Excel.xlsx";
|
031
|
public boolean insertDB(){
|
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的列
|
041
|
int totalRow=sheet.getLastRowNum();//得到excel的總記錄條數
|
042
|
log.info("得到excel的總記錄數"+totalRow);
|
047
|
String cardLength="";
|
048
|
String activityCode="";
|
050
|
String sql ="insert into BizCardBin(cardName,cardType,cardBin,cardLength,activityCode,note) values(?,?,?,?,?,?)";
|
051
|
for(int i=2;i<=totalRow;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);
|
076
|
System.out.println("preparestatement successful");
|
078
|
dbConnClose(conn, dbConn);
|
081
|
} catch (FileNotFoundException e) {
|
084
|
} catch(IOException ex){
|
086
|
ex.printStackTrace();
|
087
|
} catch(SQLException exx){
|
089
|
exx.printStackTrace();
|
094
|
public static void main(String[] args) {
|
095
|
CardBinDaoImpl e=new CardBinDaoImpl();
|
102
|
* 插入sql的樣例:insert into t_department values('D004','金融部');
|
105
|
* @throws SQLException
|
107
|
/*public int insertCardBin(BizCardBin cardBin){
|
109
|
PreparedStatement ps = null;
|
110
|
Connection conn = null;
|
111
|
String sql ="insert into BizCardBin(cardName,cardTyoe,cardBin,cardLength,cardLength,activityCode,note) values(?,?,?,?,?,?,?)";
|
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);
|
129
|
dbConnClose(conn, dbConn);
|
136
|
public void dbConnClose(Connection conn,DBConnBOImpl dbConn){
|
142
|
private static void psClose(final PreparedStatement preparedStatement,
|
143
|
final ResultSet resultSet) {
|
145
|
if (resultSet != null) {
|
148
|
if (preparedStatement != null) {
|
149
|
preparedStatement.close();
|
151
|
} catch (SQLException e) {
|
152
|
log.error("error", e);
|
160
|
public class CardBinAction extends DispatchAction {
|
161
|
public ActionForward showCardBinPage(ActionMapping mapping, ActionForm actionform,
|
162
|
HttpServletRequest request, HttpServletResponse response)
|
164
|
return mapping.findForward("showCardBinPage");
|
168
|
private CardBinService CardBinService=new CardBinServiceImpl();
|
170
|
public int insertCardBin()
|
172
|
int result=CardBinService.insertDB();
|
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29119536/viewspace-1179895/,如需轉載,請註明出處,否則將追究法律責任。