当前位置:   article > 正文

连接mysql操作的util方法_mysqlutil

mysqlutil

1.

  1. package cn.hls.xjx.util;
  2. import cn.hls.xjx.modules.resources.entity.RecordInfo;
  3. import cn.hls.xjx.modules.resources.entity.Res;
  4. import com.alibaba.dubbo.common.utils.CollectionUtils;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class updateInfoUtil {
  9. private static String driverName = "com.mysql.cj.jdbc.Driver";
  10. private static String dbURL = "jdbc:mysql://*********:3306/xjx1?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnectForPools=true";
  11. private static String userName = "test";
  12. private static String userPwd = "**********";
  13. public static void main(String[] args) {
  14. getUpdateInfoRes();
  15. }
  16. public static Connection getConnection() {
  17. try {
  18. Class.forName(driverName);
  19. Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
  20. return dbConn;
  21. } catch (ClassNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. }
  26. return null;
  27. }
  28. /**
  29. *description:获取到单机版家教 升级信息和资源关联表
  30. *@author: fuxb
  31. *@date: 2021/7/27 20:34
  32. @param
  33. *@return: java.util.List<cn.hls.xjx.modules.resources.entity.RecordInfo>
  34. */
  35. public static List<Res> getUpdateInfoRes() {
  36. Connection connection = getConnection();
  37. PreparedStatement pps = null;
  38. String sql = "select * from update_info_res r ";
  39. List<Res> list = new ArrayList<>();
  40. List<String> resourceIDList = new ArrayList<>();
  41. try {
  42. pps = connection.prepareStatement(sql);
  43. ResultSet rs = pps.executeQuery();
  44. while(rs.next()) {
  45. Res info = new Res();
  46. info.setResourceID(rs.getString("ResourceID"));
  47. info.setTitle(rs.getString("Title"));
  48. list.add(info);
  49. resourceIDList.add(info.getResourceID());
  50. System.out.println(info);
  51. }
  52. String resourceIDSql = convertListToString(resourceIDList);
  53. if(list.size() > 0){
  54. pps = connection.prepareStatement("delete from res_test where ResourceID in ("+resourceIDSql+")");
  55. pps.executeUpdate();
  56. for(int i=0;i<list.size();i++){
  57. String insertSql = "insert into res_test values ('"+
  58. list.get(i).getResourceID()+"','"+
  59. list.get(i).getTitle()+"','"+
  60. list.get(i).getKPID()+"',"+
  61. list.get(i).getKPRevDegree()+",'"+
  62. list.get(i).getVCPID()+"','"+
  63. list.get(i).getEditionID()+"',"+
  64. list.get(i).getModuleID()+",'"+
  65. list.get(i).getModuleName()+"','"+
  66. list.get(i).getCreator()+"','"+
  67. list.get(i).getCreateDate()+"','"+
  68. list.get(i).getAdaptor()+"','"+
  69. list.get(i).getAdaptDate()+"','"+
  70. list.get(i).getDescription()+"','"+
  71. list.get(i).getPublisher()+"','"+
  72. list.get(i).getContributor()+"','"+
  73. list.get(i).getType()+"','"+
  74. list.get(i).getMediaMaterial()+"',"+
  75. list.get(i).getFormat()+","+
  76. list.get(i).getDifficulty()+",'"+
  77. list.get(i).getIdentifier()+"','"+
  78. list.get(i).getParentResource()+"','"+
  79. list.get(i).getRights()+"','"+
  80. list.get(i).getVersion()+"','"+
  81. list.get(i).getSpecialProperty1()+"','"+
  82. list.get(i).getSpecialProperty2()+"','"+
  83. list.get(i).getSource()+"','"+
  84. list.get(i).getFlag()+"','"+
  85. list.get(i).getSpecialproperty3()+"','"+
  86. list.get(i).getSpecialproperty4()+"',"+
  87. list.get(i).getSubid()+",'"+
  88. list.get(i).getCopyright()+"',"+
  89. list.get(i).getSize()+",'"+
  90. list.get(i).getCoverimage()+"','"+
  91. list.get(i).getOrigin()+"','"+
  92. list.get(i).getKeywords()+"','"+
  93. list.get(i).getPixel()+"','"+
  94. list.get(i).getAuthorInTroduction()+"','')";
  95. String aflerInsertSql = insertSql.replace("'null'","''");
  96. System.out.println(aflerInsertSql);
  97. pps = connection.prepareStatement(aflerInsertSql); //最后一个字段是 SubKPID 没有到暂时置为空
  98. pps.executeUpdate();
  99. }
  100. }
  101. } catch (SQLException e) {
  102. // TODO Auto-generated catch block
  103. System.out.println("同步资源数据时报错:");
  104. e.printStackTrace();
  105. }
  106. close(pps, connection);
  107. return list;
  108. }
  109. /**
  110. * 将List<String>集合 转化为String
  111. * 如{"aaa","bbb"} To 'aaa','bbb'
  112. */
  113. public static String convertListToString(List<String> strlist) {
  114. StringBuffer sb = new StringBuffer();
  115. if (CollectionUtils.isNotEmpty(strlist)) {
  116. for (int i = 0; i < strlist.size(); i++) {
  117. if (i == 0) {
  118. sb.append("'").append(strlist.get(i)).append("'");
  119. } else {
  120. sb.append(",").append("'").append(strlist.get(i)).append("'");
  121. }
  122. }
  123. }
  124. return sb.toString();
  125. }
  126. public static List<RecordInfo> getProList() {
  127. Connection connection = getConnection();
  128. PreparedStatement pps = null;
  129. String sql = "select * from recordinfo r,user u where RI_At_szProblemID like '__JX%' and RI_At_szProblemID not in (select RI_At_szProblemID from recordinfo_jx) and r.RI_At_Creator = u.id and u.schoolId = '272440'";
  130. List<RecordInfo> list = new ArrayList<RecordInfo>();
  131. try {
  132. pps = connection.prepareStatement(sql);
  133. ResultSet rs = pps.executeQuery();
  134. while(rs.next()) {
  135. RecordInfo info = new RecordInfo();
  136. info.setPI_szAnswer(rs.getString("PI_szAnswer"));
  137. info.setRI_At_AbilityGrade(rs.getInt("RI_At_AbilityGrade"));
  138. info.setRI_At_AuditDate(rs.getString("RI_At_AuditDate"));
  139. info.setRI_At_AuditFlag(rs.getInt("RI_At_AuditFlag")); //倒库 添加flag为1
  140. info.setRI_At_Auditor(rs.getString("RI_At_Auditor"));
  141. info.setRI_At_bHaveAnalysis(rs.getBoolean("RI_At_bHaveAnalysis"));
  142. info.setRI_At_bHaveDetail(rs.getBoolean("RI_At_bHaveDetail"));
  143. info.setRI_At_bHaveHint(rs.getBoolean("RI_At_bHaveHint"));
  144. info.setRI_At_CreateDate(rs.getString("RI_At_CreateDate"));
  145. info.setRI_At_Creator(rs.getString("RI_At_Creator"));
  146. info.setRI_At_haveVideo(rs.getString("RI_At_haveVideo"));
  147. info.setRI_At_iDifCoef(rs.getInt("RI_At_iDifCoef"));
  148. info.setRI_At_iEachScore(rs.getInt("RI_At_iEachScore"));
  149. info.setRI_At_iEditonID(rs.getString("RI_At_iEditonID"));
  150. info.setRI_At_iRequireTime(rs.getInt("RI_At_iRequireTime"));
  151. info.setRI_At_iScore(rs.getInt("RI_At_iScore"));
  152. info.setRI_At_iSubject(rs.getString("RI_At_iSubject"));
  153. info.setRI_At_LTID(rs.getInt("RI_At_LTID"));
  154. info.setRI_At_LTNo(rs.getInt("RI_At_LTNo"));
  155. info.setRI_At_LTTBID(rs.getString("RI_At_LTTBID"));
  156. info.setRI_At_LTType(rs.getString("RI_At_LTType"));
  157. info.setRI_At_NoInPaper(rs.getString("RI_At_NoInPaper"));
  158. info.setRI_At_PaperID(rs.getString("RI_At_PaperID"));
  159. info.setRI_At_Prop1(rs.getString("RI_At_Prop1"));
  160. info.setRI_At_Prop2(rs.getString("RI_At_Prop2"));
  161. info.setRI_At_Prop3(rs.getString("RI_At_Prop3"));
  162. info.setRI_At_Provenance(rs.getString("RI_At_Provenance"));
  163. info.setRI_At_ScoreInPaper(rs.getString("RI_At_ScoreInPaper"));
  164. info.setRI_At_szCity(rs.getString("RI_At_szCity"));
  165. info.setRI_At_szCounty(rs.getString("RI_At_szCounty"));
  166. info.setRI_At_szKnowPointID(rs.getString("RI_At_szKnowPointID"));
  167. info.setRI_At_szKnowPointID(rs.getString("RI_At_szKnowPointID"));
  168. info.setRI_At_szKPID(rs.getString("RI_At_szKPID"));
  169. info.setRI_At_szKPIDZ(rs.getString("RI_At_szKPIDZ"));
  170. info.setRI_At_szKPName(rs.getString("RI_At_szKPName"));
  171. info.setRI_At_szNoInHomework(rs.getString("RI_At_szNoInHomework"));
  172. info.setRI_At_szNoInZD(rs.getInt("RI_At_szNoInZD"));
  173. info.setRI_At_szProblemID(rs.getString("RI_At_szProblemID"));
  174. info.setRI_At_szProbStyle(rs.getString("RI_At_szProbStyle"));
  175. info.setRI_At_szProvince(rs.getString("RI_At_szProvince"));
  176. info.setRI_At_szSchool(rs.getString("RI_At_szSchool"));
  177. info.setRI_At_szTBID(rs.getString("RI_At_szTBID"));
  178. info.setRI_At_szVCPID(rs.getString("RI_At_szVCPID"));
  179. info.setRI_At_szYear(rs.getString("RI_At_szYear"));
  180. info.setRI_At_szZDID(rs.getString("RI_At_szZDID"));
  181. info.setRI_At_UpdateDate(rs.getString("RI_At_UpdateDate"));
  182. info.setRI_Si_bIsDone(rs.getBoolean("RI_Si_bIsDone"));
  183. info.setRI_Si_bIsRight(rs.getBoolean("RI_Si_bIsRight"));
  184. info.setRI_Si_iErrorTimes(rs.getInt("RI_Si_iErrorTimes"));
  185. info.setRI_Si_iErrorTimes(rs.getInt("RI_Si_iErrorTimes"));
  186. info.setRI_Si_iLastCostTime(rs.getInt("RI_Si_iLastCostTime"));
  187. info.setRI_Si_iLastDoneTime(rs.getDate("RI_Si_iLastDoneTime"));
  188. info.setSubid(rs.getInt("subid"));
  189. list.add(info);
  190. System.out.println(info);
  191. }
  192. pps = connection.prepareStatement("insert into recordinfo_jx(RI_At_szProblemID) select RI_At_szProblemID from recordinfo r,user u where RI_At_szProblemID like '__JX%' and RI_At_szProblemID not in (select RI_At_szProblemID from recordinfo_jx) and r.RI_At_Creator = u.id and u.schoolId = '272440'");
  193. pps.executeUpdate();
  194. } catch (SQLException e) {
  195. // TODO Auto-generated catch block
  196. e.printStackTrace();
  197. }
  198. close(pps, connection);
  199. return list;
  200. }
  201. public static void close(PreparedStatement pps,Connection con) {
  202. if(pps != null) {
  203. try {
  204. pps.close();
  205. } catch (SQLException e) {
  206. // TODO Auto-generated catch block
  207. e.printStackTrace();
  208. }
  209. }
  210. if(con != null) {
  211. try {
  212. con.close();
  213. } catch (SQLException e) {
  214. // TODO Auto-generated catch block
  215. e.printStackTrace();
  216. }
  217. }
  218. }
  219. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/692574
推荐阅读
相关标签
  

闽ICP备14008679号