Advertisement
Guest User

Untitled

a guest
May 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 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(String value, boolean choice) throws SQLException {
  86. System.out.println("Debug message: deleteVocab(" + value + ", "
  87. + choice + ")");
  88. Statement s = conn.createStatement();
  89. int vID = getVocab_ID(value, choice);
  90. s.execute("Delete from Vocable where VID = " + vID);
  91. System.out.println(vID + " deleted from table Vocable");
  92. }
  93.  
  94. void deleteVocab_Word(String value) throws SQLException {
  95. System.out.println("Debug message: deleteVocab_Word(" + value + ")");
  96. Statement s = conn.createStatement();
  97. s.execute("Delete from Vocable where Word = '" + value + "'");
  98. System.out.println(value + " deleted from table Vocable");
  99. }
  100.  
  101. int getVocab_ID(String value, boolean choice) throws SQLException {
  102. System.out.println("Debug message: getVocab_ID(" + value + ", "
  103. + choice + ")");
  104. int vID = 0;
  105. Statement s = conn.createStatement();
  106.  
  107. if (choice)
  108. s.execute("select VID from Vocable where Transl = '" + value + "'");
  109. else
  110. s.execute("select VID from Vocable where Word = '" + value + "'");
  111.  
  112. return vID;
  113. }
  114.  
  115. /*--------------- SQL-METHODS FOR CATEGORY ----------------*/
  116.  
  117. ResultSet queryCategory() throws SQLException {
  118. System.out.println("Debug message: queryCategory()");
  119. Statement s = conn.createStatement();
  120. ResultSet result = s.executeQuery("select * from Category");
  121. return result;
  122. }
  123.  
  124. void insertCategory(String value) throws SQLException {
  125. System.out.println("Debug message: insertCategory(" + value + ")");
  126. Statement s = conn.createStatement();
  127. s.execute("insert into Category(Name) values " + "('" + value + "')");
  128. }
  129.  
  130. void deleteCategory(int value) throws SQLException {
  131. if(alterCategoryifNotEmpty(value)){
  132. System.out.println("Debug message: deleteCategory(" + value + ")");
  133. Statement s = conn.createStatement();
  134. s.execute("Delete from Category where CID = " + value);
  135. }
  136. }
  137.  
  138. int getCategoryID(String value) throws SQLException {
  139. int returnval = 0;
  140. Statement s = conn.createStatement();
  141. ResultSet result = s
  142. .executeQuery("select CID from Category where Name = '" + value
  143. + "'");
  144. while (result.next()) {
  145.  
  146. returnval = result.getInt(1);
  147. }
  148. return returnval;
  149. }
  150.  
  151. String getCategoryName(int id) throws SQLException {
  152. System.out.println("Debug message: getCategoryName(" + id + ")");
  153. String value = "";
  154. Statement s = conn.createStatement();
  155. ResultSet result = s
  156. .executeQuery("Select Name from Category where CID = " + id);
  157. while (result.next())
  158. value = result.getString(1);
  159. return value;
  160. }
  161.  
  162. /**
  163. * Alter Category of Vocabs in Preparation to delete a category from the
  164. * table.
  165. *
  166. * @param id
  167. * @throws SQLException
  168. */
  169. boolean alterCategoryifNotEmpty(int id) throws SQLException {
  170. System.out.println("Debug message: checkifCategoryEmpty()");
  171. Statement s = conn.createStatement();
  172. Statement s1 = conn.createStatement();
  173. if (id != 1) {
  174. ResultSet result = s
  175. .executeQuery("Select VID, Cat from Vocable where Cat = "
  176. + id);
  177.  
  178. while (result.next()) {
  179. s1.executeUpdate("update Vocable set Cat = 1 where Cat = "
  180. + result.getString(2));
  181. }
  182. return true;
  183.  
  184. } else {
  185. System.out.println("Debug message: Go fuck yourself! I ain't gonna delete myself!");
  186. return false;
  187. }
  188. }
  189.  
  190. /*--------------- SQL-METHODS FOR DBUSER ------------------*/
  191.  
  192. /**
  193. * Debug-method that returns a full list of all Users to the console.
  194. *
  195. * @throws SQLEception
  196. */
  197. void queryUser_debug() throws SQLException {
  198. System.out.println("Debug message: queryUser_debug()");
  199. Statement s = conn.createStatement();
  200. ResultSet result = s.executeQuery("select * from DBUser");
  201.  
  202. System.out.println("UserID:\tName:");
  203. System.out
  204. .println("-------------------------------------------------------------\n");
  205. while (result.next()) { // prints all results
  206. System.out
  207. .println(result.getString(1) + "\t" + result.getString(2));
  208. }
  209. System.out.println("\n");
  210. }
  211.  
  212. /**
  213. * Queries the Userdatabase and returns a full list of all Users.
  214. *
  215. * @return list of all DBUsers
  216. * @throws SQLException
  217. */
  218. ResultSet queryUser() throws SQLException {
  219. System.out.println("Debug message: queryUser()");
  220. Statement s = conn.createStatement();
  221. ResultSet result = s.executeQuery("select * from DBUser");
  222. return result;
  223. }
  224.  
  225. /**
  226. * Inserts a User {@value} into the database.
  227. *
  228. * @param value
  229. * @throws SQLException
  230. */
  231. void insertUser(String value) throws SQLException {
  232. System.out.println("Debug message: insertUser(" + value + ")");
  233. Statement s = conn.createStatement();
  234. s.execute("insert into DBUser(Name) values " + "('" + value + "')");
  235. }
  236.  
  237. /**
  238. * Deletes a User {@value} from the database.
  239. *
  240. * @param value
  241. * @throws SQLException
  242. */
  243. void deleteUser(String value) throws SQLException {
  244. System.out.println("Debug message: deleteUser(" + value + ")");
  245. Statement s = conn.createStatement();
  246. s.execute("Delete from DBUser where Name = '" + value + "'");
  247. }
  248.  
  249. /* -------------- END OF SQL-METHODS ---------------------- */
  250.  
  251. ArrayList<String> getCategory() throws SQLException {
  252. ArrayList<String> result_list = new ArrayList(1);
  253. ArrayList<Integer> result_list_nr = new ArrayList(1);
  254. Statement s = conn.createStatement();
  255. ResultSet result = s.executeQuery("select Name from Category");
  256.  
  257. while (result.next()) {
  258. result_list.add(result.getString("Name"));
  259. }
  260. return result_list;
  261. }
  262.  
  263. /* begin for User-SQL-Statements */
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement