当前位置:   article > 正文

mysql工具类_mysqlutil工具类

mysqlutil工具类

  1. @Slf4j
  2. public class MysqlUtil {
  3. public static Connection connection(Zen zen, String database) {
  4. Connection conn = null;
  5. try {
  6. Class.forName("com.mysql.jdbc.Driver");
  7. conn = DriverManager.getConnection(zen.getMysql().getHost() + "/" + database, zen.getMysql().getUser(), zen.getMysql().getPwd());
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. return conn;
  12. }
  13. /**
  14. * 1、清空 数据库数据
  15. */
  16. public static void truncateTable(Zen zen, String database, List<String> tables) {
  17. Connection conn = connection(zen, database);
  18. ResultSet rs = null;
  19. Statement statement = null;
  20. Statement statement2 = null;
  21. try {
  22. statement = conn.createStatement();
  23. rs = statement.executeQuery("show tables");
  24. while (rs.next()) {
  25. String tableName = rs.getString("Tables_in_" + database);
  26. if (!tables.contains(tableName)) {
  27. String sql = "TRUNCATE " + tableName;
  28. log.info(sql);
  29. statement2 = conn.createStatement();
  30. statement2.execute(sql);
  31. }
  32. }
  33. rs.close();
  34. statement.close();
  35. statement2.close();
  36. conn.close();
  37. } catch (SQLException throwables) {
  38. throwables.printStackTrace();
  39. }
  40. }
  41. public static void execute(Zen zen, String database, List<String> tables) {
  42. Connection conn = connection(zen, database);
  43. ResultSet rs = null;
  44. Statement statement = null;
  45. Statement statement2 = null;
  46. try {
  47. statement = conn.createStatement();
  48. rs = statement.executeQuery("show tables");
  49. while (rs.next()) {
  50. String tableName = rs.getString("Tables_in_" + database);
  51. if (!tables.contains(tableName)) {
  52. String sql = "TRUNCATE " + tableName;
  53. log.info(sql);
  54. statement2 = conn.createStatement();
  55. statement2.execute(sql);
  56. }
  57. }
  58. rs.close();
  59. statement.close();
  60. statement2.close();
  61. conn.close();
  62. } catch (SQLException throwables) {
  63. throwables.printStackTrace();
  64. }
  65. }
  66. //修改操作
  67. public static void update(Zen zen, String database, String sql) {
  68. Connection conn = connection(zen, database);
  69. PreparedStatement statement = null;
  70. try {
  71. statement = conn.prepareStatement(sql);
  72. //执行SQL
  73. int ret = statement.executeUpdate();
  74. //回收资源
  75. statement.close();
  76. conn.close();
  77. } catch (Exception e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81. //修改操作
  82. public static String select(Zen zen, String database, String sql) throws Exception {
  83. Connection conn = connection(zen, database);
  84. PreparedStatement statement = null;
  85. ResultSet resultSet = null;
  86. statement = conn.prepareStatement(sql);
  87. resultSet = statement.executeQuery();
  88. String name = "";
  89. //遍历结果集合,结果集合非常类似于迭代器
  90. while (resultSet.next()) {
  91. // name = resultSet.getString("name");
  92. name = resultSet.getString(1);
  93. }
  94. //释放资源
  95. resultSet.close();
  96. statement.close();
  97. conn.close();
  98. return name;
  99. }
  100. }

pom.xml文件

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>5.1.24</version>
  5. </dependency>

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

闽ICP备14008679号