Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.81 KB | None | 0 0
  1.  
  2. /**
  3. * This ckass communicates with the database and analyzes the data
  4. *
  5. * @author Eden
  6. * @version 1.0
  7. */
  8.  
  9. package battleship.server;
  10.  
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17.  
  18.  
  19. public class SQLConnection {
  20.  
  21. private Connection conn;
  22. private PreparedStatement ps;
  23. private ResultSet rs;
  24. private Statement stmt;
  25.  
  26. /**
  27. * Initializing the co
  28. * @return connection to datadase, null if can't connect
  29. */
  30. private Connection createConnection() {
  31.  
  32. try {
  33. String url = "jdbc:sqlserver://localhost:1433;databaseName=Battleship"
  34. + ";integratedSecurity=true";
  35.  
  36. // Create the connection
  37. conn = DriverManager.getConnection(url);
  38.  
  39. // printing a message
  40. System.out.println("database works !!!");
  41.  
  42. } catch (SQLException ex) {
  43.  
  44. // print error msg
  45. System.out.println("error: unable to connect");
  46. conn = null;
  47. }
  48.  
  49. return conn;
  50. }
  51.  
  52. /**
  53. * close the database connection objects
  54. * the fonction Closing only the open objects
  55. */
  56. private void closeConnections() {
  57.  
  58. try {
  59.  
  60. if (!rs.isClosed()){
  61. rs.close();
  62. }
  63. if (!ps.isClosed()){
  64. ps.close();
  65. }
  66. if (!conn.isClosed()){
  67. conn.close();
  68. }
  69.  
  70. } catch (SQLException ex) {
  71. System.out.println("Error: close connection in SQLConnection class ");
  72. }
  73. }
  74.  
  75. /**
  76. *
  77. * @param ip user IP, max len 15
  78. * @param mac the user mac address, max len 20
  79. * @param Nickname max len 15
  80. * @param mail max len 50
  81. * @param password max len 10
  82. * @return -1 mail in use, -2 connection Error, Positive number is the userID
  83. */
  84. public String Register(String ip, String mac, String Nickname,
  85. String mail, String password) {
  86.  
  87. try {
  88.  
  89. // create connection to datacase
  90. conn = createConnection();
  91.  
  92. // the Query, '?' for parameters
  93. String sql = "exec P_registrar ?, ?, ?, ?, ?";
  94.  
  95. // Set parameters to query
  96. ps = conn.prepareStatement(sql);
  97. ps.setString(1, Nickname);
  98. ps.setString(2, mail);
  99. ps.setString(3, password);
  100. ps.setString(4, ip);
  101. ps.setString(5, mac);
  102.  
  103. // Run the query
  104. rs = ps.executeQuery();
  105.  
  106. // Get the rusult from SQL
  107. String result = "";
  108.  
  109. while (rs.next()) {
  110. result = rs.getString(1);
  111. }
  112.  
  113. // close the objects
  114. closeConnections();
  115.  
  116. // return clear result
  117. return result.trim();
  118. } catch (SQLException ex) {
  119. System.out.println(" -> " + ex);
  120. closeConnections();
  121. return "-2";
  122. }
  123. }
  124.  
  125. /**
  126. *
  127. * @param id
  128. * @param ip
  129. * @param mac
  130. * @return -1 connection Error
  131. */
  132. public String logout(int id, String ip, String mac) {
  133.  
  134. try {
  135.  
  136. // create connection to datacase
  137. conn = createConnection();
  138.  
  139. // the Query
  140. String sql = "exec P_logout ?, ?, ?, ?";
  141.  
  142. // Set parameters to query
  143. ps = conn.prepareStatement(sql);
  144. ps.setInt(1, id);
  145. ps.setString(2, ip);
  146. ps.setString(3, mac);
  147. ps.setInt(4, 0);
  148.  
  149. // Run the query
  150. rs = ps.executeQuery();
  151.  
  152. // Get the rusult from SQL
  153. String result = "";
  154.  
  155. while (rs.next()) {
  156. result = rs.getString(1);
  157. }
  158.  
  159. // close the objects
  160. closeConnections();
  161.  
  162. // return clear result
  163. return result.trim();
  164. } catch (SQLException ex) {
  165. System.out.println(" -> " + ex);
  166. closeConnections();
  167. return "-1";
  168. }
  169. }
  170.  
  171. /**
  172. *
  173. * @param mail
  174. * @param password
  175. * @param ip
  176. * @param mac
  177. * @return -1 user no exists, -2 user already logged in, -3 connection Error
  178. */
  179. public String login(String mail, String password, String ip, String mac) {
  180.  
  181. try {
  182.  
  183. // create connection to datacase
  184. conn = createConnection();
  185.  
  186. // the Query
  187. String sql = "exec P_login ?, ?, ?, ?";
  188.  
  189. // Set parameters to query
  190. ps = conn.prepareStatement(sql);
  191. ps.setString(1, mail);
  192. ps.setString(2, password);
  193. ps.setString(3, ip);
  194. ps.setString(4, mac);
  195.  
  196. // Run the query
  197. rs = ps.executeQuery();
  198.  
  199. // Get the rusult from SQL
  200. String result = "";
  201.  
  202. while (rs.next()) {
  203. result = rs.getString(1);
  204. }
  205.  
  206. // close the objects
  207. closeConnections();
  208.  
  209. // return clear result
  210. return result.trim();
  211. } catch (SQLException ex) {
  212. System.out.println(" -> " + ex);
  213. closeConnections();
  214. return "-3";
  215. }
  216. }
  217.  
  218. /**
  219. *
  220. * @param mail
  221. * @param ip
  222. * @param mac
  223. * @return result[0] -1 Email does not exist on system, null: connection
  224. * Error
  225. */
  226. public String[] resetPassword(String mail, String ip, String mac) {
  227.  
  228. String[] result = new String[2];
  229.  
  230. result[0] = "null";
  231. result[1] = "null";
  232.  
  233. try {
  234.  
  235. // create connection to datacase
  236. conn = createConnection();
  237.  
  238. // the Query
  239. String sql = "exec P_reset_password ?, ?, ?";
  240.  
  241. // Set parameters to query
  242. ps = conn.prepareStatement(sql);
  243. ps.setString(1, mail);
  244. ps.setString(2, ip);
  245. ps.setString(3, mac);
  246.  
  247. // Run the query
  248. rs = ps.executeQuery();
  249.  
  250. // Get the rusult from SQL
  251. while (rs.next()) {
  252. result[0] = rs.getString(1).trim();
  253. result[1] = rs.getString(2).trim();
  254. }
  255.  
  256. // close the objects
  257. closeConnections();
  258.  
  259. // return clear result
  260. return result;
  261. } catch (SQLException ex) {
  262. System.out.println(" -> " + ex);
  263. closeConnections();
  264. return result;
  265. }
  266. }
  267.  
  268. /**
  269. *
  270. * @param userID
  271. * @return result[0] game id, result[1] gust id, null: connection Error
  272. */
  273. public String[] findGame(int userID) {
  274.  
  275. String[] result = new String[2];
  276.  
  277. result[0] = "null";
  278. result[1] = "null";
  279.  
  280. try {
  281.  
  282. // create connection to datacase
  283. conn = createConnection();
  284.  
  285. // the Query
  286. String sql = "exec P_find_game ?";
  287.  
  288. // Set parameters to query
  289. ps = conn.prepareStatement(sql);
  290. ps.setInt(1, userID);
  291.  
  292. // Run the query
  293. rs = ps.executeQuery();
  294.  
  295. // Get the rusult from SQL
  296. while (rs.next()) {
  297. result[0] = rs.getString(1).trim();
  298. result[1] = rs.getString(2).trim();
  299. }
  300.  
  301. // close the objects
  302. closeConnections();
  303.  
  304. // return clear result
  305. return result;
  306. } catch (SQLException ex) {
  307. System.out.println(" -> " + ex);
  308. closeConnections();
  309. return result;
  310. }
  311. }
  312.  
  313. /**
  314. *
  315. * @param userID
  316. * @param gameID
  317. * @param board
  318. * @return 1 succeeded, -1 connection Error
  319. */
  320. public String setBoard(int userID, int gameID, String board) {
  321.  
  322. try {
  323.  
  324. // create connection to datacase
  325. conn = createConnection();
  326.  
  327. // the Query
  328. String sql = "exec P_insert_my_strategic_board ?, ?, ?";
  329.  
  330. // Set parameters to query
  331. ps = conn.prepareStatement(sql);
  332. ps.setInt(1, userID);
  333. ps.setInt(2, gameID);
  334. ps.setString(3, board);
  335.  
  336. // Run the query
  337. rs = ps.executeQuery();
  338.  
  339. // Get the rusult from SQL
  340. String result = "";
  341.  
  342. while (rs.next()) {
  343. result = rs.getString(1);
  344. }
  345.  
  346. // close the objects
  347. closeConnections();
  348.  
  349. // return clear result
  350. return result;
  351. } catch (SQLException ex) {
  352. System.out.println(" -> " + ex);
  353. closeConnections();
  354. return "-1";
  355. }
  356. }
  357.  
  358. /**
  359. * Disconnects inactive users
  360. *
  361. * @return -1 connection Error, number of users disconnected
  362. */
  363. public int disconnectUsers() {
  364.  
  365. try {
  366.  
  367. // create connection to datacase
  368. conn = createConnection();
  369.  
  370. // the Query
  371. String sql = "exec P_activity_check";
  372.  
  373. // Set parameters to query
  374. stmt = conn.createStatement();
  375.  
  376. // Run the query
  377. rs = stmt.executeQuery(sql);
  378.  
  379. // Get the rusult from SQL
  380. int result = 7;
  381.  
  382. while (rs.next()) {
  383. result = rs.getInt(1);
  384. }
  385.  
  386. // close the objects
  387. stmt.close();
  388. rs.close();
  389. conn.close();
  390.  
  391. return result;
  392. } catch (SQLException ex) {
  393. System.out.println(" -> " + ex);
  394. closeConnections();
  395. return -1;
  396. }
  397. }
  398.  
  399. /**
  400. *
  401. * @param id
  402. * @return -1 The server cut you off, -2 connection Error
  403. */
  404. public String updateLastSeen(int id) {
  405.  
  406. try {
  407.  
  408. // create connection to datacase
  409. conn = createConnection();
  410.  
  411. // the Query
  412. String sql = "exec P_update_last_seen ?";
  413.  
  414. // Set parameters to query
  415. ps = conn.prepareStatement(sql);
  416. ps.setInt(1, id);
  417.  
  418. // Run the query
  419. rs = ps.executeQuery();
  420.  
  421. // Get the rusult from SQL
  422. String result = "";
  423.  
  424. while (rs.next()) {
  425. result = rs.getString(1);
  426. }
  427.  
  428. // close the objects
  429. closeConnections();
  430.  
  431. // return clear result
  432. return result;
  433. } catch (SQLException ex) {
  434. System.out.println(" -> " + ex);
  435. closeConnections();
  436. return "-2";
  437. }
  438. }
  439.  
  440. /**
  441. *
  442. * @param userID
  443. * @param gameID
  444. * @param move
  445. * @return -2connection Error, 0 miss, 1 hit, 2 win
  446. */
  447. public String setMove(int userID, int gameID, String move) {
  448.  
  449. try {
  450.  
  451. // create connection to datacase
  452. conn = createConnection();
  453.  
  454. // the Query
  455. String sql = "exec P_guess_location ?, ?, ?";
  456.  
  457. // Set parameters to query
  458. ps = conn.prepareStatement(sql);
  459. ps.setInt(1, userID);
  460. ps.setInt(2, gameID);
  461. ps.setString(3, move);
  462.  
  463. // Run the query
  464. rs = ps.executeQuery();
  465.  
  466. // Get the rusult from SQL
  467. String result = "";
  468.  
  469. while (rs.next()) {
  470. result = rs.getString(1);
  471. }
  472.  
  473. // close the objects
  474. closeConnections();
  475.  
  476. // return clear result
  477. return result;
  478. } catch (SQLException ex) {
  479. System.out.println(" -> " + ex);
  480. closeConnections();
  481. return "-2";
  482. }
  483. }
  484.  
  485. /**
  486. *
  487. * @param gameID
  488. * @return -2 connection Error, 0 no partner else partner ID
  489. */
  490. public String LookForAPartner(int gameID) {
  491.  
  492. try {
  493.  
  494. // create connection to datacase
  495. conn = createConnection();
  496.  
  497. // the Query
  498. String sql = "exec P_wait_to_player ?";
  499.  
  500. // Set parameters to query
  501. ps = conn.prepareStatement(sql);
  502. ps.setInt(1, gameID);
  503.  
  504. // Run the query
  505. rs = ps.executeQuery();
  506.  
  507. // Get the rusult from SQL
  508. String result = "";
  509.  
  510. while (rs.next()) {
  511. result = rs.getString(1);
  512. }
  513.  
  514. // close the objects
  515. closeConnections();
  516.  
  517. // return clear result
  518. return result;
  519. } catch (SQLException ex) {
  520. System.out.println(" -> " + ex);
  521. closeConnections();
  522. return "-2";
  523. }
  524. }
  525.  
  526. /**
  527. *
  528. * @param userID
  529. * @param gameID
  530. * @return -1, loss, 0 The opponent's turn, 1 Your turn, 2 win, -2
  531. * connection Error
  532. */
  533. public String checkGameUpdates(int userID, int gameID) {
  534.  
  535. try {
  536.  
  537. // create connection to datacase
  538. conn = createConnection();
  539.  
  540. // the Query
  541. String sql = "exec what_new_in_game ?, ?";
  542.  
  543. // Set parameters to query
  544. ps = conn.prepareStatement(sql);
  545. ps.setInt(1, userID);
  546. ps.setInt(2, gameID);
  547.  
  548. // Run the query
  549. rs = ps.executeQuery();
  550.  
  551. // Get the rusult from SQL
  552. String result = "";
  553.  
  554. while (rs.next()) {
  555. result = rs.getString(1);
  556. }
  557.  
  558. // close the objects
  559. closeConnections();
  560.  
  561. // return clear result
  562. return result;
  563. } catch (SQLException ex) {
  564. System.out.println(" -> " + ex);
  565. closeConnections();
  566. return "-2";
  567. }
  568. }
  569.  
  570. /**
  571. *
  572. * @param userID
  573. * @return -2 connection Error, else the NickName
  574. */
  575. public String getNickName(int userID) {
  576.  
  577. try {
  578.  
  579. // create connection to datacase
  580. conn = createConnection();
  581.  
  582. // the Query
  583. String sql = "exec P_userID_to_NickName ?";
  584.  
  585. // Set parameters to query
  586. ps = conn.prepareStatement(sql);
  587. ps.setInt(1, userID);
  588.  
  589. // Run the query
  590. rs = ps.executeQuery();
  591.  
  592. // Get the rusult from SQL
  593. String result = "";
  594.  
  595. while (rs.next()) {
  596. result = rs.getString(1);
  597. }
  598.  
  599. // close the objects
  600. closeConnections();
  601.  
  602. // return clear result
  603. return result;
  604. } catch (SQLException ex) {
  605. System.out.println(" -> " + ex);
  606. closeConnections();
  607. return "-2";
  608. }
  609. }
  610.  
  611. public void FinishedGame(int gameID) {
  612.  
  613. try {
  614.  
  615. // create connection to datacase
  616. conn = createConnection();
  617.  
  618. // the Query
  619. String sql = "exec P_finished_game ?";
  620.  
  621. // Set parameters to query
  622. ps = conn.prepareStatement(sql);
  623. ps.setInt(1, gameID);
  624.  
  625. // Run the query
  626. rs = ps.executeQuery();
  627.  
  628. // Get the rusult from SQL
  629. String result = "";
  630.  
  631. while (rs.next()) {
  632. result = rs.getString(1);
  633. }
  634.  
  635. // close the objects
  636. closeConnections();
  637.  
  638. } catch (SQLException ex) {
  639. System.out.println(" -> " + ex);
  640. closeConnections();
  641. }
  642. }
  643.  
  644. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement