Commit cf1b6113 authored by shenjunlin's avatar shenjunlin

重构代码

parent ec77eca0
package us.codecraft.webmagic.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.base.Preconditions;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.lang3.StringUtils;
import java.sql.SQLException;
public class MySQLTool {
private DruidDataSource dataSource;
......@@ -14,18 +15,50 @@ public class MySQLTool {
private String insertSql;
public MySQLTool setInsertSql(String insertSql){
this.insertSql = insertSql;
return this;
}
public MySQLTool(String url, String userName, String password){
private MySQLTool(Builder builder){
dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
dataSource.setUrl(builder.url);
dataSource.setUsername(builder.userName);
dataSource.setPassword(builder.password);
queryRunner = new QueryRunner(dataSource);
this.insertSql = builder.insertSql;
}
public static class Builder{
private String url;
private String userName;
private String password;
private String insertSql;
public Builder setUrl(String url) {
this.url = url;
return this;
}
public Builder setUserName(String userName) {
this.userName = userName;
return this;
}
public Builder setPassword(String password) {
this.password = password;
return this;
}
public Builder setInsertSql(String insertSql) {
this.insertSql = insertSql;
return this;
}
public MySQLTool build(){
Preconditions.checkArgument(StringUtils.isNotEmpty(this.url),"数据库连接信息不能为空");
Preconditions.checkArgument(StringUtils.isNotEmpty(this.userName),"数据库用户名不能为空");
Preconditions.checkArgument(StringUtils.isNotEmpty(this.password),"数据库密码不能为空");
Preconditions.checkArgument(StringUtils.isNotEmpty(this.insertSql),"数据库插入语句不能为空");
return new MySQLTool(this);
}
}
public void insert(Object...params) throws SQLException {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment