Sqltools.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.src.utils;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. /**
  8. *
  9. *
  10. */
  11. public class Sqltools {
  12. private static Connection cnn = null;
  13. private static PreparedStatement ps = null;
  14. private static ResultSet rs = null;
  15. private static String url = "jdbc:mysql://localhost:3306/sport";
  16. private static String username = "root";
  17. private static String password = "123456";
  18. private static String driver = "com.mysql.cj.jdbc.Driver";//
  19. static {
  20. try {
  21. Class.forName(driver);
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. public static Connection getConnection() {
  27. try {
  28. cnn = DriverManager.getConnection(url, username, password);
  29. } catch (SQLException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. return cnn;
  34. }
  35. public static ResultSet excuteQuery(String sql, String[] parameters) {
  36. try {
  37. cnn = getConnection();
  38. ps = cnn.prepareStatement(sql);
  39. if (parameters != null && !parameters.equals("")) {
  40. for (int i = 0; i < parameters.length; i++) {
  41. ps.setString(i + 1, parameters[i]);
  42. }
  43. }
  44. rs = ps.executeQuery();
  45. } catch (SQLException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. return rs;
  50. }
  51. /**
  52. *
  53. *
  54. *
  55. * @param sql
  56. * @param parameters
  57. */
  58. public static boolean excuteUpdates(String sql, String[] parameters) {
  59. boolean b=true;
  60. try {
  61. cnn = getConnection();
  62. ps = cnn.prepareStatement(sql);
  63. if (parameters != null && !parameters.equals("")) {
  64. for (int i = 0; i < parameters.length; i++) {
  65. ps.setString(i + 1, parameters[i]);
  66. }
  67. }
  68. ps.executeUpdate();
  69. } catch (SQLException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. b=false;
  73. throw new RuntimeException(e.getMessage());
  74. }finally{
  75. close(null,ps,cnn);
  76. }
  77. return b;
  78. }
  79. /**
  80. *
  81. *
  82. * @param rs
  83. * @param ps
  84. * @param cnn
  85. */
  86. public static void close(ResultSet rs, PreparedStatement ps, Connection cnn) {
  87. if (rs != null) {
  88. try {
  89. rs.close();
  90. } catch (SQLException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. }
  94. rs = null;
  95. }
  96. if (ps != null) {
  97. try {
  98. ps.close();
  99. } catch (SQLException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. ps = null;
  104. }
  105. if (cnn != null) {
  106. try {
  107. cnn.close();
  108. } catch (SQLException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. cnn = null;
  113. }
  114. }
  115. /**
  116. *
  117. *
  118. *
  119. * @return
  120. */
  121. public static Connection getCnn() {
  122. return cnn;
  123. }
  124. public static PreparedStatement getPs() {
  125. return ps;
  126. }
  127. public static ResultSet getRs() {
  128. return rs;
  129. }
  130. }