📄 dataimport.java
字号:
if(deleteRecord()==true)
{
return insertRecord();
}
else
{
feedbackMessage ="删除记录失败;";
return false;
}
}
////////////////////////////////////////////////////////////////////
/// ///
/// 删除目标表记录deleteRecord() ///
/// ///
////////////////////////////////////////////////////////////////////
public boolean deleteRecord()
{
Deleter del=new Deleter();
//cond="delete from "+tableName+" where tableName='UserInfo'";
cond="delete from "+tableName+deleteCond;
boolean delSuc=del.delete(cond);
deleteRows=del.getUpdateRows();
return delSuc;
}
////////////////////////////////////////////////////////////////////
/// ///
/// 向目标表插入记录deleteRecord() ///
/// ///
////////////////////////////////////////////////////////////////////
public boolean insertRecord()
{
if(rsRows<1)
{
feedbackMessage="源文件为空";
return false;
}
insertRows=0;
for (int i = firstLine; i < rsRows; i++)
{
if(excelRowToDB(i)==false)
{
// feedbackMessage = "源文件:" + fileName + ",第" + i + "行有错!请修改后重试!";
feedbackMessage = "源文件:" + fileName + ",第" + i + "行有错!请修改后重试!"+selectColumnNum;
additionalCondHead="";
additionalCondHead="";
return false;
}
}
feedbackMessage ="导入成功!";
additionalCondHead="";
additionalCondHead="";
return true;
}
////////////////////////////////////////////////////////////////////
/// ///
/// 更新(存在则修改,不存在则插入) ///
/// ///
////////////////////////////////////////////////////////////////////
public boolean updateThenInsert()
{
if(rsRows<1)
{
feedbackMessage="源文件为空";
return false;
}
insertRows=0;
updateRows=0;
for (int i = firstLine; i < rsRows; i++)
{
if(updateThisRow(i)==true) { continue; } //修改成功
if(excelRowToDB(i)==false)
{
feedbackMessage = "源文件:" + fileName + ",第" + i + "行有错!请修改后重试!";
return false;
}
}
feedbackMessage ="导入成功!";
return true;
}
////////////////////////////////////////////////////////////////////
/// ///
/// 组装更新语句 ///
/// ///
////////////////////////////////////////////////////////////////////
public String getUpdateCond(String[] content)
{
String updateCond=" where ";
int p=0;
for(;p<primaryKeyNumber-1;p++)
{
updateCond=updateCond+primaryKeys[p]+"=";
updateCond=addContentToCond(updateCond
,primaryKeyIndex[p]
,content[primaryKeyIndex[p]]
);
updateCond=updateCond+" and ";
}
updateCond=updateCond+primaryKeys[p]+"=";
updateCond=addContentToCond(updateCond
,primaryKeyIndex[p]
,content[primaryKeyIndex[p]]
);
return updateCond;
}
////////////////////////////////////////////////////////////////////
/// ///
/// 修改行 ///
/// ///
////////////////////////////////////////////////////////////////////
public boolean updateThisRow(int row)
{
try{
Cell cc;//sheet单元格
String[] content=new String[columnCount]; //存放第row行数据的数组
condHead = "update " + tableName +" set ";//插入语句头部
cond = ""; //插入语句值串
//读取第Row行所有要插入的数据
for (int j = 0; j < columnCount; j++)
{
if(selectIndex[j]!=-1)
{
cc = sheetResult.getCell(selectIndex[j], row);
content[j] = cc.getContents();
}
}
//组装执行语句
//j表示第row行的列号
int s=0;
for(int j = 0; j < columnCount; )
{
if (selectIndex[j]==-1) { j++; continue; } //没选择本列,不连接
if(content[j]==null) { j++; continue; }
if(content[j].length()<1) { j++; continue; }// 内容为空,不连接
s++;
//组装cond串
cond=cond+columnName[j]+"=";
cond=addContentToCond(cond,j , content[j]);
//根据后续内容,判断要加上的连接符号
int k=j+1;
for(;k<columnCount;k++)
{
try { //不为空结束循环
if (content[k].length() > 0) { break; }
}
catch (Exception ex) { break; }
}
//到达最后一列
if((k>=columnCount)||(s>=selectColumnNum))
{
break;
}
//没到达最后一列,而且后面还有不为空的单元格
else
{
cond=cond+",";
j=k;
}
}
//执行组装好的插入语句
cond=condHead+cond+getUpdateCond(content);
Updater updt = new Updater();
if (updt.update(cond) == false)
{
return false;
}
else
{
updateRows=updateRows+updt.getUpdateRows();
}
if(updt.getUpdateRows()<=0)
{
return false;
}
return true;
}
catch (Exception e)
{
return false;
}
}
////////////////////////////////////////////////////////////////////
/// ///
/// 根据列数据类型组装cond串 ///
/// ///
////////////////////////////////////////////////////////////////////
public String addContentToCond(String cond,int col,String content)
{
float decimal = 0f;//小数位数
float contentFloat = 0f;//浮点型数据
int contentInt = 0;//整型数据
//判断此列数据类型
if(columnType[col].equals("NUMBER"))//数字类型
{
try {
decimal = (columnLength[col] * 10) % 10; //取小数位数
if (decimal > 0.0f) { //小数位数大于0.0则为浮点型数据
contentFloat = Float.valueOf(content).floatValue();
cond = cond + contentFloat;
}
else { //整型数据
contentInt = Integer.parseInt(content);
cond = cond + contentInt;
}
}
catch (NumberFormatException ex)
{
cond = cond + "0";
}
}
else if(columnType[col].equals("DATE"))//时间类型
{
//
StringTokenizer strtn=new StringTokenizer(content,"-");
int Num=strtn.countTokens()-1;
if(Num==2)
{
cond=cond+"TO_DATE('"+content+"','YYYY-MM-DD')";
}
strtn=new StringTokenizer(content,"/");
Num=strtn.countTokens()-1;
if(Num==2)
{
cond=cond+"TO_DATE('"+content+"','DD/MM/YYYY')";
}
strtn=new StringTokenizer(content,".");
Num=strtn.countTokens()-1;
if(Num==2)
{
cond=cond+"TO_DATE('"+content.replace('.','-')+"','YYYY-MM-DD')";
}
}
else //字符串等其他类型
{ cond = cond + "'" + content +"'";
}
return cond;
}
////////////////////////////////////////////////////////////////////
/// ///
/// 读取EXCEL文件行数据 ///
/// ///
////////////////////////////////////////////////////////////////////
public boolean excelRowToDB(int row)
{
try{
Cell cc;//sheet单元格
String[] content=new String[columnCount]; //存放第row行数据的数组
condHead = "insert into " + tableName + "(";//插入语句头部
cond = " values("; //插入语句值串
//读取第Row行所有要插入的数据
for (int j = 0; j < columnCount; j++)
{
if(selectIndex[j]!=-1)
{
cc = sheetResult.getCell(selectIndex[j], row);
content[j] = cc.getContents();
}
else
{
content[j]="";
}
}
//组装执行语句
//j表示第row行的列号
int s=0;
for(int j = 0; j < columnCount; )
{
if (selectIndex[j]==-1) { j++; continue; } //没选择本列,不连接
if(content[j]==null) { j++; continue; }
if(content[j].length()<1) { j++; continue; }// 内容为空,不连接
s++;
//组装头部
condHead=condHead+columnName[j];
//组装cond串
cond=addContentToCond(cond,j , content[j]);
//根据后续内容,判断要加上的连接符号
int k=j+1;
for(;k<columnCount;k++)
{
if (selectIndex[k]==-1) { k++; continue; }
try { //不为空结束循环
if (content[k].length() > 0) { break; }
}
catch (Exception ex) { k++; break; }
}
//到达最后一列
if((k>=columnCount)||(s>=selectColumnNum))
// if((k>=columnCount))
{
condHead=condHead+additionalCondHead+")";
cond=cond+additionalCond+")";
break;
}
//没到达最后一列,而且后面还有不为空的单元格
else
{
condHead=condHead+",";
cond=cond+",";
j=k;
}
}
//执行组装好的插入语句
Inserter ins = new Inserter();
cond=condHead+cond;
if (ins.insert(cond) == false)
{
return false;
}
else
{
insertRows=insertRows+ins.getUpdateRows();
}
return true;
}
catch (Exception e)
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -