Advertisement
Guest User

hej

a guest
May 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 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 = ?"; // 1, userID
  12. public static final String SET_USERNAME = "SET userName = ? WHERE userID = ?"; //1, userName. 2, userID
  13. public static final String GET_PARTICIPANTCOUNTER = "SELECT participantCounter FROM USER WHERE userID = ?"; //1, userID
  14. public static final String INCREASE_PARTICIPANTCOUNTER = "UPDATE USER SET participantCounter = participantCounter + 1 WHERE userID = ?"; //1, userID
  15. public static final String GET_HOSTCOUNTER = "SELECT hostCounter FROM USER WHERE userID = ?"; //1, userID
  16. public static final String INCREASE_HOSTCOUNTER = "UPDATE USER SET hostCounter = hostCounter + 1 WHERE userID = ?"; //1, userID
  17. public static final String GET_GENDER = "SELECT gender FROM USER WHERE userID = ?"; //1, userID
  18. public static final String SET_GENDER = "SET gender = ? WHERE userID = ?"; //1, gender, 2, userID
  19. public static final String GET_AGE = "SELECT age FROM USER WHERE userID = ?"; //1, userID
  20. public static final String SET_AGE = "SET age = ? WHERE userID = ?"; // 1, age, 2 userID
  21. public static final String GET_NAME = "SELECT name FROM USER WHERE userID = ?"; // 1, userID
  22. public static final String SET_NAME = "SET name = ? WHERE userID = ?"; //1, name, 2, 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(String gender, 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.setString(1, gender);
  349. prstmt.setInt(2, inputUserID); // Switch to correct preparedStatment
  350. // input
  351.  
  352. ResultSet rs = prstmt.executeQuery();
  353.  
  354. rs.close();
  355. } catch (SQLException se) {
  356. se.printStackTrace();
  357. } catch (Exception e) {
  358. e.printStackTrace();
  359. }
  360.  
  361. finally {
  362. // Finally block used to close resources
  363. closePrstmt(prstmt);
  364. closeConnection(connection);
  365. }
  366. }
  367.  
  368. public int getAge(int inputUserID) throws SQLException {
  369. PreparedStatement prstmt = null;
  370. int age = -1;
  371. Connection connection = null;
  372. try {
  373. connection = getConnection();
  374. System.out.println("Database connected!");
  375. // Execute query
  376.  
  377. prstmt = connection.prepareStatement(GET_AGE);
  378.  
  379. prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  380. // input
  381.  
  382. ResultSet rs = prstmt.executeQuery();
  383.  
  384. while (rs.next()) {
  385.  
  386. // Retrieve by column name
  387.  
  388. age = rs.getInt("age");
  389.  
  390. // Display values
  391.  
  392. System.out.println("age" + age);
  393.  
  394. }
  395. rs.close();
  396. } catch (SQLException se) {
  397. se.printStackTrace();
  398. } catch (Exception e) {
  399. e.printStackTrace();
  400. }
  401.  
  402. finally {
  403. // Finally block used to close resources
  404. closePrstmt(prstmt);
  405. closeConnection(connection);
  406. }
  407. return age;
  408. }
  409.  
  410. public void setAge(int age, int inputUserID) throws SQLException {
  411.  
  412. PreparedStatement prstmt = null;
  413. Connection connection = null;
  414. try {
  415. connection = getConnection();
  416. System.out.println("Database connected!");
  417. // Execute query
  418.  
  419. prstmt = connection.prepareStatement(SET_AGE);
  420.  
  421. prstmt.setInt(1, age);
  422. prstmt.setInt(2, inputUserID); // Switch to correct preparedStatment
  423. // input
  424.  
  425. ResultSet rs = prstmt.executeQuery();
  426.  
  427. rs.close();
  428. } catch (SQLException se) {
  429. se.printStackTrace();
  430. } catch (Exception e) {
  431. e.printStackTrace();
  432. }
  433.  
  434. finally {
  435. // Finally block used to close resources
  436. closePrstmt(prstmt);
  437. closeConnection(connection);
  438. }
  439. }
  440.  
  441. public String getName(int inputUserID) throws SQLException {
  442. PreparedStatement prstmt = null;
  443. String name = null;
  444. Connection connection = null;
  445. try {
  446. connection = getConnection();
  447. System.out.println("Database connected!");
  448. // Execute query
  449.  
  450. prstmt = connection.prepareStatement(GET_NAME);
  451.  
  452. prstmt.setInt(1, inputUserID); // Switch to correct preparedStatment
  453. // input
  454.  
  455. ResultSet rs = prstmt.executeQuery();
  456.  
  457. while (rs.next()) {
  458.  
  459. // Retrieve by column name
  460.  
  461. name = rs.getString("username");
  462.  
  463. // Display values
  464.  
  465. System.out.println("userID" + name);
  466.  
  467. }
  468. rs.close();
  469. } catch (SQLException se) {
  470. se.printStackTrace();
  471. } catch (Exception e) {
  472. e.printStackTrace();
  473. }
  474.  
  475. finally {
  476. // Finally block used to close resources
  477. closePrstmt(prstmt);
  478. closeConnection(connection);
  479. }
  480. return name;
  481. }
  482.  
  483. public void setName(String name, int inputUserID) throws SQLException {
  484.  
  485. PreparedStatement prstmt = null;
  486. Connection connection = null;
  487. try {
  488. connection = getConnection();
  489. System.out.println("Database connected!");
  490. // Execute query
  491.  
  492. prstmt = connection.prepareStatement(SET_NAME);
  493.  
  494. prstmt.setString(1, name);
  495. prstmt.setInt(2, inputUserID); // Switch to correct preparedStatment
  496. // input
  497.  
  498. ResultSet rs = prstmt.executeQuery();
  499.  
  500. rs.close();
  501. } catch (SQLException se) {
  502. se.printStackTrace();
  503. } catch (Exception e) {
  504. e.printStackTrace();
  505. }
  506.  
  507. finally {
  508. // Finally block used to close resources
  509. closePrstmt(prstmt);
  510. closeConnection(connection);
  511. }
  512. }
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement