Advertisement
Guest User

Untitled

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