Advertisement
Guest User

Untitled

a guest
May 30th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7. import java.util.Vector;
  8.  
  9. public class Database_class {
  10.  
  11. String driver;
  12. Connection conn;
  13. private Vector dataVector;
  14.  
  15. /*--------------- SQL-METHODS FOR DB-CONNECTION -----------*/
  16.  
  17. public Database_class() throws SQLException {
  18. driver = "org.apache.derby.jdbc.EmbeddedDriver";
  19. conn = DriverManager.getConnection("jdbc:derby:testdb;create=true");
  20. }
  21.  
  22. void connect2db() throws SQLException {
  23. try {
  24. Class.forName(driver); // load Driver
  25. System.out.println(driver + " loaded. ");
  26. } catch (java.lang.ClassNotFoundException e) {
  27. System.err.print("ClassNotFoundException: ");
  28. System.err.println(e.getMessage());
  29. }
  30. }
  31.  
  32. void closedb() throws SQLException {
  33. conn.close();
  34. if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
  35. boolean gotSQLExc = false;
  36. try {
  37. DriverManager.getConnection("jdbc:derby:;shutdown=true");
  38. } catch (SQLException se) {
  39. if (se.getSQLState().equals("XJ015")) {
  40. gotSQLExc = true;
  41. }
  42. }
  43. if (!gotSQLExc) {
  44. System.out.println("Database did not shut down normally");
  45. } else {
  46. System.out.println("Database shut down normally");
  47. }
  48. }
  49. }
  50.  
  51. /*--------------- SQL-METHODS FOR VOCAB -------------------*/
  52.  
  53. void queryVocab_debug() throws SQLException {
  54. System.out.println("Debug message: getVocab_debug()");
  55. Statement s = conn.createStatement();
  56. ResultSet result = s.executeQuery("select * from Vocable");
  57.  
  58. System.out.println("ID:\tEnglisch:\t\t\tDeutsch:\t\t\tKategorie:");
  59. System.out
  60. .println("-------------------------------------------------------------\n");
  61. while (result.next()) { // Prints all results
  62. System.out.println(result.getString(1) + "\t" + result.getString(2)
  63. + "\t" + result.getString(3) + "\t" + result.getString(4));
  64. }
  65. System.out.println("\n");
  66. }
  67.  
  68. ResultSet queryVocab() throws SQLException {
  69. System.out.println("Debug message: queryVocab()");
  70. Statement s = conn.createStatement();
  71. ResultSet result = s
  72. .executeQuery("select Vocable.VID, Vocable.Word, Vocable.Transl, Category.Name from Vocable inner join Category on Vocable.Cat = Category.CID");
  73. return result;
  74.  
  75. }
  76.  
  77. void insertVocab(String value, String transl, int cat) throws SQLException {
  78. System.out.println("Debug message: insertVocab('" + value + "', '"
  79. + transl + "', '" + cat + "')");
  80. Statement s = conn.createStatement();
  81. s.execute("insert into Vocable(Word, Transl, Cat) values " + "('"
  82. + value + "','" + transl + "'," + cat + ")");
  83. }
  84.  
  85. void deleteVocab(int value) throws SQLException {
  86. System.out.println("Debug message: deleteVocab(" + value + ")");
  87. Statement s = conn.createStatement();
  88. s.execute("Delete from Vocable where VID = " + value);
  89. System.out.println(value + " deleted from table Vocable");
  90. }
  91.  
  92. void deleteVocab_Word(String value) throws SQLException {
  93. System.out.println("Debug message: deleteVocab_Word(" + value + ")");
  94. Statement s = conn.createStatement();
  95. s.execute("Delete from Vocable where Word = '" + value + "'");
  96. System.out.println(value + " deleted from table Vocable");
  97. }
  98.  
  99. int getVocab_ID(String value, boolean choice) throws SQLException {
  100. System.out.println("Debug message: getVocab_ID(" + value + ", "
  101. + choice + ")");
  102. int vID = 0;
  103. Statement s = conn.createStatement();
  104.  
  105. if (choice)
  106. s.execute("select VID from Vocable where Transl = '" + value + "'");
  107. else
  108. s.execute("select VID from Vocable where Word = '" + value + "'");
  109.  
  110. return vID;
  111. }
  112.  
  113. /*--------------- SQL-METHODS FOR TRAINING ----------------*/
  114.  
  115. String getRandomVocab() throws SQLException {
  116. String returnval = "";
  117.  
  118. System.out.println("Debug message: getRandomVocab()");
  119. Statement s = conn.createStatement();
  120.  
  121. ResultSet result = s
  122. .executeQuery("Select Word from Vocable order by RANDOM()");
  123. result.next();
  124. returnval = result.getString(1);
  125. System.out.println(returnval);
  126. return returnval;
  127. }
  128.  
  129. String checkAnswer(String value) throws SQLException {
  130. String returnvalue = "";
  131. System.out.print("Debug message: checkAnswer(): ");
  132. Statement s = conn.createStatement();
  133. ResultSet result = s.executeQuery("Select Word from Vocable where Transl = '"+value+"'");
  134.  
  135. if(result.next()){
  136. returnvalue = result.getString(1);
  137. return returnvalue;
  138. } else {
  139. return returnvalue;
  140. }
  141. }
  142.  
  143. /*--------------- SQL-METHODS FOR CATEGORY ----------------*/
  144.  
  145. ResultSet queryCategory() throws SQLException {
  146. System.out.println("Debug message: queryCategory()");
  147. Statement s = conn.createStatement();
  148. ResultSet result = s.executeQuery("select * from Category");
  149. return result;
  150. }
  151.  
  152. void insertCategory(String value) throws SQLException {
  153. System.out.println("Debug message: insertCategory(" + value + ")");
  154. Statement s = conn.createStatement();
  155. s.execute("insert into Category(Name) values " + "('" + value + "')");
  156. }
  157.  
  158. void deleteCategory(int value) throws SQLException {
  159. if (alterCategoryifNotEmpty(value)) {
  160. System.out.println("Debug message: deleteCategory(" + value + ")");
  161. Statement s = conn.createStatement();
  162. s.execute("Delete from Category where CID = " + value);
  163. }
  164. }
  165.  
  166. int getCategoryID(String value) throws SQLException {
  167. int returnval = 0;
  168. Statement s = conn.createStatement();
  169. ResultSet result = s
  170. .executeQuery("select CID from Category where Name = '" + value
  171. + "'");
  172. while (result.next()) {
  173.  
  174. returnval = result.getInt(1);
  175. }
  176. return returnval;
  177. }
  178.  
  179. String getCategoryName(int id) throws SQLException {
  180. System.out.println("Debug message: getCategoryName(" + id + ")");
  181. String value = "";
  182. Statement s = conn.createStatement();
  183. ResultSet result = s
  184. .executeQuery("Select Name from Category where CID = " + id);
  185. while (result.next())
  186. value = result.getString(1);
  187. return value;
  188. }
  189.  
  190. /**
  191. * Alter Category of Vocabs in Preparation to delete a category from the
  192. * table.
  193. *
  194. * @param id
  195. * @throws SQLException
  196. */
  197. boolean alterCategoryifNotEmpty(int id) throws SQLException {
  198. System.out.println("Debug message: checkifCategoryEmpty()");
  199. Statement s = conn.createStatement();
  200. Statement s1 = conn.createStatement();
  201. if (id != 1) {
  202. ResultSet result = s
  203. .executeQuery("Select VID, Cat from Vocable where Cat = "
  204. + id);
  205.  
  206. while (result.next()) {
  207. s1.executeUpdate("update Vocable set Cat = 1 where Cat = "
  208. + result.getString(2));
  209. }
  210. return true;
  211.  
  212. } else {
  213. System.out
  214. .println("Debug message: Nice try! I ain't gonna delete myself!");
  215. return false;
  216. }
  217. }
  218.  
  219. int checkCategory(String value) throws SQLException {
  220. int returnval = 0;
  221. Statement s = conn.createStatement();
  222. ResultSet result = s
  223. .executeQuery("select CID from Category where Name = '" + value
  224. + "'");
  225. while (result.next()) {
  226.  
  227. returnval = result.getInt(1);
  228. }
  229. return returnval;
  230. }
  231.  
  232. /*--------------- SQL-METHODS FOR DBUSER ------------------*/
  233.  
  234. /**
  235. * Debug-method that returns a full list of all Users to the console.
  236. *
  237. * @throws SQLEception
  238. */
  239. void queryUser_debug() throws SQLException {
  240. System.out.println("Debug message: queryUser_debug()");
  241. Statement s = conn.createStatement();
  242. ResultSet result = s.executeQuery("select * from DBUser");
  243.  
  244. System.out.println("UserID:\tName:");
  245. System.out
  246. .println("-------------------------------------------------------------\n");
  247. while (result.next()) { // prints all results
  248. System.out
  249. .println(result.getString(1) + "\t" + result.getString(2));
  250. }
  251. System.out.println("\n");
  252. }
  253.  
  254. /**
  255. * Queries the Userdatabase and returns a full list of all Users.
  256. *
  257. * @return list of all DBUsers
  258. * @throws SQLException
  259. */
  260. ResultSet queryUser() throws SQLException {
  261. System.out.println("Debug message: queryUser()");
  262. Statement s = conn.createStatement();
  263. ResultSet result = s.executeQuery("select * from DBUser");
  264. return result;
  265. }
  266.  
  267. /**
  268. * Inserts a User {@value} into the database.
  269. *
  270. * @param value
  271. * @throws SQLException
  272. */
  273. void insertUser(String value) throws SQLException {
  274. System.out.println("Debug message: insertUser(" + value + ")");
  275. Statement s = conn.createStatement();
  276. s.execute("insert into DBUser(Name) values " + "('" + value + "')");
  277. }
  278.  
  279. /**
  280. * Deletes a User {@value} from the database.
  281. *
  282. * @param value
  283. * @throws SQLException
  284. */
  285. void deleteUser(int value) throws SQLException {
  286. Statement s = conn.createStatement();
  287. s.execute("Delete from DBUser where UID = " + value);
  288. System.out.println("Debug message: deleteUser(" + value + ")");
  289. }
  290.  
  291. /* -------------- END OF SQL-METHODS ---------------------- */
  292.  
  293. ArrayList<String> getCategory() throws SQLException {
  294. ArrayList<String> result_list = new ArrayList(1);
  295. ArrayList<Integer> result_list_nr = new ArrayList(1);
  296. Statement s = conn.createStatement();
  297. ResultSet result = s.executeQuery("select Name from Category");
  298.  
  299. while (result.next()) {
  300. result_list.add(result.getString("Name"));
  301. }
  302. return result_list;
  303. }
  304.  
  305. /* begin for User-SQL-Statements */
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement