1d3f52d9f188dd8718e4f7694e28e0e8bde55f61.svn-base 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.toolkit.databak;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7. import java.util.Vector;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import com.persistence.service.PersistenceFactory;
  11. import com.persistence.service.SysPersistence;
  12. import com.sysmodel.datamodel.Parameter.DataBaseType;
  13. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  14. import com.toolkit.databak.vo.RsMetaDataVo;
  15. public class GetInsertTableDll{
  16. private static Log log = LogFactory.getLog(GetInsertTableDll.class);
  17. public static void getInsertDll(ArrayList<String> tables) throws Exception {
  18. String path = System.getProperty("user.dir").replace("\\", "/") + "/WebContent/WEB-INF/";
  19. SysModelImpl sysmodel = SysModelImpl.getInstance(path + "DataModel.xml");
  20. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  21. Connection con = null;
  22. Statement statament = null;
  23. ResultSet resultset = null;
  24. try {
  25. con = persistence.getConnection();
  26. for (int i = 0; i < tables.size(); i++) {
  27. String tableName = tables.get(i).toUpperCase();
  28. String sql = "select * from " + tableName;
  29. statament = con.createStatement();
  30. statament.setFetchSize(100);
  31. resultset = statament.executeQuery(sql);
  32. // ---------------------------------------------------
  33. ResultSetMetaData rsmd = resultset.getMetaData();
  34. int colCount = rsmd.getColumnCount();
  35. // 取得每一列列名、列类型
  36. String[][] fields = new String[colCount][2];
  37. for (int col = 1; col <= colCount; col++) {
  38. fields[col - 1][0] = rsmd.getColumnName(col);
  39. fields[col - 1][1] = rsmd.getColumnTypeName(col);
  40. }
  41. ArrayList<Vector<RsMetaDataVo>> al = new ArrayList<Vector<RsMetaDataVo>>();
  42. while (resultset.next()) {
  43. Vector<RsMetaDataVo> vecCol = new Vector<RsMetaDataVo>();
  44. for (int k = 0; k < fields.length; k++) {
  45. RsMetaDataVo RsMetaDataVo = new RsMetaDataVo();
  46. RsMetaDataVo.setColumnName(fields[k][0]);
  47. RsMetaDataVo.setColumnTypeName(fields[k][1]);
  48. RsMetaDataVo.setColumnValue(resultset.getString(fields[k][0]));
  49. vecCol.add(RsMetaDataVo);
  50. }
  51. al.add(vecCol);
  52. }
  53. // -----------------------------------------------------
  54. log.info("--生成表 " + tableName + "insert sql......");
  55. log.info("DELETE FROM " + tableName + ";");
  56. getSql(sysmodel.getDataSourceByCode(1).getDataBase(), tableName, al);
  57. log.info("--生成表 " + tableName + "insert sql end");
  58. }
  59. } catch (Exception ex) {
  60. ex.printStackTrace();
  61. throw ex;
  62. } finally {
  63. resultset.close();
  64. statament.close();
  65. con.close();
  66. }
  67. }
  68. private static String getSql(String database, String tableName,
  69. ArrayList<Vector<RsMetaDataVo>> al) {
  70. if (!al.isEmpty()) {
  71. String INSERTINTOCol = getINSERTINTOCol(tableName, al.get(0));
  72. for (int i = 0; i < al.size(); i++) {
  73. Vector<RsMetaDataVo> vecCol = al.get(i);
  74. StringBuffer sb = new StringBuffer();
  75. sb.append(INSERTINTOCol);
  76. sb.append("(");
  77. for (int j = 0; j < vecCol.size(); j++) {
  78. RsMetaDataVo RsMetaDataVo = vecCol.get(j);
  79. if (RsMetaDataVo.getColumnValue() == null) {
  80. sb.append("null");
  81. } else if (RsMetaDataVo.getColumnTypeName().equals("DATETIME")) {
  82. if (database.equals(DataBaseType.ORACLE)) {
  83. sb.append("to_date(" + RsMetaDataVo.getColumnValue() + ")");
  84. } else {
  85. sb.append("'" + RsMetaDataVo.getColumnValue() + "'");
  86. }
  87. } else {
  88. sb.append("'" + RsMetaDataVo.getColumnValue() + "'");
  89. }
  90. // sb.append(RsMetaDataVo.getColumnValue());
  91. if (j != vecCol.size() - 1)
  92. sb.append(", ");
  93. }
  94. sb.append(");");
  95. }
  96. }
  97. return null;
  98. }
  99. private static String getINSERTINTOCol(String tableName, Vector<RsMetaDataVo> vecCol) {
  100. StringBuffer sb = new StringBuffer("INSERT INTO " + tableName + " (");
  101. int len = vecCol.size();
  102. for (int j = 0; j < len; j++) {
  103. RsMetaDataVo RsMetaDataVo = vecCol.get(j);
  104. sb.append(RsMetaDataVo.getColumnName());
  105. if (j != len - 1)
  106. sb.append(", ");
  107. }
  108. sb.append(") VALUES ");
  109. return sb.toString();
  110. }
  111. public static void main(String[] args) throws Exception {
  112. // GetInsertTableDll dll = new GetInsertTableDll();
  113. ArrayList<String> tables = new ArrayList<String>();
  114. tables.add("BM$_Menu");
  115. // tables.add("AH_IsertSQLDll");
  116. GetInsertTableDll.getInsertDll(tables);
  117. }
  118. }