Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Date;
  8.  
  9.  
  10. public class GameLogger {
  11.  
  12. Connection fConnection;
  13.  
  14. public static void main(String[] args)
  15. {
  16. new GameLogger();
  17. }
  18.  
  19. public GameLogger()
  20. {
  21. fConnection = getConnection();
  22. printTable("User");
  23. changePassword("Samat", "a");
  24. printTable("User");
  25. try {
  26. fConnection.close();
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. /**
  33. * Returns a connection to the database named LogBook.db
  34. * @return
  35. */
  36. private Connection getConnection()
  37. {
  38. Connection con;
  39.  
  40. //Find database Classes
  41. try {
  42. Class.forName("org.sqlite.JDBC");
  43. } catch (ClassNotFoundException e) {
  44. System.out.println("Unable to find database");
  45. System.exit(2);
  46. }
  47. //Connect to database
  48. try {
  49. con = DriverManager.getConnection("jdbc:sqlite:LogBook.db");
  50. return con;
  51. } catch (SQLException e) {
  52. System.out.println("Unable to find database");
  53. System.exit(2);
  54. }
  55.  
  56. return null;
  57. }
  58.  
  59. /**
  60. * Adds a new user to the database
  61. * @param aUser Username
  62. * @param aPass Password
  63. * @param aName Firstname
  64. * @param aSurname Surname
  65. * @param aEmail Email
  66. */
  67. private void createUser(String aUser, String aPass, String aName, String aSurname, String aEmail)
  68. {
  69. try {
  70. Statement lStatement = fConnection.createStatement();
  71. String statement = "INSERT INTO USER VALUES(\"" + aUser + "\",\""
  72. + aPass + "\",\""
  73. + aName + "\",\""
  74. + aSurname + "\",\""
  75. + aEmail + "\","
  76. + "0);";
  77. lStatement.executeUpdate(statement);
  78. } catch (SQLException e) {
  79. System.out.println("Unable to add new user");
  80. }
  81. }
  82.  
  83. /**
  84. * Adds a new developer to the database
  85. * @param aName Name of developer
  86. */
  87. private void createDeveloper(String aName)
  88. {
  89. try
  90. {
  91. Statement lStatement = fConnection.createStatement();
  92. String statement = "INSERT INTO Developer VALUES(\"" + aName +"\")";
  93. lStatement.executeUpdate(statement);
  94. } catch(SQLException e)
  95. {
  96. System.out.println("Unable to add new Developer");
  97. }
  98. }
  99.  
  100. /**
  101. * Adds a new platform to the database
  102. * @param aName platform name
  103. * @param aDesc platform description
  104. */
  105. private void createPlatform(String aName, String aDesc)
  106. {
  107. try
  108. {
  109. Statement lStatement = fConnection.createStatement();
  110. String statement = "INSERT INTO Platform VALUES(\""
  111. + aName + "\",\""
  112. + aDesc + "\");";
  113. lStatement.executeUpdate(statement);
  114. } catch(SQLException e)
  115. {
  116. System.out.println("Unable to add new Developer");
  117. }
  118. }
  119.  
  120. /**
  121. * Adds a new page comment
  122. * @param aMadeBy Person who made comment
  123. * @param aMadeTo Person who the comment is aimed at
  124. * @param aTime Time it was made
  125. * @param aComment The comment
  126. */
  127. private void createPageComment(String aMadeBy, String aMadeTo, long aTime, String aComment)
  128. {
  129. try
  130. {
  131. long time = new Date().getTime();
  132. Statement lStatement = fConnection.createStatement();
  133. String statement = "INSERT INTO Platform VALUES(\"" + aMadeBy + "\",\""
  134. + aMadeTo + "\","
  135. + time + ",\""
  136. + aComment + "\",);";
  137. lStatement.executeUpdate(statement);
  138. } catch(SQLException e)
  139. {
  140. System.out.println("Unable to add new Developer");
  141. }
  142. }
  143.  
  144. /**
  145. * Adds a new game to the database with the default rating of 0
  146. * @param aGame GameName
  147. * @param aDesc Description of game
  148. * @param aDev Developer
  149. * @param aPlat Platform
  150. */
  151. private void createGame(String aGame, String aDesc, String aDev, String aPlat)
  152. {
  153. try
  154. {
  155. Statement lStatement = fConnection.createStatement();
  156. String statement = "INSERT INTO Game VALUES(\"" + aGame + "\",\""
  157. + aDesc + "\","
  158. + 0 + ","
  159. + 0 + ",\""
  160. + aDev + "\",\""
  161. + aPlat + "\");";
  162. lStatement.executeUpdate(statement);
  163. } catch(SQLException e)
  164. {
  165. System.out.println("Unable to add new Developer");
  166. }
  167. }
  168.  
  169. /**
  170. * TODO: Use gamename to find platform and developername also get time? Also implement this method!
  171. * @param aComment
  172. * @param aGameName
  173. */
  174. private void createGameComment(String aComment, String aGameName)
  175. {
  176. long time = new Date().getTime();]
  177. try
  178. {
  179. Statement lStatement = fConnection.createStatement();
  180. String statement = "DELETE FROM GameComment WHERE CommentText = " +
  181. "\"" + aComment + "\" " +
  182. "AND Username = \"" + aUser + "\"" +
  183. "AND Time = " + aTime +
  184. ";";
  185. lStatement.executeUpdate(statement);
  186. } catch (SQLException e)
  187. {
  188.  
  189. }
  190. }
  191.  
  192. /**
  193. * TODO: Implement this method, grab the time and get developer + platform from game table
  194. * @param aStatus Status of completeion
  195. * @param aComment Comment on game
  196. * @param aUser Username
  197. * @param aGameName Game name
  198. */
  199. private void createOwnedGame(String aStatus, String aComment, String aUser, String aGameName)
  200. {
  201. }
  202.  
  203. private void deleteGameComment(long aTime, String aUser, String aComment)
  204. {
  205. try
  206. {
  207. Statement lStatement = fConnection.createStatement();
  208. String statement = "DELETE FROM GameComment WHERE CommentText = " +
  209. "\"" + aComment + "\" " +
  210. "AND Username = \"" + aUser + "\"" +
  211. "AND Time = " + aTime +
  212. ";";
  213. lStatement.executeUpdate(statement);
  214. } catch(SQLException e)
  215. {
  216. System.out.println("Unable to add new Developer");
  217. }
  218. }
  219.  
  220. /**
  221. * Prints out the contents of a table in the console
  222. * @param aTableName
  223. */
  224. private void printTable(String aTableName)
  225. {
  226. try
  227. {
  228. Statement lStatement = fConnection.createStatement();
  229. String statement = "SELECT * FROM " + aTableName + ";";
  230. ResultSet result = lStatement.executeQuery(statement);
  231. ResultSetMetaData resultMeta = result.getMetaData();
  232.  
  233. int columnCount = resultMeta.getColumnCount();
  234.  
  235. //Print column names
  236. for(int i = 1; i <columnCount; i++)
  237. {
  238. if (i > 1)
  239. System.out.print(", ");
  240. String columnName = resultMeta.getColumnName(i);
  241. System.out.print(columnName);
  242. }
  243.  
  244. System.out.println();
  245.  
  246. //Print row data
  247. while(result.next())
  248. {
  249. for (int i = 1; i <= columnCount; i++) {
  250. if (i > 1) System.out.print(", ");
  251. String columnValue = result.getString(i);
  252. System.out.print(columnValue);
  253. }
  254. System.out.println();
  255. }
  256.  
  257. } catch(SQLException e)
  258. {
  259. System.out.println("Unable to print table " + aTableName);
  260. }
  261. }
  262.  
  263. /**
  264. * Changes the password of the passed in username
  265. * @param aUser Username to change password of
  266. * @param aPass Password to change to
  267. */
  268. private void changePassword(String aUser, String aPass)
  269. {
  270. try
  271. {
  272. Statement lStatement = fConnection.createStatement();
  273. String statement = "UPDATE user SET Password= \"" + aPass
  274. + "\" WHERE username = \""
  275. + aUser + "\";";
  276. lStatement.executeUpdate(statement);
  277. } catch (SQLException e)
  278. {
  279. System.out.println("Unable to change password");
  280. }
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement