Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.15 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. import javax.naming.Context;
  4. import javax.naming.InitialContext;
  5. import javax.sql.DataSource;
  6.  
  7. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  8.  
  9. public class UserHandler {
  10.  
  11.     public static final String GET_USERNAME = "SELECT userName FROM USER WHERE userID = ?";
  12.     public static final String SET_USERNAME = "SET userName = ? WHERE userID = ?";
  13.     public static final String GET_PARTICIPANTCOUNTER = "SELECT participantCounter FROM USER WHERE userID = ?";
  14.     public static final String INCREASE_PARTICIPANTCOUNTER = "UPDATE USER SET participantCounter = participantCounter + 1 WHERE userID = ?";
  15.     public static final String GET_HOSTCOUNTER = "SELECT hostCounter FROM USER WHERE userID = ?";
  16.     public static final String INCREASE_HOSTCOUNTER = "UPDATE USER SET hostCounter = hostCounter + 1 WHERE userID = ?";
  17.     public static final String GET_GENDER = "SELECT gender FROM USER WHERE userID = ?";
  18.     public static final String SET_GENDER = "SET gender = ? WHERE userID = ?";
  19.     public static final String GET_AGE = "SELECT age FROM USER WHERE userID = ?";
  20.     public static final String SET_AGE = "SET age = ? WHERE userID = ?";
  21.     public static final String GET_NAME = "SELECT name FROM USER WHERE userID = ?";
  22.     public static final String SET_NAME = "SET name = ? WHERE userID = ?";
  23.  
  24.     public static DataSource getMySQLDataSource() { // Funkar denna som connect
  25.         MysqlDataSource mysqlDS = null;
  26.  
  27.         try {
  28.             mysqlDS = new MysqlDataSource();
  29.             mysqlDS.setURL("jdbc:mysql://localhost:3306/mydb");
  30.             mysqlDS.setUser("root");
  31.             mysqlDS.setPassword("");
  32.  
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.         return mysqlDS;
  37.     }
  38.  
  39.     public Connection getConnection() throws SQLException {
  40.         Connection connection = null;
  41.  
  42.         // Get the MySqlDataSource
  43.  
  44.         System.out.println("Verifying access");
  45.         DataSource dataSource = getMySQLDataSource();
  46.  
  47.         // Get connection from the database
  48.  
  49.         System.out.println("Connecting database...");
  50.         connection = dataSource.getConnection();
  51.  
  52.         return connection;
  53.  
  54.     }
  55.  
  56.     public void closePrstmt(PreparedStatement prstmt) {
  57.         try {
  58.             if (prstmt != null) {
  59.                 prstmt.close();
  60.             }
  61.         } catch (SQLException sqlException) {
  62.             sqlException.printStackTrace();
  63.         }
  64.  
  65.     }
  66.  
  67.     public void closeConnection(Connection connection) {
  68.         try {
  69.             if (connection != null) {
  70.                 connection.close();
  71.             }
  72.         } catch (SQLException sqlException) {
  73.             sqlException.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     public String getUsername(int inputUserID) throws SQLException {
  78.         PreparedStatement prstmt = null;
  79.         String username = null;
  80.         Connection connection = null;
  81.         try {
  82.             connection = getConnection();
  83.             System.out.println("Database connected!");
  84.             // Execute query
  85.  
  86.             prstmt = connection.prepareStatement(GET_USERNAME);
  87.  
  88.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  89.                                             // input
  90.  
  91.             ResultSet rs = prstmt.executeQuery();
  92.  
  93.             while (rs.next()) {
  94.  
  95.                 // Retrieve by column name
  96.  
  97.                 username = rs.getString("username");
  98.  
  99.                 // Display values
  100.  
  101.                 System.out.println("username: " + username);
  102.  
  103.             }
  104.             rs.close();
  105.         } catch (SQLException se) {
  106.             se.printStackTrace();
  107.         } catch (Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.  
  111.         finally {
  112.             // Finally block used to close resources
  113.             closePrstmt(prstmt);
  114.             closeConnection(connection);
  115.         }
  116.         return username;
  117.     }
  118.  
  119.     public void setUsername(int inputUserID, String inputUsername) throws SQLException {
  120.  
  121.         PreparedStatement prstmt = null;
  122.         Connection connection = null;
  123.         try {
  124.             connection = getConnection();
  125.             System.out.println("Database connected!");
  126.             // Execute query
  127.  
  128.             prstmt = connection.prepareStatement(SET_USERNAME);
  129.  
  130.             prstmt.setString(1, inputUsername);
  131.             prstmt.setInt(2, inputUserID); // Switch to correct preparedStatment
  132.                                             // input
  133.  
  134.             ResultSet rs = prstmt.executeQuery();
  135.  
  136.             rs.close();
  137.         } catch (SQLException se) {
  138.             se.printStackTrace();
  139.         } catch (Exception e) {
  140.             e.printStackTrace();
  141.         }
  142.  
  143.         finally {
  144.             // Finally block used to close resources
  145.             closePrstmt(prstmt);
  146.             closeConnection(connection);
  147.         }
  148.     }
  149.  
  150.     public int getParticipantCounter(int inputUserID) throws SQLException {
  151.         PreparedStatement prstmt = null;
  152.         int participantCounter = -1;
  153.         Connection connection = null;
  154.         try {
  155.             connection = getConnection();
  156.             System.out.println("Database connected!");
  157.             // Execute query
  158.  
  159.             prstmt = connection.prepareStatement(GET_PARTICIPANTCOUNTER);
  160.  
  161.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  162.                                             // input
  163.  
  164.             ResultSet rs = prstmt.executeQuery();
  165.  
  166.             while (rs.next()) {
  167.  
  168.                 // Retrieve by column name
  169.  
  170.                 participantCounter = rs.getInt("participantCounter");
  171.  
  172.                 // Display values
  173.  
  174.                 System.out.println("participantCounter" + participantCounter);
  175.  
  176.             }
  177.             rs.close();
  178.         } catch (SQLException se) {
  179.             se.printStackTrace();
  180.         } catch (Exception e) {
  181.             e.printStackTrace();
  182.         }
  183.  
  184.         finally {
  185.             // Finally block used to close resources
  186.             closePrstmt(prstmt);
  187.             closeConnection(connection);
  188.         }
  189.         return participantCounter;
  190.     }
  191.  
  192.     public void increaseParticipantCounter(int inputUserID) throws SQLException {
  193.  
  194.         PreparedStatement prstmt = null;
  195.         Connection connection = null;
  196.         try {
  197.             connection = getConnection();
  198.             System.out.println("Database connected!");
  199.             // Execute query
  200.  
  201.             prstmt = connection.prepareStatement(INCREASE_PARTICIPANTCOUNTER);
  202.  
  203.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  204.                                             // input
  205.  
  206.             ResultSet rs = prstmt.executeQuery();
  207.  
  208.             rs.close();
  209.         } catch (SQLException se) {
  210.             se.printStackTrace();
  211.         } catch (Exception e) {
  212.             e.printStackTrace();
  213.         }
  214.  
  215.         finally {
  216.             // Finally block used to close resources
  217.             closePrstmt(prstmt);
  218.             closeConnection(connection);
  219.         }
  220.  
  221.     }
  222.  
  223.     public int getHostCounter(int inputUserID) throws SQLException {
  224.         PreparedStatement prstmt = null;
  225.         int hostCounter = -1;
  226.         Connection connection = null;
  227.         try {
  228.             connection = getConnection();
  229.             System.out.println("Database connected!");
  230.             // Execute query
  231.  
  232.             prstmt = connection.prepareStatement(GET_HOSTCOUNTER);
  233.  
  234.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  235.                                             // input
  236.  
  237.             ResultSet rs = prstmt.executeQuery();
  238.  
  239.             while (rs.next()) {
  240.  
  241.                 // Retrieve by column name
  242.  
  243.                 hostCounter = rs.getInt("hostCounter");
  244.  
  245.                 // Display values
  246.  
  247.                 System.out.println("hostCounter" + hostCounter);
  248.  
  249.             }
  250.             rs.close();
  251.         } catch (SQLException se) {
  252.             se.printStackTrace();
  253.         } catch (Exception e) {
  254.             e.printStackTrace();
  255.         }
  256.  
  257.         finally {
  258.             // Finally block used to close resources
  259.             closePrstmt(prstmt);
  260.             closeConnection(connection);
  261.         }
  262.         return hostCounter;
  263.     }
  264.  
  265.     public void increaseHostCounter(int inputUserID) throws SQLException {
  266.  
  267.         PreparedStatement prstmt = null;
  268.         Connection connection = null;
  269.         try {
  270.             connection = getConnection();
  271.             System.out.println("Database connected!");
  272.             // Execute query
  273.  
  274.             prstmt = connection.prepareStatement(INCREASE_HOSTCOUNTER);
  275.  
  276.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  277.                                             // input
  278.  
  279.             ResultSet rs = prstmt.executeQuery();
  280.  
  281.             rs.close();
  282.         } catch (SQLException se) {
  283.             se.printStackTrace();
  284.         } catch (Exception e) {
  285.             e.printStackTrace();
  286.         }
  287.  
  288.         finally {
  289.             // Finally block used to close resources
  290.             closePrstmt(prstmt);
  291.             closeConnection(connection);
  292.         }
  293.     }
  294.  
  295.     public String getGender(int inputUserID) throws SQLException {
  296.         PreparedStatement prstmt = null;
  297.         String gender = null;
  298.         Connection connection = null;
  299.         try {
  300.             connection = getConnection();
  301.             System.out.println("Database connected!");
  302.             // Execute query
  303.  
  304.             prstmt = connection.prepareStatement(GET_GENDER);
  305.  
  306.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  307.                                             // input
  308.  
  309.             ResultSet rs = prstmt.executeQuery();
  310.  
  311.             while (rs.next()) {
  312.  
  313.                 // Retrieve by column name
  314.  
  315.                 gender = rs.getString("username");
  316.  
  317.                 // Display values
  318.  
  319.                 System.out.println("userID: " + gender);
  320.  
  321.             }
  322.             rs.close();
  323.         } catch (SQLException se) {
  324.             se.printStackTrace();
  325.         } catch (Exception e) {
  326.             e.printStackTrace();
  327.         }
  328.  
  329.         finally {
  330.             // Finally block used to close resources
  331.             closePrstmt(prstmt);
  332.             closeConnection(connection);
  333.         }
  334.         return gender;
  335.     }
  336.  
  337.     public void setGender(int inputUserID) throws SQLException {
  338.  
  339.         PreparedStatement prstmt = null;
  340.         Connection connection = null;
  341.         try {
  342.             connection = getConnection();
  343.             System.out.println("Database connected!");
  344.             // Execute query
  345.  
  346.             prstmt = connection.prepareStatement(SET_GENDER);
  347.  
  348.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  349.                                             // input
  350.  
  351.             ResultSet rs = prstmt.executeQuery();
  352.  
  353.             rs.close();
  354.         } catch (SQLException se) {
  355.             se.printStackTrace();
  356.         } catch (Exception e) {
  357.             e.printStackTrace();
  358.         }
  359.  
  360.         finally {
  361.             // Finally block used to close resources
  362.             closePrstmt(prstmt);
  363.             closeConnection(connection);
  364.         }
  365.     }
  366.  
  367.     public int getAge(int inputUserID) throws SQLException {
  368.         PreparedStatement prstmt = null;
  369.         int age = -1;
  370.         Connection connection = null;
  371.         try {
  372.             connection = getConnection();
  373.             System.out.println("Database connected!");
  374.             // Execute query
  375.  
  376.             prstmt = connection.prepareStatement(GET_AGE);
  377.  
  378.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  379.                                             // input
  380.  
  381.             ResultSet rs = prstmt.executeQuery();
  382.  
  383.             while (rs.next()) {
  384.  
  385.                 // Retrieve by column name
  386.  
  387.                 age = rs.getInt("age");
  388.  
  389.                 // Display values
  390.  
  391.                 System.out.println("age" + age);
  392.  
  393.             }
  394.             rs.close();
  395.         } catch (SQLException se) {
  396.             se.printStackTrace();
  397.         } catch (Exception e) {
  398.             e.printStackTrace();
  399.         }
  400.  
  401.         finally {
  402.             // Finally block used to close resources
  403.             closePrstmt(prstmt);
  404.             closeConnection(connection);
  405.         }
  406.         return age;
  407.     }
  408.  
  409.     public void setAge(int inputUserID) throws SQLException {
  410.  
  411.         PreparedStatement prstmt = null;
  412.         Connection connection = null;
  413.         try {
  414.             connection = getConnection();
  415.             System.out.println("Database connected!");
  416.             // Execute query
  417.  
  418.             prstmt = connection.prepareStatement(SET_AGE);
  419.  
  420.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  421.                                             // input
  422.  
  423.             ResultSet rs = prstmt.executeQuery();
  424.  
  425.             rs.close();
  426.         } catch (SQLException se) {
  427.             se.printStackTrace();
  428.         } catch (Exception e) {
  429.             e.printStackTrace();
  430.         }
  431.  
  432.         finally {
  433.             // Finally block used to close resources
  434.             closePrstmt(prstmt);
  435.             closeConnection(connection);
  436.         }
  437.     }
  438.  
  439.     public String getName(int inputUserID) throws SQLException {
  440.         PreparedStatement prstmt = null;
  441.         String name = null;
  442.         Connection connection = null;
  443.         try {
  444.             connection = getConnection();
  445.             System.out.println("Database connected!");
  446.             // Execute query
  447.  
  448.             prstmt = connection.prepareStatement(GET_NAME);
  449.  
  450.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  451.                                             // input
  452.  
  453.             ResultSet rs = prstmt.executeQuery();
  454.  
  455.             while (rs.next()) {
  456.  
  457.                 // Retrieve by column name
  458.  
  459.                 name = rs.getString("username");
  460.  
  461.                 // Display values
  462.  
  463.                 System.out.println("userID" + name);
  464.  
  465.             }
  466.             rs.close();
  467.         } catch (SQLException se) {
  468.             se.printStackTrace();
  469.         } catch (Exception e) {
  470.             e.printStackTrace();
  471.         }
  472.  
  473.         finally {
  474.             // Finally block used to close resources
  475.             closePrstmt(prstmt);
  476.             closeConnection(connection);
  477.         }
  478.         return name;
  479.     }
  480.  
  481.     public void setName(int inputUserID) throws SQLException {
  482.  
  483.         PreparedStatement prstmt = null;
  484.         Connection connection = null;
  485.         try {
  486.             connection = getConnection();
  487.             System.out.println("Database connected!");
  488.             // Execute query
  489.  
  490.             prstmt = connection.prepareStatement(SET_NAME);
  491.  
  492.             prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  493.                                             // input
  494.  
  495.             ResultSet rs = prstmt.executeQuery();
  496.  
  497.             rs.close();
  498.         } catch (SQLException se) {
  499.             se.printStackTrace();
  500.         } catch (Exception e) {
  501.             e.printStackTrace();
  502.         }
  503.  
  504.         finally {
  505.             // Finally block used to close resources
  506.             closePrstmt(prstmt);
  507.             closeConnection(connection);
  508.         }
  509.     }
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement