Guest User

Untitled

a guest
Dec 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class DBService {
  6.  
  7.  
  8. private static String dbUrl;
  9. private final static String dbUser = "root";
  10. private final static String dbPass = "coderslab";
  11.  
  12. private static void initParams(String dbName){
  13. dbUrl = "jdbc:mysql://127.0.0.1:3306/"+dbName+"?useSSL=false&characterEncoding=UTF8";
  14. }
  15.  
  16. public static Connection getConnection(String dbName) throws SQLException {
  17. initParams(dbName);
  18. return DriverManager.getConnection(dbUrl, dbUser, dbPass);
  19. }
  20.  
  21.  
  22. public static void executeUpdate(String query, String dbName) throws SQLException{
  23.  
  24. // try (
  25. // Connection conn = getConnection(dbName);
  26. // Statement stmt = conn.createStatement();
  27. // ) {
  28. //
  29. // stmt.executeUpdate(query);
  30. //
  31. // } catch (Exception e) {
  32. // e.printStackTrace();
  33. // }
  34.  
  35. executeQuery(query, null, dbName);
  36. }
  37.  
  38.  
  39. public static int executeInsert(String query, String[] params ,String dbName) throws Exception{
  40.  
  41. try(Connection conn = DBService.getConnection(dbName)){
  42.  
  43. String generatedColumns[] = { "ID" };
  44. PreparedStatement st = conn.prepareStatement(query, generatedColumns);
  45.  
  46. setParams(params, st);
  47.  
  48. st.executeUpdate();
  49. ResultSet rs = st.getGeneratedKeys();
  50. if (rs.next()) {
  51. int id = rs.getInt(1);
  52. return id;
  53. }else{
  54. throw new Exception("No new id");
  55. }
  56.  
  57. }catch (SQLException e){
  58. throw e;
  59. }
  60.  
  61. }
  62.  
  63. public static void executeQuery(String query, String[] params ,String dbName) throws SQLException{
  64.  
  65. try(Connection conn = DBService.getConnection(dbName)){
  66.  
  67. PreparedStatement st = conn.prepareStatement(query);
  68.  
  69. setParams(params, st);
  70.  
  71. st.executeUpdate();
  72.  
  73.  
  74. }catch (SQLException e){
  75. throw e;
  76. }
  77.  
  78. }
  79.  
  80. /**
  81. * Returns list of rows which are 1D arrays
  82. * @param query
  83. * @param params
  84. * @param dbName
  85. * @return
  86. */
  87. public static List<String[]> getData(String query, String[] params, String dbName) throws SQLException{
  88.  
  89. try(Connection conn = getConnection(dbName)){
  90.  
  91. PreparedStatement st = conn.prepareStatement(query);
  92. setParams(params, st);
  93.  
  94. //execute preparestatement and get results
  95. ResultSet rs = st.executeQuery();
  96.  
  97. //get informations about table ex. columns names, columns count
  98. ResultSetMetaData data = rs.getMetaData();
  99.  
  100. //we create result LIST
  101. List<String[]> result = new ArrayList<>();
  102.  
  103. while(rs.next()){
  104.  
  105. String[] row = new String[data.getColumnCount()];
  106.  
  107. //iterate over columns names and fill data to row array
  108. for(int i=1; i<=data.getColumnCount(); i++){
  109. String currentColumnName = data.getColumnName(i);
  110. row[i-1] = rs.getString(currentColumnName);
  111. }
  112.  
  113. //we filled row array which add to resultList
  114. result.add(row);
  115.  
  116. }
  117.  
  118. return result;
  119.  
  120. }catch (SQLException e){
  121. throw e;
  122. }
  123.  
  124.  
  125. }
  126.  
  127. private static void setParams(String[] params, PreparedStatement st) throws SQLException {
  128. if (params != null) {
  129. int paramNumber = 1;
  130. for (String param : params) {
  131. st.setString(paramNumber, param);
  132. paramNumber++;
  133. }
  134. }
  135. }
  136.  
  137.  
  138. }
Add Comment
Please, Sign In to add comment