package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import citicprj.database.connectionPool;
/**
* CSV操作(导出和导入)
*
* @author 林计钦
* @version 1.0 Jan 27, 2014 4:30:58 PM
*/
public class CSVUtil {
/**
* 导出
*
* @param file
* csv文件(路径+文件名),csv文件不存在会自动创建
* @param dataList
* 数据
* @return
*/
public static boolean exportCsv(File file, List<String> dataList) {
boolean isSucess = false;
FileOutputStream out = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
out = new FileOutputStream(file);
osw = new OutputStreamWriter(out, "GBK");
osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }));
bw = new BufferedWriter(osw);
if (dataList != null && !dataList.isEmpty()) {
for (String data : dataList) {
bw.append(data).append("\r");
}
}
isSucess = true;
} catch (Exception e) {
isSucess = false;
} finally {
if (bw != null) {
try {
bw.close();
bw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
osw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return isSucess;
}
/**
* 导入
*
* @param file
* csv文件(路径+文件)
* @return
*/
public static List<String> importCsv(File file) {
List<String> dataList = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
dataList.add(line);
}
} catch (Exception e) {
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return dataList;
}
@SuppressWarnings("unchecked")
public static List getData(String sqlstr){
java.sql.Connection conn = null;
java.sql.CallableStatement stmt = null;
java.sql.ResultSet rs = null;
List data=new ArrayList();
try {
conn = connectionPool.getConnection();
stmt = conn.prepareCall(sqlstr);
StringBuilder sb=new StringBuilder();
stmt.execute();
while (true) {
rs = stmt.getResultSet();
if (rs != null)
break;
if ((stmt.getUpdateCount() >= 0)
&& (stmt.getMoreResults() == true)) { // 不是最后一个结果
continue;
}
if ((stmt.getUpdateCount() < 0)
&& (stmt.getMoreResults() == false) && (rs == null)) { // 没有结果
break;
}
}
int cols = rs.getMetaData().getColumnCount();
for (int i = 1; i <= cols; i++) {
sb.append(rs.getMetaData().getColumnName(i)+",");
}
data.add(sb.toString());
while (rs.next()) {
StringBuilder sb2=new StringBuilder();
for (int i = 1; i <= cols; i++) {
if(null == rs.getObject(i)){
sb2.append(",");
}else{
sb2.append(rs.getObject(i)+",");
}
}
data.add(sb2.toString());
}
return data;
} catch (SQLException e) {
e.printStackTrace();
return null;
}finally{
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class CustsmTimerTask {
/**
* Description: 从本地上传文件到共享目录
* @param remoteUrl
* 共享文件目录
* @param localFilePath
* 本地文件绝对路径
*/
public static boolean smbPut(String remoteUrl, String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void doTimerTask() {
Runnable runnable = new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
List<String> data=CSVUtil.getData("{call report..p_report_smjj_wtxx('getOne')}");
List<String> data2=CSVUtil.getData("{call report..p_report_smjj_wtxx('getTwo')}");
List<String> data3=CSVUtil.getData("{call report..p_report_smjj_wtxx('getThree')}");
System.out.println(data.toString());
Date date=new Date();
DateFormat sfm=new SimpleDateFormat("yyyyMMddHHmmss");
String datestr=sfm.format(date);
CSVUtil.exportCsv(new File("c://custsm/一节点/"+datestr+".csv"), data);
CSVUtil.exportCsv(new File("c://custsm/二节点/"+datestr+".csv"), data2);
CSVUtil.exportCsv(new File("c://custsm/三节点/"+datestr+".csv"), data3);
boolean a=smbPut("smb://CSC;xupeng:Ren1234@10.101.238.177/d/share/一节点", "c://custsm/一节点/"+datestr+".csv") ;
smbPut("smb://CSC;xupeng:Ren1234@10.101.238.177/d/share/二节点", "c://custsm/二节点/"+datestr+".csv") ;
smbPut("smb://CSC;xupeng:Ren1234@10.101.238.177/d/share/三节点", "c://custsm/三节点/"+datestr+".csv") ;
System.out.println(a+"-----------");
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 3, 24, TimeUnit.HOURS);
}
}