Database.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package captiveportal;
  2. import java.sql.*;
  3. public class Database {
  4. private Connection conn = null;
  5. private Statement stmt = null;
  6. private ResultSet resultSet = null;
  7. private int count = 0;
  8. //#######Database details#######
  9. private String dbHost = "localhost"; //The ip or hostname of the database host e.g: localhost or 127.0.0.1 etc.
  10. private String dbPort = "3306"; //The port the database is listening to
  11. private String dbName = "CaptivePortal"; //The name of the scheme
  12. private String dbUser = "CaptivePortal"; //The username of the account that can 'Update and Select' on the scheme
  13. private String dbPass = "raspberry"; //The password of the account that can 'Update and Select' on the scheme
  14. //##############################
  15. //Create method for checking if ticket/lastname combination exists (On SUCCES => true else => false)
  16. public boolean Select(String ticket, String lastName) throws Exception {
  17. try {
  18. //Load the MySQL driver
  19. Class.forName("com.mysql.jdbc.Driver");
  20. //Setup the connection with the database
  21. conn = DriverManager.getConnection("jdbc:mysql://" +dbHost+ ":" +dbPort+ "/" +dbName, dbUser, dbPass);
  22. //Create statement to execute query
  23. stmt = conn.createStatement();
  24. //Execute query
  25. resultSet = stmt.executeQuery("select lastname, ticket from users where ticket='" +ticket+ "'and lastname='" +lastName+ "'");
  26. //Count the results that are returned (affected rows) this should be 1 (unique ticket/lastname combination)
  27. while(resultSet.next())
  28. {
  29. //increment count by 1
  30. count++;
  31. }
  32. if (count == 1)
  33. {
  34. //If there is 1 row with the ticket/lastname combination => return true
  35. return true;
  36. }
  37. else
  38. {
  39. return false;
  40. }
  41. //If there is an error, throw it
  42. } catch (Exception e) {
  43. throw e;
  44. //Finally close the connections
  45. } finally {
  46. if (stmt != null) stmt.close();
  47. if (conn != null) conn.close();
  48. }
  49. }
  50. //Create method for registering internet usage per user (On SUCCES => true else => false)
  51. public boolean Update(String ticket, String lastName) throws Exception {
  52. try {
  53. //Load the MySQL driver
  54. Class.forName("com.mysql.jdbc.Driver");
  55. //Setup the connection with the database
  56. conn = DriverManager.getConnection("jdbc:mysql://" +dbHost+ ":" +dbPort+ "/" +dbName, dbUser, dbPass);
  57. //Create statement to execute query
  58. stmt = conn.createStatement();
  59. //Execute query
  60. int result = stmt.executeUpdate("update users set internet = '1' where ticket='" +ticket+ "' and lastname='" +lastName+ "'");
  61. //Result is already an int so, if good => true else => false (executeUpdate returns the number of rows updated, so it should be 1)
  62. if(result == 1) {
  63. return true;
  64. }
  65. else {
  66. return false;
  67. }
  68. //If there is an error, throw it
  69. } catch (Exception e) {
  70. throw e;
  71. //Finally close the connections
  72. } finally {
  73. if (stmt != null) stmt.close();
  74. if (conn != null) conn.close();
  75. }
  76. }
  77. //Create method for resetting user in database (set internet to 0) (On SUCCES => true else => false)
  78. public boolean Reset(String ticket, String lastName) throws Exception {
  79. try {
  80. //Load the MySQL driver
  81. Class.forName("com.mysql.jdbc.Driver");
  82. //Setup the connection with the database
  83. conn = DriverManager.getConnection("jdbc:mysql://" +dbHost+ ":" +dbPort+ "/" +dbName, dbUser, dbPass);
  84. //Create statement to execute query
  85. stmt = conn.createStatement();
  86. //Execute query
  87. int result = stmt.executeUpdate("update users set internet = '0' where ticket='" +ticket+ "' and lastname='" +lastName+ "'");
  88. //Result is already an int so, if good => true else => false (executeUpdate returns the number of rows updated, so it should be 1)
  89. if(result == 1) {
  90. return true;
  91. }
  92. else {
  93. return false;
  94. }
  95. //If there is an error, throw it
  96. } catch (Exception e) {
  97. throw e;
  98. //Finally close the connections
  99. } finally {
  100. if (stmt != null) stmt.close();
  101. if (conn != null) conn.close();
  102. }
  103. }
  104. }