Advertisement
Guest User

hej victor

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