package com.hthy.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccservice.Util.PropertyUtil;
import com.ccservice.Util.db.DBHelper;
import com.ccservice.Util.string.StringUtil;
/**
* DBHelper扩展类
* @author gaoyide
*
*/
public class ExtendDBHelper {
private static Connection GetCONN() {
Connection result = null;
// net.sourceforge.jtds.jdbc.Driver
try {
result = cpds.getConnection();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
};
private static DruidDataSource cpds = null;
//取得连接
private static boolean GetPool() {
if (cpds != null)
return true;
try {
cpds = new DruidDataSource();
String driverClass = PropertyUtil.getValue("jdbc.driver", "database-config.properties");
String sDBUrl = PropertyUtil.getValue("jdbc.url", "database-config.properties");
String sUserName = PropertyUtil.getValue("jdbc.username", "database-config.properties");
String sPassword = PropertyUtil.getValue("jdbc.password", "database-config.properties");
// driverClass = "net.sourceforge.jtds.jdbc.Driver";
// sDBUrl = "jdbc:jtds:sqlserver://192.168.0.5:1433/B2B_DB";
// sUserName = "b2c";
// sPassword = "5n0wbIrd";
cpds.setDriverClassName(driverClass);
cpds.setUrl(sDBUrl);
cpds.setUsername(sUserName);
cpds.setPassword(sPassword);
//_CONN = cpds.getConnection();
}
catch (Exception ex) {
return false;
}
return true;
}
//关闭连接
private static void CloseConn() {
try {
cpds.close();
cpds = null;
}
catch (Exception ex) {
// System.out.println(ex.getMessage());
cpds = null;
}
}
private static void CloseConn(Connection myCon) {
try {
myCon.close();
myCon = null;
}
catch (Exception ex) {
// System.out.println(ex.getMessage());
myCon = null;
}
}
public static int insertSql(String sSQL) {
GetPool();
Connection myCon = GetCONN();
int id = 0;
try {
Statement st = myCon.createStatement();
int row = st.executeUpdate(sSQL, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = st.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(row);
}
}
catch (Exception ex) {
ex.printStackTrace();
return id;
}
finally {
CloseConn(myCon);
}
return id;
}
public static int executeBatchInsert(JSONArray arr_busi) throws SQLException {
int i = 0;
//设置批量处理的数量
int batchSize = arr_busi.size();
GetPool();
Connection myCon = GetCONN();
try {
PreparedStatement stmt = myCon.prepareStatement(
"insert into account_information (username,password,order_date,order_number,regular_brigade_quantity) values (?,?,?,?,?)");
// 关闭事务自动提交 ,这一行必须加上
myCon.setAutoCommit(false);
for (Object obj : arr_busi) {
JSONObject o = (JSONObject) obj;
String username = o.getString("username");//账号
String password = o.getString("password");//密码
String regular_brigade_quantity = o.getString("regular_brigade_quantity");//常旅个数
String order_date = "";
String order_number="";
try {
order_date=o.getString("order_date");//订单时间
order_number = o.getString("order_number");//订单号
} catch (Exception e) {
// TODO: handle exception
}
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setString(3, order_date);
stmt.setString(4, order_number);
stmt.setString(5, regular_brigade_quantity);
stmt.addBatch();
i++;
}
stmt.executeBatch();
myCon.commit();
System.out.println("数据插入成功");
} catch (Exception e) {
// TODO: handle exception
}finally {
CloseConn(myCon);
}
return i;
}
}