5f9fde1c3faeabd31e573c9090b837bf0cdddab8.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. package com.sinosoft.common.excel;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.text.DateFormat;
  12. import java.text.ParseException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.ArrayList;
  15. import java.util.Date;
  16. import java.util.HashMap;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Vector;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.ws.rs.POST;
  24. import javax.ws.rs.Path;
  25. import javax.ws.rs.ProduceMime;
  26. import javax.ws.rs.core.Context;
  27. import org.apache.log4j.Logger;
  28. import org.apache.poi.hssf.usermodel.HSSFCell;
  29. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  30. import org.apache.poi.hssf.usermodel.HSSFRow;
  31. import org.apache.poi.hssf.usermodel.HSSFSheet;
  32. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.ss.usermodel.Cell;
  35. import org.apache.poi.ss.usermodel.DateUtil;
  36. import org.apache.poi.xssf.usermodel.XSSFCell;
  37. import org.apache.poi.xssf.usermodel.XSSFRow;
  38. import org.apache.poi.xssf.usermodel.XSSFSheet;
  39. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  40. import com.formaction.Parameter;
  41. import com.formaction.Utils;
  42. import com.formaction.vo.Msg;
  43. import com.persistence.DbConnection;
  44. import com.sinosoft.common.upLoad.service.UploadManageImpl;
  45. import com.sinosoft.lz.system.util.JsonUtil;
  46. /**
  47. * 读取Excel将数据返回到頁面
  48. *
  49. * @author 彭志超
  50. *
  51. */
  52. @Path("/GeneratorCarExcelToDBTableTool/")
  53. public class GeneratorCarExcelToDBTableTool {
  54. private static final Logger log = Logger.getLogger(GeneratorCarExcelToDBTableTool.class);
  55. @ProduceMime("text/html")
  56. @POST
  57. @Path("/importExpert")
  58. public String importExpert(@Context HttpServletRequest request,
  59. @Context HttpServletResponse response) throws Exception{//新增保存
  60. // Msg m=new Msg();
  61. UploadManageImpl upload2 = new UploadManageImpl();
  62. String path = upload2.uploadFilePath(request, response);
  63. String result="";
  64. log.info("saveFile.path===="+path);
  65. if("success".equals(path.split(";")[1])){
  66. //将数据返回到界面
  67. path=path.split(";")[2];
  68. // String report_type=request.getParameter("report_type");
  69. GeneratorCarExcelToDBTableTool xte = new GeneratorCarExcelToDBTableTool();
  70. result=xte.readExcelByName(path);
  71. }else{
  72. //返回失败信息
  73. result="";
  74. }
  75. log.info("result======"+result);
  76. return result;
  77. }
  78. @ProduceMime("application/json")
  79. @POST
  80. @Path("/getGeneratorCarExcel")
  81. public String readExcelByName(String fileName) throws Exception {
  82. GeneratorCarExcelToDBTableTool tool = new GeneratorCarExcelToDBTableTool();
  83. // String fileName=Utils.getParameter("fileName", params) == null ? "" : Utils.getParameter(
  84. // "fileName", params);
  85. // String fileName="E:\\javaDemo\\person.xls";
  86. int i=fileName.lastIndexOf(".");
  87. String type=fileName.substring(i+1);
  88. log.info("fileName===="+fileName);
  89. String result="";
  90. if("xls".equals(type)){
  91. result=tool.readExcel(fileName,"person");
  92. }else if("xlsx".equals(type)){
  93. result= tool.readExcel2007(fileName,"person");
  94. }
  95. result="{\"rows\":"+result+"}";
  96. log.info(result);
  97. return result;
  98. }
  99. // public void readExcel2007(DBModel dbConn, String filePath, String tableName)
  100. @SuppressWarnings({ "unused", "rawtypes", "unchecked", "resource" })
  101. public String readExcel2007( String filePath, String tableName)
  102. throws Exception {
  103. String result="";
  104. try {
  105. // InputStream inp = new FileInputStream(filePath);
  106. // Workbook wb = WorkbookFactory.create(inp);
  107. XSSFWorkbook wb = new XSSFWorkbook(filePath);
  108. int sheetSize = wb.getNumberOfSheets();
  109. // for (int i = 0; i < sheetSize; i++) {
  110. List list=new ArrayList();
  111. XSSFSheet sheet = wb.getSheetAt(0);
  112. // Sheet sheet = wb.getSheetAt(i);
  113. for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
  114. // 迭代行
  115. XSSFRow row = (XSSFRow) rit.next();
  116. // 迭代单元格
  117. Vector datas = new Vector();
  118. StringBuffer sb=new StringBuffer();
  119. Map<String, String>lineMap=new HashMap<String, String>();
  120. for (Iterator cit = row.cellIterator(); cit.hasNext();) {
  121. // 定义集合datas用于存Excel中一个行的数据
  122. XSSFCell cell = (XSSFCell) cit.next();
  123. String value = getValue2007(cell);
  124. sb.append(value+"|");
  125. // 注意行和列是基于0索引的
  126. // System.out.print(cell.getRowIndex() + ":" + cell.getColumnIndex() + " ");
  127. datas.add(value);
  128. // System.out.println();
  129. // 开始操作单元格
  130. }
  131. String[] arr=sb.toString().split("[|]");
  132. list.add(arr);
  133. // result = fillMap(list);;
  134. // 向表中插入数据
  135. // DBFactory.insertData(dbConn, tableName, datas);
  136. }
  137. result = fillMap(list);
  138. System.out.println(result);
  139. // }
  140. } catch (FileNotFoundException e) {
  141. e.printStackTrace();
  142. throw e;
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. throw e;
  146. }
  147. return result;
  148. }
  149. /**
  150. * 將数据转换成json
  151. * @param list
  152. * @return
  153. * @throws UnsupportedEncodingException
  154. */
  155. public String fillMap(List<String[]> list) throws UnsupportedEncodingException{
  156. List<Map<String, String>>dataList=new ArrayList<Map<String,String>>();
  157. for(int i=0;i<list.size();i++){
  158. Map<String, String>tempMap=new HashMap<String, String>();
  159. String[]arr=list.get(i);
  160. // String[] CAR_LEVEL_265 = arr[2]==null ? null:arr[2].split(":");
  161. // String[] STORED_ENERGY_TYPE_265 = arr[4]==null ? null:arr[4].split(":");
  162. // String[] CAR_GAS_265 = arr[7]==null ? null:arr[7].split(":");
  163. // String[] IS_USED_265 = arr[14]==null ? null:arr[14].split(":");
  164. // String[] USED_STAT_265 = arr[15]==null ? null:arr[15].split(":");
  165. tempMap.put("CAR_NAME_265",new String(java.net.URLEncoder.encode(arr[0]==null ? "":arr[0].replace(" ", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  166. tempMap.put("CAR_TYPE_265",new String(java.net.URLEncoder.encode(arr[1]==null ? "":arr[1].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  167. // if(CAR_LEVEL_265!=null)
  168. tempMap.put("CAR_LEVEL_265", new String(java.net.URLEncoder.encode(arr[2]==null ? "":arr[2].replace("10kV", "138").replace("380V", "139"), "UTF-8").getBytes(), "ISO-8859-1"));
  169. // else
  170. // tempMap.put("CAR_LEVEL_265", "");
  171. tempMap.put("CAR_CONTENT_265", new String(java.net.URLEncoder.encode(arr[3]==null ? "":arr[3].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  172. // if(STORED_ENERGY_TYPE_265!=null)
  173. tempMap.put("STORED_ENERGY_TYPE_265", new String(java.net.URLEncoder.encode(arr[4]==null ? "":arr[4].replace("发电机", "1").replace("UPS", "2").replace("磁飞轮", "3"), "UTF-8").getBytes(), "ISO-8859-1"));
  174. // else
  175. // tempMap.put("STORED_ENERGY_TYPE_265","");
  176. tempMap.put("CAR_NUM_265", new String(java.net.URLEncoder.encode(arr[5]==null ? "":arr[5].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  177. tempMap.put("CAR_PRICE_265", new String(java.net.URLEncoder.encode(arr[6]==null ? "":arr[6].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  178. // if(CAR_GAS_265!=null)
  179. tempMap.put("CAR_GAS_265", new String(java.net.URLEncoder.encode(arr[7]==null ? "":arr[7].replace("汽油", "201").replace("柴油", "202"), "UTF-8").getBytes(), "ISO-8859-1"));
  180. // else
  181. // tempMap.put("CAR_GAS_265","");
  182. tempMap.put("CAR_PROVIDER_265",new String(java.net.URLEncoder.encode(arr[8]==null ? "":arr[8].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  183. tempMap.put("BUY_DATE_265", new String(java.net.URLEncoder.encode(arr[9]==null ? "":arr[9].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  184. tempMap.put("STORE_PLACE_265",new String(java.net.URLEncoder.encode(arr[10]==null ? "":arr[10].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  185. tempMap.put("CAR_INPIRE_265",new String(java.net.URLEncoder.encode(arr[11]==null ? "":arr[11].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1"));
  186. //new String(java.net.URLEncoder.encode(arr[12].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1")
  187. tempMap.put("OFFICE_TEL_265", arr[12]==null ? "":arr[12].replace("\n", ","));
  188. //new String(java.net.URLEncoder.encode(arr[13].replace("\n", ""), "UTF-8").getBytes(), "ISO-8859-1")
  189. tempMap.put("MOBILE_PHONE_265", arr[13]==null ? "":arr[13].replace("\n", ""));
  190. // if(IS_USED_265!=null)
  191. tempMap.put("IS_USED_265", new String(java.net.URLEncoder.encode(arr[14]==null ? "":arr[14].replace("可调用", "1").replace("不可调用", "2"), "UTF-8").getBytes(), "ISO-8859-1"));
  192. // else
  193. // tempMap.put("IS_USED_265", "");
  194. // if(USED_STAT_265!=null)
  195. tempMap.put("USED_STAT_265", new String(java.net.URLEncoder.encode(arr[15]==null ? "":arr[15].replace("未调拨", "1").replace("已调拨", "2"), "UTF-8").getBytes(), "ISO-8859-1"));
  196. // else
  197. // tempMap.put("USED_STAT_265", "");
  198. dataList.add(tempMap);
  199. }
  200. String result=JsonUtil.list2json(dataList);
  201. return result;
  202. }
  203. /**
  204. * 将Excel数据导入到表中
  205. *
  206. * @param filePath
  207. * Excel路径
  208. * @param tableName
  209. * 表名
  210. */
  211. // public void readExcel(DBModel dbConn, String filePath, String tableName)
  212. @SuppressWarnings({ "deprecation", "unchecked", "rawtypes", "resource" })
  213. public String readExcel( String filePath, String tableName)
  214. throws Exception {
  215. String result="";
  216. try {
  217. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( filePath));
  218. // 创建工作簿
  219. HSSFWorkbook workBook = new HSSFWorkbook(fs);
  220. log.info(workBook.getNumberOfSheets());
  221. // System.out.println("工作表个数 :" + workBook.getNumberOfSheets());
  222. for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
  223. // 创建工作表
  224. HSSFSheet sheet = workBook.getSheetAt(i);
  225. int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
  226. if (rows > 0) {
  227. sheet.getMargin(HSSFSheet.TopMargin);
  228. List list=new ArrayList();
  229. for (int r = 0; r < rows; r++) { // 行循环
  230. HSSFRow row = sheet.getRow(r);
  231. if (row != null && r != 0 && r != 1) {// 不取第一行,第二行
  232. int cells = row.getLastCellNum();// 获得列数
  233. // 定义集合datas用于存Excel中一个行的数据
  234. Vector datas = new Vector();
  235. String[]arr=new String[cells];
  236. List<String>paramList=new ArrayList<String>();
  237. for (short c = 0; c < cells; c++) { // 列循环
  238. HSSFCell cell = row.getCell(c);
  239. if (cell != null) {
  240. String value = getValue(cell);
  241. // System.out.println("第" + r + "行 " + "第" + c + "列:" + value);
  242. datas.add(value);
  243. paramList.add(value);
  244. arr[c]=value;
  245. }
  246. }
  247. list.add(arr);
  248. result = fillMap(list);
  249. // dataIntoDb(tableName, paramList);
  250. // 向表中插入数据
  251. // DBFactory.insertData(dbConn, tableName, datas);
  252. }
  253. }
  254. } else {
  255. }
  256. }
  257. } catch (Exception ex) {
  258. ex.printStackTrace();
  259. log.info(ex);
  260. throw ex;
  261. }
  262. return result;
  263. }
  264. public String getValue2007(XSSFCell cell) {
  265. String value = "";
  266. switch (cell.getCellType()) {
  267. case Cell.CELL_TYPE_STRING:
  268. // System.out.println(cell.getRichStringCellValue().getString());
  269. break;
  270. case Cell.CELL_TYPE_NUMERIC:
  271. if (DateUtil.isCellDateFormatted(cell)) {
  272. // System.out.println(cell.getDateCellValue());
  273. java.util.Date date = cell.getDateCellValue();
  274. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  275. value = format.format(date);
  276. } else {
  277. // System.out.println(cell.getNumericCellValue());
  278. value = String.valueOf(cell.getNumericCellValue());
  279. }
  280. break;
  281. case Cell.CELL_TYPE_BOOLEAN:
  282. // System.out.println(cell.getBooleanCellValue());
  283. value = " " + cell.getBooleanCellValue();
  284. break;
  285. case Cell.CELL_TYPE_FORMULA:
  286. // System.out.println(cell.getCellFormula());
  287. value = cell.getCellFormula();
  288. break;
  289. default:
  290. System.out.println();
  291. }
  292. return value;
  293. }
  294. /**
  295. * 获取Excel中某个单元格的值
  296. *
  297. * @param cell
  298. * @return
  299. * @throws ParseException
  300. */
  301. public String getValue(HSSFCell cell) throws ParseException {
  302. String value = "";
  303. String value1 = "";
  304. switch (cell.getCellType()) {
  305. case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
  306. System.out.println(cell);
  307. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  308. log.info("时间类型=============");
  309. // 如果是date类型则 ,获取该cell的date值
  310. Date d = cell.getDateCellValue();
  311. log.info(d);
  312. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  313. value = format.format(d);
  314. } else
  315. {// 纯数字
  316. //转换成文本格式
  317. cell.setCellType(Cell.CELL_TYPE_STRING);
  318. value = cell.getStringCellValue();
  319. }
  320. break;
  321. /* 此行表示单元格的内容为string类型 */
  322. case HSSFCell.CELL_TYPE_STRING: // 字符串型
  323. value = cell.getStringCellValue();
  324. break;
  325. case HSSFCell.CELL_TYPE_FORMULA:// 公式型
  326. // 读公式计算值
  327. value = String.valueOf(cell.getNumericCellValue());
  328. if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
  329. value = cell.getStringCellValue().toString();
  330. }
  331. cell.getCellFormula();
  332. break;
  333. case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
  334. value = " " + cell.getBooleanCellValue();
  335. break;
  336. /* 此行表示该单元格值为空 */
  337. case HSSFCell.CELL_TYPE_BLANK: // 空值
  338. value = "";
  339. break;
  340. case HSSFCell.CELL_TYPE_ERROR: // 故障
  341. value = "";
  342. break;
  343. default:
  344. value = cell.getStringCellValue().toString();
  345. }
  346. return value;
  347. }
  348. // public boolean dataIntoDb(String tabName,List<String>list) throws ClassNotFoundException, SQLException{
  349. // Class.forName("dm.jdbc.driver.DmDriver");
  350. // Connection conn = DriverManager.getConnection("jdbc:dm://192.168.1.16:5236","NWYJ","NWYJ123456");
  351. // String sql="INSERT INTO SYS_MSG_MODEL(FD_OBJECTID,MSG_MODEL_TYPE,MSG_MODEL_CONTENT,MSG_MODEL_RANGE,MSG_MODEL_COMP,MSG_MODEL_USER,MSG_MODEL_TIME,IS_DEL)VALUES(?,?,?,?,?,?,?,?)";
  352. // PreparedStatement ps=null;
  353. // conn.setAutoCommit(false);
  354. // ps = conn.prepareStatement(sql);
  355. // ps.setString(1, (long) ((Math.random()+1)*1000000000)+"");
  356. // ps.setString(2, list.get(0));
  357. // ps.setString(3,list.get(1));
  358. // ps.setString(4, list.get(2));
  359. // ps.setString(5, list.get(3));
  360. // ps.setString(6, list.get(4));
  361. // ps.setString(7, list.get(5));
  362. // ps.setString(8,list.get(6));
  363. // int k=ps.executeUpdate();
  364. // conn.commit();
  365. //// System.out.println("k====="+k);
  366. // return false;
  367. // }
  368. /**
  369. * 将界面传输过来的数据保存到数据库
  370. * @param params
  371. * @return
  372. * @throws ClassNotFoundException
  373. */
  374. @SuppressWarnings("static-access")
  375. @ProduceMime("application/json")
  376. @POST
  377. @Path("/generatorCarToDb")
  378. public Msg generatorCarToDb(String params) throws ClassNotFoundException{
  379. Msg m=new Msg();
  380. String json = Utils.getParameter("json", params) == null ? "" : Utils.getParameter(
  381. "json", params);
  382. log.info("json===" + json);
  383. String classId = Utils.getParameter("classId", params) == null ? "" : Utils
  384. .getParameter("classId", params);
  385. log.info("classId===" + classId);
  386. String deptId = Utils.getParameter("deptId", params) == null ? "" : Utils
  387. .getParameter("deptId", params);
  388. log.info("deptId===" + deptId);
  389. String userId = Utils.getParameter("userid", params) == null ? "" : Utils
  390. .getParameter("userid", params);
  391. log.info("userId===" + userId);
  392. json=json.replace("_"+classId, "");
  393. Connection conn = null;
  394. PreparedStatement st = null;
  395. DbConnection db = new DbConnection();
  396. List<GeneratorCarBean> list = JsonPluginsUtil.jsonToBeanList(json, GeneratorCarBean.class);
  397. String sql="INSERT INTO EMC_AM_GENERATOR_CAR (FD_OBJECTID,CAR_NAME,CAR_TYPE,CAR_LEVEL,CAR_CONTENT,"
  398. + "CAR_NUM,CAR_PRICE,CAR_GAS,CAR_PROVIDER,BUY_DATE,STORE_PLACE,CAR_INPIRE,OFFICE_TEL,MOBILE_PHONE,"
  399. + "IS_USED,USED_STAT,COMP_ID,IS_DEL,STORED_ENERGY_TYPE,UPDATEDATE,COLUMN_1,TYPEIN_PEOPLE) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  400. try {
  401. conn = db.getConnection();
  402. int num=0;
  403. conn.setAutoCommit(false);
  404. st = conn.prepareStatement(sql);
  405. for(int i=0;i<list.size();i++){
  406. GeneratorCarBean eb=list.get(i);
  407. log.info("eb.getCOMP_ID()===="+ eb.getCOMP_ID());
  408. st.setString(1, createMsgId(getMsgId()));
  409. st.setString(2, eb.getCAR_NAME() );
  410. st.setString(3, eb.getCAR_TYPE() );
  411. st.setString(4, eb.getCAR_LEVEL() );
  412. st.setString(5, eb.getCAR_CONTENT() );
  413. st.setString(6, eb.getCAR_NUM() );
  414. st.setString(7, eb.getCAR_PRICE() );
  415. st.setString(8, eb.getCAR_GAS() );
  416. st.setString(9, eb.getCAR_PROVIDER() );
  417. st.setString(10, eb.getBUY_DATE() );
  418. st.setString(11, eb.getSTORE_PLACE() );
  419. st.setString(12, eb.getCAR_INPIRE() );
  420. st.setString(13, eb.getOFFICE_TEL() );
  421. st.setString(14, eb.getMOBILE_PHONE() );
  422. st.setString(15, eb.getIS_USED());
  423. st.setString(16, eb.getUSED_STAT());
  424. // st.setString(17, eb.getCOMP_ID());
  425. st.setString(17, deptId);
  426. st.setString(18, "0");
  427. st.setString(19, eb.getSTORED_ENERGY_TYPE());
  428. st.setString(20, formatDate(new Date()));
  429. st.setString(21, "0");
  430. st.setString(22, userId);
  431. st.executeUpdate();
  432. num++;
  433. }
  434. if(num==list.size()){
  435. log.info("保存成功");
  436. conn.commit();
  437. m.setSucsess(new Boolean(true).toString());
  438. m.setInfo(Parameter.DELETE_SUCSESS);
  439. return m;
  440. }else{
  441. m.setSucsess(new Boolean(false).toString());
  442. m.setInfo(Parameter.DELETE_FAILURE);
  443. // st.executeBatch();
  444. conn.rollback();
  445. log.info("保存失败");
  446. return m;
  447. }
  448. } catch (SQLException e) {
  449. this.log.error(e.getMessage(), e);
  450. throw new ClassNotFoundException("DAO Layou: 消息保存", e);
  451. } finally {
  452. db.close(st);
  453. db.close(conn);
  454. }
  455. }
  456. public String formatDate(Date d) {
  457. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  458. String date = sd.format(d);
  459. return date;
  460. }
  461. @SuppressWarnings({ "unchecked", "rawtypes" })
  462. public List<String> getMsgId() throws ClassNotFoundException {
  463. String sql = "SELECT FD_OBJECTID FROM EMC_AM_GENERATOR_CAR";
  464. Connection conn = null;
  465. Statement stat = null;
  466. ResultSet rs = null;
  467. DbConnection db = new DbConnection();
  468. try {
  469. conn = db.getConnection();
  470. stat = conn.createStatement();
  471. rs = stat.executeQuery(sql);
  472. List<String> list = new ArrayList();
  473. while (rs.next()) {
  474. list.add(rs.getString("FD_OBJECTID"));
  475. }
  476. return list;
  477. } catch (SQLException e) {
  478. // this.log.error(e.getMessage(), e);
  479. throw new ClassNotFoundException("DAO Layou: 获得数据库消息ID集合"
  480. + sql, e);
  481. } finally {
  482. db.close(rs);
  483. db.close(stat);
  484. db.close(conn);
  485. }
  486. }
  487. public String createMsgId(List<String> list) throws ClassNotFoundException{
  488. long l=(long) ((Math.random()+1)*1000000000);
  489. String msgId=l+"";
  490. if(list.contains(msgId)){
  491. return createMsgId(list);
  492. }
  493. else{
  494. // log.info("msgid==="+msgId);
  495. return msgId;
  496. }
  497. }
  498. }