Advertisement
Guest User

Untitled

a guest
Jul 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. package exam;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class DbService {
  8.  
  9. private static String dbName = "exam_3";
  10. private static String dbUser = "root";
  11. private static String dbPass = "coderslab";
  12.  
  13. private static Connection createConn() throws SQLException {
  14. String connUrl = "jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
  15. return DriverManager.getConnection(connUrl, dbUser, dbPass);
  16. }
  17. /**
  18.  
  19. * Execute insert query and return id of created element, if not then null
  20. * @param query
  21. * @param params
  22. * @return id or null
  23. * @throws SQLException
  24. */
  25. public static Integer insertIntoDatabase(String query, List<String> params) throws SQLException {
  26.  
  27. try(Connection conn = createConn()){
  28. String[] generatedColumns = {"id"};
  29. PreparedStatement pst = conn.prepareStatement(query, generatedColumns);
  30. if(params != null) {
  31. int i = 1;
  32. for (String p : params) {
  33. pst.setString(i++, p);
  34. }
  35. }
  36. pst.executeUpdate();
  37. ResultSet res = pst.getGeneratedKeys();
  38. if(res.next()) {
  39. return res.getInt(1);
  40. }
  41. else {
  42. return null;
  43. }
  44. }
  45. catch (SQLException e ){
  46. throw e;
  47. }
  48. }
  49.  
  50. public static List<String[]> getData(String query, List<String> params) throws SQLException{
  51.  
  52. try(Connection conn = createConn()){
  53.  
  54. //prepare query
  55. PreparedStatement st = getPreparedStatement(query, params, conn);
  56.  
  57. //execute and get results
  58. ResultSet rs = st.executeQuery();
  59.  
  60. //get columns from query
  61. ResultSetMetaData columns = rs.getMetaData();
  62.  
  63. //prepare list of results
  64. List<String[]> result = new ArrayList<>();
  65.  
  66. while(rs.next()){
  67.  
  68. //New String array for row data
  69. String[] row = new String[columns.getColumnCount()];
  70.  
  71. for(int j=1; j<=columns.getColumnCount(); j++){
  72. row[j-1] = rs.getString( columns.getColumnName(j) );
  73. }
  74. result.add(row);
  75. }
  76. return result;
  77. }
  78. catch (SQLException e){
  79. throw e;
  80. }
  81. }
  82.  
  83. public static int executeQuery(String query, List<String> params) throws SQLException{
  84. try(Connection conn = createConn()) {
  85. PreparedStatement st = getPreparedStatement(query, params, conn);
  86. return st.executeUpdate();
  87. }
  88. catch(SQLException e){
  89. throw e;
  90. }
  91. }
  92.  
  93. private static PreparedStatement getPreparedStatement(String query, List<String> params, Connection conn) throws SQLException {
  94. //prepare query
  95. PreparedStatement st = conn.prepareStatement(query);
  96. if (params != null) {
  97. int i = 1;
  98. for (String p : params) {
  99. st.setString(i++, p);
  100. }
  101. }
  102. return st;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement