现在要实现以导入并更新功能,不是使用数据库自带的导入功能,
而是使用编写程序的方式来实现;
具体功能如下:
1、 首先要判断该目录有没有此文件(*.CSV)
2、然后要读取里面的数据,(csv文件里的格式是以","分隔的)
我需要对其进行分组;(好和数据库里的字段想对应)
csv文件格式的第一列是和数据库的某个字段是相对应的(就是更新条件)
csv格式是"文本,值","文本,值"。。。。
哪位大神给力啊!急需
非常感谢
if(null!=filename){
long before = System.currentTimeMillis();
long after =0;
//根据附件上传的ID得到附件的路径
String excellink=proDuctService.getfjmc(filename);
String newpath="d:/jboss-4.2.2.GA/server/default/tmp/deploy/data.csv";
//把取出来的文件复制到另一个目录下,并保存成csv文件
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(excellink);
if (oldfile.exists()){
//文件存在时
InputStream inStream = new FileInputStream(excellink);
//读入原文件
FileOutputStream fs = new FileOutputStream(newpath);
byte[] buffer = new byte[2000];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
//字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
File files=new File("d:/data.ctl");
if(!files.exists()){
files.createNewFile();
}
FileWriter fw=new FileWriter("d:/data.ctl",false);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("load data\r\ninfile '"+newpath+"'\r\nappend into table "+tablename+"\r\nfields terminated by ','\r\n("+col+")");
fw.flush();
bw.close();
fw.close();
String sql = "sqlldr userid="+dlname+"/"+passname+"@"+dataname+" control='d:/data.ctl' log='d:/jboss-4.2.2.GA/server/default/deploy/goa.ear/goa.war/datalog/"+filename+".log'";
try {
Runtime.getRuntime().exec(sql.toString());
} catch (IOException e) {
e.printStackTrace();
}finally{
after = System.currentTimeMillis();
}
}