数据库表中的数据写入文件中(优化)

来源:互联网 发布:淘宝店怎么免费推广 编辑:程序博客网 时间:2024/06/04 23:26
package cn.du.quartz;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 测试工作类
 * @author dolying
 *
 */
public class TestJob implements Job{
//     SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
//        Date d = new Date();  
//        String returnstr = DateFormat.format(d);    
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
//            System.out.println(returnstr+"******");
            Properties properties = new Properties();
            try {
                properties.load(this.getClass().getResourceAsStream("/db.properties"));
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            String driver = properties.getProperty("dbDriver");
            String url = properties.getProperty("url");
            String user = properties.getProperty("userName");
            String password = properties.getProperty("password");
            
//            String driver = "oracle.jdbc.driver.OracleDriver";// 驱动字符串
//            String url = "jdbc:oracle:thin:@10.10.1.15:1550:paytest";// 链接字符串
//            String user = "posp";// 用户名
//            String password = "posp";// 密码
            Connection con = null;
            PreparedStatement pstm = null;
            ResultSet rs = null;
            boolean flag=false;
            File file = new File("G:/sql.txt");//创建一个文件
             if(file.exists()){
                 file.delete();
             }else{
                 try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            PrintStream ps = new PrintStream(out);
            try {
                Class.forName(driver);
                try {
                    con = DriverManager.getConnection(url, user, password);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String sql = "select ORDER_NO,CARD_ACCP_ID,CARD_ACCP_TERM_ID,AMT_TRANS,RESP_CODE,UPDT_DATE from TBL_N_TXN";
                pstm = con.prepareStatement(sql);
                rs = pstm.executeQuery();
                
                while (rs.next()) {
                    ps.print(rs.getString("ORDER_NO").trim()+"|");
                    ps.print(rs.getString("CARD_ACCP_ID")+"|");
                    ps.print(rs.getString("CARD_ACCP_TERM_ID")+"|");
                    ps.print(rs.getString("AMT_TRANS")+"|");
                    ps.print(rs.getString("RESP_CODE")+"|");
                    ps.println(rs.getString("UPDT_DATE"));
                 }              
                flag=true;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                    ps.close();
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    try {
                        pstm.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            }
            if(flag){
                System.out.println("执行成功!");
            }else{
                System.out.println("执行失败!");
            }
        }  
    
}





0 0
原创粉丝点击