一:创建类
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;