Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package com.scoresSystem.Vendor;
  2.  
  3.  
  4. import com.scoresSystem.Models.Score;
  5. import com.scoresSystem.Models.User;
  6.  
  7. import java.sql.*;
  8. import java.util.*;
  9. import java.util.Date;
  10.  
  11. /**
  12.  * Created by Bashar on 9/11/2017.
  13.  */
  14. public class DBManager {
  15.     //Connection
  16.     private static Connection connection;
  17.     private static Statement statement;
  18.  
  19.     private static void connect(){
  20.         try {
  21.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/scoresSystem", "root", "");
  22.             statement = connection.createStatement();
  23.         }catch (SQLException e){
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     private static void closeConnection(){
  29.         try {
  30.             connection.close();
  31.             statement.close();
  32.         } catch (SQLException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.     }
  37.  
  38.  
  39.     public static User getUser(String id) {
  40.         User targetUser = null;
  41.         try {
  42.             Class.forName("com.mysql.jdbc.Driver");
  43.  
  44.             connect();
  45.             ResultSet result = statement.executeQuery("SELECT * FROM Users WHERE ID=" + id);
  46.  
  47.             while (result.next()) {
  48.                 targetUser = new User(
  49.  
  50.                         result.getString("iid"),
  51.                         result.getString("id"),
  52.                         result.getString("name"),
  53.                         result.getString("email"),
  54.                         result.getString("password"),
  55.                         result.getString("type"),
  56.                         result.getString("createdBy"),
  57.                         result.getString("updatedBy"),
  58.                         result.getTimestamp("createdAt") != null ? new Date(result.getTimestamp("createdAt").getTime()) : null,
  59.                         result.getTimestamp("updatedAt") != null ? new Date(result.getTimestamp("updatedAt").getTime()) : null,
  60.                         getAllScores(result.getString("id"))
  61.  
  62.                 );
  63.                 break;
  64.             }
  65.  
  66.  
  67.             result.close();
  68.             closeConnection();
  69.  
  70.  
  71.         } catch (SQLException e) {
  72.             e.printStackTrace();
  73.  
  74.         } catch (ClassNotFoundException e) {
  75.             e.printStackTrace();
  76.         }
  77.  
  78.         return targetUser;
  79.  
  80.     }
  81.  
  82.  
  83.  
  84.     public static Score getLastScore(String id) {
  85.         Score targetScore = null;
  86.  
  87.         try{
  88.             Class.forName("com.mysql.jdbc.Driver");
  89.  
  90.             connect();
  91.             ResultSet result = statement.executeQuery("SELECT * FROM Scores WHERE ID=" + id);
  92.  
  93.             while (result.next()) {
  94.                 targetScore = new Score(
  95.  
  96.                         result.getString("iid"),
  97.                         result.getString("id"),
  98.                         result.getDouble("score"),
  99.                         result.getString("createdBy"),
  100.                         result.getString("updatedBy"),
  101.                         result.getTimestamp("createdAt") != null ? new Date(result.getTimestamp("createdAt").getTime()) : null,
  102.                         result.getTimestamp("updatedAt") != null ? new Date(result.getTimestamp("updatedAt").getTime()) : null
  103.  
  104.                 );
  105.                 break;
  106.             }
  107.             result.close();
  108.             closeConnection();
  109.  
  110.         }catch(ClassNotFoundException e){
  111.             e.printStackTrace();
  112.         }catch (SQLException e){
  113.             e.printStackTrace();
  114.         }
  115.  
  116.         return targetScore;
  117.     }
  118.  
  119.  
  120.     public static List<Score> getAllScores(String id) {
  121.         Score targetScore = null;
  122.         List<Score> scores = new ArrayList<>();
  123.  
  124.         try{
  125.             Class.forName("com.mysql.jdbc.Driver");
  126.  
  127.             //Connection
  128.             connect();
  129.             ResultSet result = statement.executeQuery("SELECT * FROM Scores WHERE ID=" + id);
  130.  
  131.             while (result.next()) {
  132.                 targetScore = new Score(
  133.  
  134.                         result.getString("iid"),
  135.                         result.getString("id"),
  136.                         result.getDouble("score"),
  137.                         result.getString("createdBy"),
  138.                         result.getString("updatedBy"),
  139.                         result.getTimestamp("createdAt") != null ? new Date(result.getTimestamp("createdAt").getTime()) : null,
  140.                         result.getTimestamp("updatedAt") != null ? new Date(result.getTimestamp("updatedAt").getTime()) : null
  141.  
  142.                 );
  143.                 scores.add(targetScore);
  144.             }
  145.  
  146.             result.close();
  147.             closeConnection();
  148.  
  149.  
  150.         }catch(ClassNotFoundException e){
  151.             e.printStackTrace();
  152.         }catch (SQLException e){
  153.             e.printStackTrace();
  154.         }
  155.  
  156.         return scores;
  157.     }
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement