This commit is contained in:
Kane Wang 2021-06-27 10:48:16 +08:00
parent fa9385a064
commit cfc285bd0f
7 changed files with 76 additions and 298 deletions

View File

@ -116,251 +116,6 @@ public class DataImport
return policyList; return policyList;
} }
/**
* 读取xls表格插入数据要么全部成功要么全部失败
*
* @param fileName xls文件名
* @param telsalePolicyOperatorListTableName informix库电销转介绍表名
* @param informixURL informix数据库URL
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException 数据库异常
* @throws XlsFileFormatException xls文件格式错误异常
*/
public static String importTelsalePolicyDataFromXlsx( String fileName,
String telsalePolicyOperatorListTableName,
String informixURL )
throws IOException,
ClassNotFoundException,
SQLException,
XlsFileFormatException
{
String importResult = "";
String importMessage = null;
int rowIndex = 1; //excel文件行索引
int rowCount = -1;
int successCount = 0; //成功写入的行计数
int failCount = 0; //写入失败的行计数
//excel相关
FileInputStream xlsFileIn = null;
HSSFWorkbook xlsFile = null;
HSSFSheet sheet = null;
HSSFRow row = null;
try
{
xlsFileIn = new FileInputStream( fileName );
xlsFile = new HSSFWorkbook( xlsFileIn );
sheet = xlsFile.getSheetAt( 0 ); //第一个sheet
rowIndex = sheet.getFirstRowNum() + 1;
rowCount = sheet.getLastRowNum();
row = sheet.getRow( rowIndex );
}
catch ( IOException error ) //有文件格式错误的可能
{
xlsFile.close();
xlsFileIn.close();
throw new XlsFileFormatException( "文件格式错误请检查是否xls格式文件。" );
}
//informix相关
Class.forName( "com.informix.jdbc.IfxDriver" );
String ifxSql =
"INSERT INTO " + telsalePolicyOperatorListTableName + " \n" +
" (bdh,\n" +
" zhjywy,\n" +
" zhjywymc,\n" +
" kshdm,\n" +
" kshmc,\n" +
" zhjywybm,\n" +
" zhjywybmm,\n" +
//" chshdm,\n" +
//" khmc,\n" +
//" chph,\n" +
" khjl,\n" +
" khjllx,\n" +
" jjbj,\n" +
" czydm,\n" +
" czrq)\n" +
"VALUES\n" +
" (?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
" ?,\n" +
//" ?,\n" +
//" ?,\n" +
//" ?,\n" +
" 0,\n" +
" 0,\n" +
" 1,\n" +
" '800',\n" +
" today)";
Connection ifxConn = DriverManager.getConnection( informixURL );
PreparedStatement ifxStmt = ifxConn.prepareStatement( ifxSql );
ifxConn.setAutoCommit( false );//关闭自动提交整个表格要么全部成写入要么全部失败
//数据相关
String policyNo = null;
String operatorCode = null;
StaffInfo staff = null;
boolean isSuccess = true; //标志位用来指示在保存过程中是否出现错误
while ( rowIndex <= rowCount )
{
//用来判断是不是空行
HSSFCell cell = row.getCell( 0 );
if ( cell == null )
{
rowIndex++;
continue;
}
policyNo = row.getCell( 0 ).getStringCellValue().trim();
operatorCode = getStringValueFromHSSFCell( row.getCell( 1 ) );
//判断一下是不是空的单元格
if ( policyNo.length() == 0 || operatorCode.length() == 0 )
{
importResult = importResult + "" + rowIndex + "行数据为空。<br>";
//isSuccess = false;
}
else //不是空单元格进行处理
{
try
{
//取得经办人信息
staff = new StaffInfo( operatorCode );
//写入informix
ifxStmt.setString( 1, policyNo );
ifxStmt.setString( 2, staff.getStaffCode() );
ifxStmt.setString( 3, staff.getStaffName() );
ifxStmt.setString( 4, staff.getSectionOfficeCode() );
ifxStmt.setString( 5, staff.getSectionOfficeName() );
ifxStmt.setString( 6, staff.getDepartmentCode() );
ifxStmt.setString( 7, staff.getDepartmentName() );
//ifxStmt.setString( 8, getStringValueFromHSSFCell( row.getCell( 6 ) ) );
//ifxStmt.setString( 9, getStringValueFromHSSFCell( row.getCell( 3 ) ) );
//ifxStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) );
//ifxStmt.execute();
//成功写入提示
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",转介绍信息入成功!<br>";
successCount++;
}
catch ( StaffCodeNotExistException error )
{
//经办人不存在
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + "不存在。<br>";
//标志位
isSuccess = false;
//计数
failCount++;
}
catch ( OracleConnectionException error )
{
//查询工号失败
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",经办人" + operatorCode + ",查询工号失败。<br>";
//标志位
isSuccess = false;
//计数
failCount++;
}
catch ( SQLException error )
{
int errorCode = error.getErrorCode();
switch ( errorCode )
{
case -268: //保单号重复
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",已录入过转介绍信息!" + error.getCause() + "。<br>";
break;
default: //写入错误
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error
.getCause() + "。<br>";
break;
}
//标志位
isSuccess = false;
//计数
failCount++;
}
catch ( Exception error )
{
//写入错误
importResult = importResult + "" + rowIndex + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>";
//标志位
isSuccess = false;
//计数
failCount++;
}
finally
{
ifxConn.commit();
}
}
rowIndex++;
row = sheet.getRow( rowIndex );
}
//判断标志位如果为false则说明在保存过程中出现了错误放弃所有过程进行回滚
// if ( isSuccess == true )
// {
// //oracleConn.commit();
// ifxConn.commit();
// }
// else
// {
// ifxConn.rollback();
// }
//清理
xlsFile.close();
xlsFileIn.close();
//日志
importMessage = "读取记录数量:" + ( rowIndex - 1 ) +
"<br>写入记录数量:" + successCount +
"<br>错误记录数量:" + failCount + "<br>";
// if ( failCount != 0 )
// {
// //有错误的记录输出提示
// importMessage += "<br>请根据错误提示修正后重新导入!<br>";
// }
// else
// {
// //无错误的记录输出成功提示
// importMessage += "<br>文件已成功导入!<br>";
// }
//追加导入记录
importMessage += importResult;
return importMessage;
}
private static String getStringValueFromHSSFCell( HSSFCell cell ) private static String getStringValueFromHSSFCell( HSSFCell cell )
{ {
String value = null; String value = null;
@ -593,8 +348,8 @@ public class DataImport
/** /**
* @Description: 读取excel文件将数据写入oracle和informix数据库 * @Description: 读取excel文件将数据写入oracle和informix数据库
* 数据库连接关闭自动提交在两个数据库都写入成功后提交 * 数据库连接关闭自动提交在两个数据库都写入成功后提交
* 只要有一个数据库写入抛出异常回滚两个数据库的事务 * 只要有一个数据库写入抛出异常回滚两个数据库的事务
* @Param: * @Param:
* @return: void * @return: void
* @Author: 王炜 * @Author: 王炜

View File

@ -35,13 +35,13 @@ public class uploadDataProcessor extends HttpServlet
ServletException ServletException
{ {
//获取参数 //获取参数
oracleURL = (String) config.getServletContext().getInitParameter( "oracleURL" ); oracleURL = (String) config.getServletContext().getInitParameter( "oracle_url_dev01" );
oracleUserName = (String) config.getServletContext().getInitParameter( "oracleUserName" ); oracleUserName = (String) config.getServletContext().getInitParameter( "oracleUserName" );
oraclePassword = (String) config.getServletContext().getInitParameter( "oraclePassword" ); oraclePassword = (String) config.getServletContext().getInitParameter( "oraclePassword" );
//informix dxbd_test //informix dxbd_test
//informixURL = (String) config.getServletContext().getInitParameter( "informixURL" ); //informixURL = (String) config.getServletContext().getInitParameter( "informixURL" );
informixURL = (String) config.getServletContext().getInitParameter( "dxbd_test" ); informixURL = (String) config.getServletContext().getInitParameter( "informix_url_92" );
telsalePolicyOperatorListTableName = (String) config.getServletContext() telsalePolicyOperatorListTableName = (String) config.getServletContext()
.getInitParameter( "telsalePolicyOperatorListTableName" ); .getInitParameter( "telsalePolicyOperatorListTableName" );
} }

View File

@ -20,15 +20,14 @@ public class StaffInfo
private static String oracleURL = "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1"; private static String oracleURL = "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1";
public StaffInfo( String staffCode ) public StaffInfo(String staffCode)
throws StaffCodeNotExistException, throws StaffCodeNotExistException,
OracleConnectionException, OracleConnectionException,
ClassNotFoundException, ClassNotFoundException,
SQLException SQLException
{ {
if ( staffCode.length() < 3 ) if (staffCode.length() < 3) {
{ throw new StaffCodeNotExistException(staffCode + "不存在。");
throw new StaffCodeNotExistException( staffCode + "不存在。" );
} }
//url要改成可配置的 //url要改成可配置的
@ -39,7 +38,7 @@ public class StaffInfo
this.staffCode = staffCode; this.staffCode = staffCode;
Class.forName( "oracle.jdbc.driver.OracleDriver" ); Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -58,43 +57,32 @@ public class StaffInfo
" AND ry.department_code = bm.department_code\n" + " AND ry.department_code = bm.department_code\n" +
" AND ry.section_office_code = ksh.section_office_code"; " AND ry.section_office_code = ksh.section_office_code";
try try {
{ conn = DriverManager.getConnection(oracleURL, oracleUserName, oraclePassword);
conn = DriverManager.getConnection( oracleURL, oracleUserName, oraclePassword ); stmt = conn.prepareStatement(sql);
stmt = conn.prepareStatement( sql ); stmt.setString(1, staffCode);
stmt.setString( 1, staffCode );
result = stmt.executeQuery(); result = stmt.executeQuery();
if ( result.next() ) if (result.next()) {
{ staffName = result.getString("staff_name");
staffName = result.getString( "staff_name" ); sectionOfficeCode = result.getString("section_office_code");
sectionOfficeCode = result.getString( "section_office_code" ); sectionOfficeName = result.getString("section_office_name");
sectionOfficeName = result.getString( "section_office_name" ); departmentCode = result.getString("department_code");
departmentCode = result.getString( "department_code" ); departmentName = result.getString("department_name");
departmentName = result.getString( "department_name" );
} }
if ( staffName.isEmpty() == true ) if (staffName.isEmpty() == true) {
{
//没查到数据 //没查到数据
throw new StaffCodeNotExistException( "工号" + staffCode + "不存在。" ); throw new StaffCodeNotExistException("工号" + staffCode + "不存在。");
} }
} } catch (SQLException error) {
catch ( SQLException error ) throw new OracleConnectionException(error.getMessage());
{ } finally {
throw new OracleConnectionException( error.getMessage() ); try {
} if (conn != null) {
finally
{
try
{
if ( conn != null )
{
conn.close(); conn.close();
} }
} } catch (SQLException error) {
catch ( SQLException error )
{
//不处理了 //不处理了
} }
} }

View File

@ -20,9 +20,13 @@
<param-value>temporaryFiles</param-value> <param-value>temporaryFiles</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>oracleURL</param-name> <param-name>oracle_url_dev01</param-name>
<param-value>jdbc:oracle:thin:@10.39.0.85:1521:dev01</param-value> <param-value>jdbc:oracle:thin:@10.39.0.85:1521:dev01</param-value>
</context-param> </context-param>
<context-param>
<param-name>oracle_url_xmcx1</param-name>
<param-value>jdbc:oracle:thin:@10.39.0.86:1521:xmcx1</param-value>
</context-param>
<context-param> <context-param>
<param-name>oracleUserName</param-name> <param-name>oracleUserName</param-name>
<param-value>telsale</param-value> <param-value>telsale</param-value>
@ -32,17 +36,13 @@
<param-value>Cpic#1234</param-value> <param-value>Cpic#1234</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>informixURL</param-name> <param-name>informix_prod_url</param-name>
<param-value>jdbc:informix-sqli://10.187.11.163:9096/ywgl_xm:INFORMIXSERVER=pxmcx2;newcodeset=GBK,8859-1,819;user=ccx99;password=ct0IT17!</param-value> <param-value>jdbc:informix-sqli://10.187.11.163:9096/ywgl_xm:INFORMIXSERVER=pxmcx2;newcodeset=GBK,8859-1,819;user=ccx99;password=ct0IT17!</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>dxbd_test</param-name> <param-name>informix_url_92</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> <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>
<context-param>
<param-name>ora_telsale_url</param-name>
<param-value>jdbc:oracle:thin:@10.39.0.85:1521:dev01;user=telsale;password=Cpic%231234;</param-value>
</context-param>
<context-param> <context-param>
<param-name>ora_xmcx1_url</param-name> <param-name>ora_xmcx1_url</param-name>
<param-value>jdbc:oracle:thin:@10.39.0.86:1521:xmcx1;user=ywglxt;password=ywglxt;</param-value> <param-value>jdbc:oracle:thin:@10.39.0.86:1521:xmcx1;user=ywglxt;password=ywglxt;</param-value>

View File

@ -0,0 +1,7 @@
/*
truncate table
*/
SELECT *
FROM ;

View File

@ -6,7 +6,7 @@ SavePath=D:\develop\projects_win\2018\telsale_management_2017\
Index=1 Index=1
Filename=D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\包\telsale_policy_check_pkg.pck Filename=D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\包\telsale_policy_check_pkg.pck
VCSDBObject=0 VCSDBObject=0
Connection=203611904672479448844638482446903692419849284826467636303960398639804038366443144308449440244018359636063616 Connection=271145534323418944714289409142133087477745794477439133453099306130553049344348614855459331633157337533853395
DPI=96 DPI=96
Left=0 Left=0
Top=0 Top=0
@ -17,11 +17,38 @@ Type=3
ChildListIndex=1 ChildListIndex=1
Pinned=0 Pinned=0
Index=2
Filename=D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\쉔깊\든饋괏데斤口깊.sql
Connection=228750254923504546234889507549414071444946675045492738813667372537513777391545654559423337633757384738573867
DPI=96
Left=0
Top=0
Width=1474
Height=636
State=0
Type=1
ChildListIndex=3
Pinned=0
Index=3
Filename=D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\쉔깊\꿴璂.sql
Connection=206849344704482648524670472845943724416649604602477237263992395439484070356842184212446240564050401240224032
DPI=96
Left=0
Top=0
Width=1550
Height=719
State=0
Type=1
ChildListIndex=2
Pinned=0
[Files] [Files]
[MRU] [MRU]
3,D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\관\telsale_policy_check_pkg.pck 1,D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\쉔깊\꿴璂.sql
1,D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\建表\电销保单信息表.sql 1,D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\建表\电销保单信息表.sql
3,D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\관\telsale_policy_check_pkg.pck
1,D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\建表\电销保单信息录入日志表.sql 1,D:\develop\projects_win\2018\telsale_management_2017\代码\oracle\项目\建表\电销保单信息录入日志表.sql
[Layout] [Layout]

View File

@ -3,7 +3,7 @@ PL/SQL Developer Project
[Options] [Options]
AutoConnect=0 AutoConnect=0
Username=telsale Username=telsale
Password=2093407949054803468536713937389938934015 Password=2784480245724182438433383060305430483170
Database=DEV01 Database=DEV01
ConnectAs=Normal ConnectAs=Normal
Edition=1.0 Edition=1.0
@ -17,8 +17,8 @@ VersionControlPath=
ShowItems=-1 ShowItems=-1
[History] [History]
Programs=D:\develop\projects_win\2019\car_dealer_util\代码\数据库\oracle\pkg Programs=D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\관
SQL Scripts=D:\develop\projects_win\2018\xmty_util_2018\代码\oracle\xmty\sql\查询测试 SQL Scripts=D:\develop\projects_win\2018\telsale_management_2017\덜쯤\oracle\淃커\쉔깊
Report Files= Report Files=
Test Scripts= Test Scripts=
Command Scripts= Command Scripts=
@ -31,6 +31,7 @@ Diagram Files=
[Files] [Files]
1,0,,,建表\电销保单信息表.sql 1,0,,,建表\电销保单信息表.sql
1,0,,,쉔깊\꿴璂.sql
[Notes] [Notes]
{\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Arial;}} {\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil Arial;}}