@Component
public class TmpTableUtils {
@Autowired
private JdbcTemplate jdbcTpl;
private String fname = "CODE";
public String getFName() {
return fname;
}
public String createTempTable() throws Exception {
String tableName = "TMP_" + OrderGenerator.newOrder();
String sqlBuffer = String.format("CREATE TABLE %s (%s varchar(40))", tableName, getFName());
jdbcTpl.execute(sqlBuffer);
return tableName;
}
public int prepareTempTableData(String tableName, List<String> fValues) throws Exception {
String insertSql = String.format("INSERT INTO %s ( %s ) VALUES (?)", tableName, getFName());
int[] updateSizeArr = jdbcTpl.batchUpdate(insertSql, new BatchPreparedStatementSetter(){
public void setValues(PreparedStatement ps,int i)throws SQLException
{
ps.setString(1, fValues.get(i));
}
public int getBatchSize()
{
return fValues.size();
}
});
return fValues.size();
}
public String createTempTable(String querySql) throws Exception {
String tableName = "TMP_" + OrderGenerator.newOrder();
String sqlBuffer = String.format("CREATE TABLE %s AS (%s)", tableName, querySql);
jdbcTpl.execute(sqlBuffer);
return tableName;
}
public Boolean dropTempTable(String tableName) throws Exception {
Boolean isDrop = false;
String sqlDrop = String.format("drop table %s ", tableName);
jdbcTpl.execute(sqlDrop);
isDrop = true;
return isDrop;
}
public Boolean clearTempTable(String tableName) throws Exception {
Boolean isTruncate = false;
String sqlDrop = String.format("truncate table %s ", tableName);
jdbcTpl.execute(sqlDrop);
isTruncate = true;
return isTruncate;
}
public List<Map<String, Object>> getSqlList(String querySql) {
return (List<Map<String, Object>>) jdbcTpl.queryForList(querySql);
}