从一个文件夹中遍历文件插入到数据库

来源:互联网 发布:自动编程软件 编辑:程序博客网 时间:2024/06/06 00:35

一:创建类

package com.files.insert;


import java.math.BigDecimal;
/**
 * 类用于读取文件存放
 * @author Frank.dai
 *
 */
public class InfoBean {


private BigDecimal seqNo;
private String billNo;
private String policyNo;
private String certiNo;
private BigDecimal amount;
public BigDecimal getSeqNo() {
return seqNo;
}
public void setSeqNo(BigDecimal seqNo) {
this.seqNo = seqNo;
}
public String getBillNo() {
return billNo;
}
public void setBillNo(String billNo) {
this.billNo = billNo;
}
public String getPolicyNo() {
return policyNo;
}
public void setPolicyNo(String policyNo) {
this.policyNo = policyNo;
}
public String getCertiNo() {
return certiNo;
}
public void setCertiNo(String certiNo) {
this.certiNo = certiNo;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}

二:创建properties文件

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://10.8.9.11:3306/trshdev_dw_aml?useUnicode=true&characterEncoding=UTF-8
jdbc.username = trshdev_dw_aml
jdbc.password = 123456


encrypt.algorithm=MD5
password.min_length=8
password.max_length=20

三:读取properties文件

package com.files.insert;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/**
 * 数据库连接
 * @author Frank.dai
 *
 */


public class BeyondbConnection {


public static Connection getConnection() {
Connection con = null;

Properties properties = new Properties();
//Thread.currentThread()获取当前运行的线程currentThread()是static的,只是返回当前线程对象。
InputStream in = Thread.currentThread().getClass().getResourceAsStream("/database.properties");
try {
//properties.load(new FileInputStream("/database.properties"));
properties.load(in);

String driver = properties.getProperty("driver");

if(driver != null){
System.setProperty("jdbc.drvers", driver);
}

String url = properties.getProperty("jdbc.url");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}




// String driver = "com.mysql.jdbc.Driver";
// String url = "jdbc:mysql://10.8.9.11:3306/trshdev_dw_aml";
// String user = "trshdev_dw_aml";
// String password = "123456";


// try {
//
// con = DriverManager.getConnection(url, user, password);
// } catch (SQLException ex) {
// Logger.getLogger(BeyondbConnection.class.getName()).log(Level.SEVERE, null, ex);
// }
return con;


}
}

四: 创建数据库执行语句

package com.files.insert;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * 对文件进行插入操作
 * @author Frank.dai
 *
 */
public class OperationDB {

private Connection con = null;
 
     public void addRcorder(InfoBean infoBean) throws SQLException {
     
         if(con == null){
              con = BeyondbConnection.getConnection();
         }
       
           String sql = "insert into t_pa_nb_list (seq_no,bill_no,policy_no,certi_no,amount) values(?,?,?,?,?)";
     
            PreparedStatement pstmt = con.prepareStatement(sql);//数据预处理
            pstmt.setBigDecimal(1, infoBean.getSeqNo());
            pstmt.setString(2, infoBean.getBillNo());
            pstmt.setString(3, infoBean.getPolicyNo());
            pstmt.setString(4, infoBean.getCertiNo());
            pstmt.setBigDecimal(5, infoBean.getAmount());
        
           
            pstmt.executeUpdate();   
    }
}

五:主函数执行

package com.files.insert;





import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * 主函数运行
 * @author Frank.dai
 *
 */
public class Text {


public static void main(String[] args){
Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, -1);    //得到前一天
        String  yestedayDate= new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
        String path = "D:\\JKLIFE\\REQ";
        String filePath = path + "\\" +yestedayDate;
       
        //判断文件夹下的文件
        File file = new File(filePath);
        if(file.exists()){
        File[] files = file.listFiles();
        if(files.length == 0){
        System.out.println("文件夹是空的");
        return;
        } else {
        for(File file2 : files){
        if(!file2.isDirectory()){// isDirectory 是目录
        // String filepathName = file2.getAbsolutePath();//返回抽象路径名的绝对路径名字符串
        String fileName = file2.getName();//获取文件名
        String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
        String a = "tmp";
        if(a.equals(prefix)){
        int i = fileName.lastIndexOf('.');            
        if ((i >-1) && (i < (fileName.length()))) {                
        String fileName1 = fileName.substring(0, i);
        String fileName2 = fileName1 +  "." +"txt";
        String fileName3 = filePath+"\\"+fileName2;
        System.out.println("获取带txt后缀的文件名:"+fileName2);
        System.out.println(prefix);
        ReadFile.readTxtFile(fileName3);
        }
        //String fileName1 = 
        }
        }else{
        String filepathName = file2.getAbsolutePath();
           System.out.println("文件:" +filepathName);
        }
        }
        }
        }else{
    System.out.println("文件不存在");
    }
        
        
  }
}
1 0
原创粉丝点击