Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. import domain.*;
  11.  
  12. public class PgDatabase extends Database {
  13.  
  14. private Connection db;
  15. private Statement sql;
  16. private String username;
  17. private String password;
  18. private String uri;
  19. private String location = "machkau.auteur";
  20. private Database database;
  21.  
  22. public PgDatabase() throws DataBaseException{
  23. setUsername("518561");
  24. System.out.println(getUsername());
  25. setPassword("DitIsMijnPasswoordniet");
  26. setUri("jdbc:postgresql://" + database);
  27.  
  28. init();
  29. }
  30.  
  31. public void init() throws DataBaseException {
  32.  
  33.  
  34.  
  35.  
  36.  
  37. try {
  38. db = DriverManager.getConnection(getUri(), getUsername(),
  39. getPassword());
  40. setSql(db.createStatement());
  41. } catch (SQLException e) {
  42. System.err.println(e.getMessage());
  43. throw new DataBaseException(e);
  44.  
  45. }
  46.  
  47. }
  48.  
  49. private String getLocation() {
  50. return this.location;
  51. }
  52.  
  53. private String getUsername() {
  54. return this.username;
  55. }
  56.  
  57. private String getPassword() {
  58. return this.password;
  59. }
  60.  
  61. private String getUri() {
  62. return this.uri;
  63. }
  64.  
  65. private void setUri(String uri) throws DataBaseException {
  66. if (this.uri == null || uri.equals("")) {
  67. throw new DataBaseException("uri is null or empty");
  68. } else {
  69. this.uri = uri;
  70. }
  71. }
  72.  
  73. private void setPassword(String password) throws DataBaseException {
  74. if (this.password == null || password.equals("")) {
  75. throw new DataBaseException(" password is null or empty");
  76. } else {
  77.  
  78. this.password = password;
  79. }
  80. }
  81.  
  82. public void setUsername(String username) throws DataBaseException {
  83. if (username == null || username.equals("")) {
  84. throw new DataBaseException("username is null or empty");
  85. } else {
  86. this.username = username;
  87. }
  88. }
  89.  
  90. public void setSql(Statement sql) throws DataBaseException {
  91. if (sql == null || sql.equals("")) {
  92. throw new DataBaseException("statement is null or empty");
  93. } else {
  94. this.sql = sql;
  95. }
  96. }
  97.  
  98. public Statement getSql() {
  99. return sql;
  100. }
  101.  
  102. @Override
  103. public boolean createAuteur(Auteur auteur) throws DataBaseException {
  104. boolean bool = false;
  105. if (auteur == null || auteur.equals("")) {
  106. throw new DataBaseException("auteur is null or empty");
  107. } else {
  108. if (getAuteur(auteur.getId()) != null) {// als id al bestaat
  109. throw new DataBaseException("auteur met dit id bestaat al");
  110. } else {
  111. String query = "INSERT INTO " + getLocation()
  112. + "(auteursnr,voornaam,naam) " + "values('" + auteur.getId()
  113. + "','" + auteur.getName() + "','"
  114. + auteur.getLastname() + "');";
  115. try {
  116. int res = getSql().executeUpdate(query);
  117. if (res > 0) {
  118. bool = true;
  119. }
  120. } catch (SQLException e) {
  121. System.err.println(e.getMessage());
  122. throw new DataBaseException(e);
  123. }
  124. }
  125. }
  126. // TODO Auto-generated method stub
  127. return bool;
  128. }
  129.  
  130. @Override
  131. public boolean deleteAuter(int id) throws DataBaseException {
  132. boolean succes = false;
  133. if (id < 0) {
  134. throw new DataBaseException("id is not valid");
  135. } else {
  136. if (getAuteur(id) != null) {// eerst kijken of hij bestaat
  137. String qry = ("DELETE FROM " + getLocation()
  138. + " WHERE auteursnr = " + id + ";");
  139. try {
  140. int res = sql.executeUpdate(qry);
  141. if (res > 0) {
  142. succes = true;
  143. }
  144. } catch (SQLException e) {
  145. throw new DataBaseException(e);
  146. }
  147. }
  148. }
  149. return succes;
  150. }
  151.  
  152. public boolean deleteAuter(Auteur auteur) throws DataBaseException {
  153. boolean bool = false;
  154. if (auteur == null || auteur.equals("")) {
  155. throw new DataBaseException("auteur is null or empty");
  156. } else {
  157. if (getAuteur(auteur.getId()) != null) {
  158. String qry = ("DELETE FROM " + getLocation()
  159. + " WHERE voornaam LIKE '" + auteur.getName()
  160. + "' AND naam LIKE '" + auteur.getLastname() + "';");
  161.  
  162. try {
  163. int res = sql.executeUpdate(qry);
  164. if (res > 0) {
  165. bool = true;
  166. }
  167. } catch (SQLException e) {
  168. throw new DataBaseException(e);
  169. }
  170. }
  171. }
  172. return bool;
  173. }
  174.  
  175. @Override
  176. public ArrayList<Auteur> giveList() throws DataBaseException {
  177. ArrayList<Auteur> list = null;
  178. list = database.giveList();
  179. return list;
  180. // TODO checken
  181.  
  182. }
  183.  
  184. @Override
  185. public boolean updateAuteur(Auteur auteur) throws DataBaseException {
  186. boolean succes = false;
  187. boolean bool = false;
  188. if (auteur == null || auteur.equals("")) {
  189. throw new DataBaseException("auteur is null or empty");
  190. } else {
  191.  
  192. /*
  193. * TODO keuze tussen eerst auteur deleten en dan een nieuwe creeren
  194. * of via update in query String qry = "UPDATE " + getLocation() +
  195. * " " + "voornaam = '"+ auteur.getName() + "' " + "naam = '" +
  196. * auteur.getLastname() + "' ;"; try{ succes=sql.execute(qry);
  197. * }catch (SQLException e){ throw new DataBaseException(e); }
  198. */
  199. deleteAuter(auteur);
  200. bool = true;
  201. }
  202. if (bool) {
  203. createAuteur(auteur);
  204. succes = true;
  205. } else {
  206. succes = false;
  207. }
  208. return succes;
  209. }
  210.  
  211. @Override
  212. public Auteur getAuteur(int id) throws DataBaseException {
  213. boolean succes = false;
  214. Auteur auteur = null;
  215. ResultSet nm;
  216. ResultSet an;
  217. if (id < 0) {
  218. throw new DataBaseException("id is not valid");
  219. } else {
  220. String naam = "SELECT voornaam FROM " + getLocation() + " "
  221. + "WHERE auteursnr = " + id + " ;";
  222. String lastname = "SELECT naam FROM " + getLocation() + " "
  223. + "WHERE auteursnr = " + id + " ;";
  224. try {
  225. // succes = sql.execute(naam);
  226. // succesLastname = sql.execute(lastname);
  227. System.out.println(naam);
  228. nm = sql.executeQuery(naam);
  229. an = sql.executeQuery(lastname);
  230. succes = true;
  231. } catch (SQLException e) {
  232. throw new DataBaseException(e);
  233. }
  234. }
  235. if (succes) {
  236. try {
  237. auteur = new Auteur(id, nm.getString("voornaam"),
  238. an.getString("naam"));
  239. } catch (SQLException e) {
  240. // TODO Auto-generated catch block
  241. throw new DataBaseException(e);
  242. }
  243. } else {
  244. throw new DataBaseException("did not found auteur with given id");
  245. }
  246. return auteur;
  247. }
  248.  
  249. @Override
  250. public void save() throws DataBaseException {
  251. // database.save();
  252.  
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement