表名参数化

This commit is contained in:
王炜 2018-06-21 14:39:11 +08:00
parent a319843c16
commit ecf50ef77e
12 changed files with 119 additions and 86 deletions

View File

@ -1,3 +1,2 @@
.idea/workspace.xml
out/
dataSources/

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Kane" />
</state>
</component>

View File

@ -24,21 +24,18 @@ public class DataImport
/**
* 读取xls表格插入数据要么全部成功要么全部失败
* @param fileName xls文件名
* @param oracleURL oracle数据库URL
* @param oracleUserName oracle用户名
* @param oraclePassword oracle密码
* @param informixURL informix数据库URL
*
* @param fileName xls文件名
* @param telsalePolicyOperatorListTableName informix库电销转介绍表名
* @param informixURL informix数据库URL
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException 数据库异常
* @throws SQLException 数据库异常
* @throws XlsFileFormatException xls文件格式错误异常
*/
public static String importTelsalePolicyDataFromXlsx( String fileName,
String oracleURL,
String oracleUserName,
String oraclePassword,
String telsalePolicyOperatorListTableName,
String informixURL ) throws
IOException,
ClassNotFoundException,
@ -47,9 +44,9 @@ public class DataImport
{
String importResult = "";
String importMessage = null;
int rowCount = 1;
int successCount = 0;
int failCount = 0;
int rowCount = 1; //excel文件行索引
int successCount = 0; //成功写入的行计数
int failCount = 0; //写入失败的行计数
//excel相关
FileInputStream xlsFileIn = null;
@ -61,7 +58,7 @@ public class DataImport
{
xlsFileIn = new FileInputStream( fileName );
xlsFile = new HSSFWorkbook( xlsFileIn );
sheet = xlsFile.getSheet( "Sheet1" );
sheet = xlsFile.getSheetAt( 0 ); //第一个sheet
row = sheet.getRow( rowCount );
}
catch( IOException error ) //有文件格式错误的可能
@ -72,47 +69,10 @@ public class DataImport
throw new XlsFileFormatException( "文件格式错误请检查是否xls格式文件。" );
}
//oracle相关
Class.forName( "oracle.jdbc.driver.OracleDriver" );
Connection oracleConn = null;
PreparedStatement oracleStmt = null;
String oraSql =
"INSERT INTO telsale_oper_lst\n" +
" (policy_no,\n" +
" operator_code,\n" +
" operator_name,\n" +
" section_office_code,\n" +
" section_office_name,\n" +
" department_code,\n" +
" department_name,\n" +
" autotrader_code,\n" +
" insruant_name,\n" +
" plate_no,\n" +
" entry_staff_code,\n" +
" entry_date)\n" +
"VALUES\n" +
" (?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" '800',\n" +
" SYSDATE)";
oracleConn = DriverManager.getConnection( oracleURL, oracleUserName, oraclePassword );
oracleConn.setAutoCommit( false );//关闭自动提交整个表格要么全部成写入要么全部失败
oracleStmt = oracleConn.prepareStatement( oraSql );
//informix相关
Class.forName( "com.informix.jdbc.IfxDriver" );
String ifxSql =
"INSERT INTO w_dxbd_i\n" +
"INSERT INTO " + telsalePolicyOperatorListTableName + " \n" +
" (bdh,\n" +
" zhjywy,\n" +
" zhjywymc,\n" +
@ -174,20 +134,6 @@ public class DataImport
//取得经办人信息
staff = new StaffInfo( operatorCode );
//写入oracle
// oracleStmt.setString( 1, policyNo );
// oracleStmt.setString( 2, staff.getStaffCode() );
// oracleStmt.setString( 3, staff.getStaffName() );
// oracleStmt.setString( 4, staff.getSectionOfficeCode() );
// oracleStmt.setString( 5, staff.getSectionOfficeName() );
// oracleStmt.setString( 6, staff.getDepartmentCode() );
// oracleStmt.setString( 7, staff.getDepartmentName() );
// oracleStmt.setString( 8, getStringValueFromHSSFCell( row.getCell( 6 ) ) );
// oracleStmt.setString( 9, getStringValueFromHSSFCell( row.getCell( 3 ) ) );
// oracleStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) );
//
// oracleStmt.execute();
//写入informix
ifxStmt.setString( 1, policyNo );
ifxStmt.setString( 2, staff.getStaffCode() );
@ -201,6 +147,8 @@ public class DataImport
ifxStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) );
ifxStmt.execute();
successCount++;
}
catch( StaffCodeNotExistException error )
{
@ -209,6 +157,9 @@ public class DataImport
//标志位
isSuccess = false;
//计数
failCount++;
}
catch( SQLException error )
{
@ -217,6 +168,20 @@ public class DataImport
//标志位
isSuccess = false;
//计数
failCount++;
}
catch( Exception error )
{
//写入错误
importResult = importResult + "" + rowCount + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>";
//标志位
isSuccess = false;
//计数
failCount++;
}
}
@ -227,25 +192,36 @@ public class DataImport
//判断标志位如果为false则说明在保存过程中出现了错误放弃所有过程进行回滚
if ( isSuccess == true )
{
oracleConn.commit();
//oracleConn.commit();
ifxConn.commit();
}
else
{
oracleConn.rollback();
ifxConn.rollback();
}
//清理
oracleConn.close();
xlsFile.close();
xlsFileIn.close();
//日志
importMessage = "记录数量:" + (rowCount - 1) +
"<br>导入成功数量:" + successCount +
"<br>导入失败数量:" + failCount +
"<br>" + importResult;
importMessage = "读取记录数量:" + (rowCount - 1) +
"<br>正确记录数量:" + successCount +
"<br>错误记录数量:" + failCount;
if ( failCount != 0 )
{
//有错误的记录输出提示
importMessage += "<br>请根据错误提示修正后重新导入!<br>";
}
else
{
//无错误的记录输出成功提示
importMessage += "<br>文件已成功导入!<br>";
}
//追加导入记录
importMessage += importResult;
return importMessage;
}

View File

@ -19,24 +19,23 @@ import com.cpic.telsale.DataImport.*;
public class uploadDataProcessor extends HttpServlet
{
private String tempPathRoot; //临时文件目录
private String oracleURL;
private String oracleUserName;
private String oraclePassword;
// private String oracleURL;
// private String oracleUserName;
// private String oraclePassword;
private String informixURL;
private String telsalePolicyOperatorListTableName; //informix库电销转介绍表名
@Override
public void init( ServletConfig config ) throws ServletException
{
//获取参数
//tempPathRoot = ( String )config.getServletContext().getAttribute( "临时文件根目录" );
//oracle
oracleURL = ( String )config.getServletContext().getInitParameter( "oracleURL" );
oracleUserName = ( String )config.getServletContext().getInitParameter( "oracleUserName" );
oraclePassword = ( String )config.getServletContext().getInitParameter( "oraclePassword" );
// oracleURL = ( String )config.getServletContext().getInitParameter( "oracleURL" );
// oracleUserName = ( String )config.getServletContext().getInitParameter( "oracleUserName" );
// oraclePassword = ( String )config.getServletContext().getInitParameter( "oraclePassword" );
//informix
informixURL = ( String )config.getServletContext().getInitParameter( "informixURL" );
telsalePolicyOperatorListTableName = (String)config.getServletContext().getInitParameter( "telsalePolicyOperatorListTableName" );
}
@Override
@ -66,9 +65,7 @@ public class uploadDataProcessor extends HttpServlet
String upLoadedFileName = saveUploadedFile( fileItem );
importMessage = DataImport.importTelsalePolicyDataFromXlsx( upLoadedFileName,
oracleURL,
oracleUserName,
oraclePassword,
telsalePolicyOperatorListTableName,
informixURL );
}
}

View File

@ -35,6 +35,10 @@
<param-name>informixURL</param-name>
<param-value>jdbc:informix-sqli://10.39.0.92:16192/ywgl_xm:INFORMIXSERVER=xmcx2;newcodeset=GBK,8859-1,819;user=ccx99;password=c92IT09</param-value>
</context-param>
<context-param>
<param-name>telsalePolicyOperatorListTableName</param-name>
<param-value>w_dxbd_i1</param-value>
</context-param>
<!--Servlet设置-->
<servlet>

Binary file not shown.

View File

@ -26,6 +26,8 @@
<input type="submit" name="upload" value="上传">
<input type="reset" name="reset" value="重置">
</form>
<a href="../doc/电销转介绍导入模板.xls" style="color:red;">导入模板下载</a>
<p style="display: inline">备注:保单号、车牌号、被保人、经办人代码、车商代码为必填项!</p>
<!--上传结果显示-->
<%
int uploadCount = 0;

View File

@ -0,0 +1,18 @@
DROP TABLE telsale_oper_lst;
CREATE TABLE telsale_oper_lst
(
policy_no VARCHAR2(20) PRIMARY KEY,
operator_code VARCHAR2(6) NOT NULL,
operator_name VARCHAR2(100 CHAR) NOT NULL,
section_office_code VARCHAR2(6) NOT NULL,
section_office_name VARCHAR2(100 CHAR) NOT NULL,
department_code VARCHAR2(6),
department_name VARCHAR2(100 CHAR),
autotrader_code VARCHAR2(30), --
insruant_name VARCHAR2(200 CHAR), --
plate_no VARCHAR2(7 CHAR), --
entry_staff_code VARCHAR2(6) NOT NULL,
entry_date DATE DEFAULT SYSDATE
);
CREATE INDEX telsale_oper_lst_i ON telsale_oper_lst(POLICY_NO,operator_code);

View File

@ -0,0 +1,32 @@
/*
truncate table telsale_oper_lst;
*/
/*
INSERT INTO telsale_oper_lst
(policy_no,
operator_code,
operator_name,
section_office_code,
section_office_name,
department_code,
department_name,
autotrader_code,
insruant_name,
plate_no,
entry_staff_code,
entry_date)
VALUES
(?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
'800',
SYSDATE)
*/
SELECT * FROM telsale_oper_lst;

BIN
数据/导入20170110-1.xls Normal file

Binary file not shown.

BIN
数据/无重复.xls Normal file

Binary file not shown.

BIN
数据/有重复.xls Normal file

Binary file not shown.