所以这就是我要做的是为oracle sql表创建一个备份文件。我使用opencsv.jar来创建table_bkup.csv文件。
然后我应该将备份文件中的3列与oracle数据库中的当前表进行比较。
我决定将另一个csv作为table_current.csv文件。
我现在想比较两个csv文件,看看3列是增加还是减少。表中有58行数据和20列。
现在,三列中的值始终应该等于备份或增加值。 如果出现问题且价值降低,则需要向主管发送电子邮件。
我该怎么做? 谢谢, 迈克尔B.
P.S。我是Java编程的新手,任何帮助将不胜感激!到目前为止,我已经在这个项目上花了大约11个小时,包括所有研究和学习如何尽可能多地完成。
答案 0 :(得分:1)
执行此操作的一种方法是使用java的Set
接口;将每一行作为字符串读取,将其添加到集合中,然后在第一组上使用第二个集合执行removeAll()
,从而保留不同的行。当然,这假定文件中没有重复的行。
// using FileUtils to read in the files.
HashSet<String> f1 = new HashSet<String>(FileUtils.readLines("file1.csv"));
HashSet<String> f2 = new HashSet<String>(FileUtils.readLines("file2.csv"));
f1.removeAll(f2); // f1 now contains only the lines which are not in f2
你有PK领域吗?我只是假设你知道如何从你的字符串中得到它;使用openCSV或正则表达式或任何你想要的。如上所示设置实际的HashMap
而不是HashSet
,使用PK作为键,将行作为值。
HashMap<String, String> f1 = new HashMap<String, String>();
HashMap<String, String> f2 = new HashMap<String, String>();
// read f1, f2; use PK field as the key
List<String> deleted = new ArrayList<String>();
List<String> updated = new ArrayList<String>();
for(Map.Entry<String, String> entry : f1.keySet()) {
if(!f2.containsKey(entry.getKey()) {
deleted.add(entry.getValue());
} else {
if(!f2.get(entry.getKey().equals(f1.getValue())) {
updated.add(f1.getValue());
}
}
}
for(String key : f1.keySet()) {
f2.remove(key);
}
// f2 now contains only "new" rows
答案 1 :(得分:-1)
try{
CSVReader reader = new CSVReader(new FileReader("b2b_control_number_bkup.csv"));
String [] nextLine;
int counter = 0;
// First File Variables
String SENDER_NAME = null;
String RECEIVER_NAME = null;
String DOC_PROTOCOL_NAME = null;
String DIRECTION = null;
String NAME = null;
String IS_DEFAULT = null;
/*
String INTERCHANGE = null;
String GROUP_NUM = null;
String TRANSACTION_NUM = null;
*/
int INTERCHANGE = 0;
int GROUP_NUM = 0;
int TRANSACTION_NUM = 0;
//Second File variables
String SENDER_NAMEb = null;
String RECEIVER_NAMEb = null;
String DOC_PROTOCOL_NAMEb = null;
String DIRECTIONb = null;
String NAMEb = null;
String IS_DEFAULTb = null;
/*
String INTERCHANGEb = null;
String GROUP_NUMb = null;
String TRANSACTION_NUMb = null;
*/
int INTERCHANGEb = 0;
int GROUP_NUMb = 0;
int TRANSACTION_NUMb = 0;
while ((nextLine = reader.readNext()) != null) {
//System.out.println(nextLine[4] +" "+ nextLine[5] +" "+ nextLine[6]);
SENDER_NAME = nextLine[0];
RECEIVER_NAME = nextLine[1];
DOC_PROTOCOL_NAME = nextLine[2];
DIRECTION = nextLine[3];
INTERCHANGE = Integer.parseInt(nextLine[4]);
GROUP_NUM = Integer.parseInt(nextLine[5]);
TRANSACTION_NUM = Integer.parseInt(nextLine[6]);
NAME = nextLine[15];
IS_DEFAULT = nextLine[16];
// nextLine[] is an array of values from the line
CSVReader readerb = new CSVReader(new FileReader("b2b_control_number_current.csv"));
String [] nextLineb;
while ((nextLineb = readerb.readNext()) != null) {
SENDER_NAMEb = nextLineb[0];
RECEIVER_NAMEb = nextLineb[1];
DOC_PROTOCOL_NAMEb = nextLineb[2];
DIRECTIONb = nextLineb[3];
INTERCHANGEb = Integer.parseInt(nextLineb[4]);
GROUP_NUMb = Integer.parseInt(nextLineb[5]);
TRANSACTION_NUMb = Integer.parseInt(nextLineb[6]);
NAMEb = nextLineb[15];
IS_DEFAULTb = nextLineb[16];
//System.out.println(nextLineb[4] +" "+ nextLineb[5] +" "+ nextLineb[6]);
//System.out.println("RECEIVER_NAMEa: " + RECEIVER_NAME +" "+ RECEIVER_NAMEb);
if((SENDER_NAME.equals(SENDER_NAMEb))
&& (RECEIVER_NAME.equals(RECEIVER_NAMEb))
&& (DOC_PROTOCOL_NAME.equals(DOC_PROTOCOL_NAMEb))
&& (DIRECTION.equals(DIRECTIONb))
&& (NAME.equals(NAMEb))
&& (IS_DEFAULT.equals(IS_DEFAULTb))){
if((INTERCHANGE<=(INTERCHANGEb)) && (GROUP_NUM<=(GROUP_NUMb)) && (TRANSACTION_NUM<=(TRANSACTION_NUMb))){
pw.println("Everything Okay");
}else{
pw.println("Error Send Email");
sendEmail ="1";
}
//System.out.println("RECEIVER_NAME: " + RECEIVER_NAME +" "+ RECEIVER_NAMEb);
pw.println("INTERCHANGE: " + INTERCHANGE +" "+ INTERCHANGEb +
" GROUP_NUM: " + GROUP_NUM +" "+ GROUP_NUMb+
" TRANSACTION_NUM: " + TRANSACTION_NUM +" "+ TRANSACTION_NUMb);
}
}
/*
counter ++;
if(counter == 2){
break;
}
*/
readerb.close();
}
reader.close();
}catch(Exception e){
e.printStackTrace();
}