Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.04 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package sonderaufgabe;
  7.  
  8. import java.sql.*;
  9. import java.util.*;
  10.  
  11. /**class MainWindow
  12. * @author usher
  13. */
  14. public class MappingLayer
  15. {
  16.  
  17. /*Singleton Instanz vom Mapping Layer*/
  18. private static MappingLayer fmlInstance = null;
  19. /*Connection zur Datenbank*/
  20. private static Connection con;
  21. /*Datenbank URL*/
  22. private static final String databaseURL = "jdbc:firebirdsql:127.0.0.1:E:\\studium\\sem3\\datenbank"
  23. +"\\javasonderaufgabe\\SONDERAUFGABE.FDB";
  24. /*Datenbank Username*/
  25. private static final String userName = "SYSDBA";
  26. /*Datenbank Passwort*/
  27. private static final String password = "masterkey";
  28. /*Datenbanktreiber*/
  29. private static final String driverName = "org.firebirdsql.jdbc.FBDriver";
  30.  
  31. /*constant states for external using*/
  32. public static final int SEARCH = 0;
  33. public static final int INSERT = 1;
  34. public static final int UPDATE = 2;
  35.  
  36. /**Konstruktor zur Datenbank Connection
  37. *
  38. */
  39. private MappingLayer()
  40. {
  41. try
  42. {
  43. Class.forName (driverName);
  44. con = DriverManager.getConnection(databaseURL, userName, password);
  45.  
  46. System.out.println ("Database connected.");
  47. }
  48. catch (ClassNotFoundException e)
  49. {
  50. System.out.println ("Firebird driver not found");
  51. System.out.println (e.getMessage ());
  52. }
  53. catch (SQLException e)
  54. {
  55. e.printStackTrace();
  56. System.out.println ("Unable to establish a connection.");
  57. showSQLException (e);
  58. }
  59. }
  60.  
  61. /**Finalize zum schließen der Connection zur DB
  62. *
  63. */
  64. @Override
  65. protected void finalize()
  66. {
  67. try
  68. {
  69. con.close();
  70.  
  71. System.out.println ("Database disconnected.");
  72. }
  73. catch (SQLException e)
  74. {
  75. e.printStackTrace();
  76. System.out.println ("Unable to disconnect a connection.");
  77. showSQLException (e);
  78. }
  79. }
  80.  
  81. /*Singleton Methode für getInstanc
  82. *
  83. */
  84. public static MappingLayer getInstance()
  85. {
  86. if(fmlInstance == null)
  87. fmlInstance = new MappingLayer();
  88.  
  89. return fmlInstance;
  90. }
  91.  
  92. /**close() Löschen der Singleton Methode zum schließen der Datenbank
  93. *
  94. */
  95. public static void close()
  96. {
  97. fmlInstance = null;
  98. System.gc();
  99. }
  100.  
  101. /**showSQLException - shows detailed SQL error message
  102. *
  103. * @param e
  104. */
  105. private static void showSQLException (SQLException e)
  106. {
  107. SQLException message = e;
  108. while (message != null)
  109. {
  110. System.out.println (message.getMessage ());
  111. System.out.println ("Error Code: " + message.getErrorCode ());
  112. System.out.println ("SQL State: " + message.getSQLState ());
  113. message = message.getNextException ();
  114. }
  115. }
  116.  
  117. /**Liste der Artikel in der DB erhalten
  118. *
  119. * @return list of clients
  120. */
  121. List<Article> getAllArticles()
  122. {
  123. List<Article> Article_List = new ArrayList<Article>();
  124. Statement statement = null;
  125. ResultSet rs = null;
  126. Article newArticle = null;
  127. String selectString = "SELECT ARTIKELID, ARTIKELNR, BEZEICHNUNG, ARTIKELGRUPPEID, " +
  128. "LIEFERANTID, BESTANDSMENGE, PREIS " +
  129. "FROM ARTIKEL ORDER BY ARTIKELID";
  130. try
  131. {
  132. statement = con.createStatement();
  133. rs = statement.executeQuery(selectString);
  134.  
  135. while (rs.next())
  136. {
  137. /*SELECT ARTIKELID, ARTIKELNR, BEZEICHNUNG, ARTIKELGRUPPEID,LIEFERANTID, BESTANDSMENGE, PREIS*/
  138. newArticle = new Article(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4),rs.getInt(5), rs.getInt(6), rs.getFloat(7));
  139.  
  140. Article_List.add(newArticle);
  141. }
  142. rs.close();
  143. statement.close();
  144. }
  145. catch (SQLException e)
  146. {
  147. e.printStackTrace();
  148. System.out.println ("Failure with SQL-Statement.");
  149. showSQLException (e);
  150. }
  151.  
  152. return Article_List;
  153. }
  154.  
  155. /**searchArticles
  156. *
  157. * @param searchArticle
  158. * @return list of articles
  159. */
  160. public List<Article> searchArticles(Article searchArticle)
  161. {
  162. List<Article> Article_List = new ArrayList<Article>();
  163. Article newArticle = null;
  164. boolean iNotFirst = false;
  165. PreparedStatement ps = null;
  166. ResultSet rs = null;
  167. String selectString = "SELECT ARTIKELID, ARTIKELNR, BEZEICHNUNG, ARTIKELGRUPPEID, " +
  168. "LIEFERANTID, BESTANDSMENGE, PREIS " +
  169. "FROM ARTIKEL";
  170. String conditionString = null;
  171.  
  172. if(searchArticle != null)
  173. {
  174. conditionString = " WHERE ";
  175.  
  176. if(searchArticle.getArticleID() != -1)
  177. {
  178. conditionString += "ARTIKELID LIKE( '%" + searchArticle.getArticleID() + "%')";
  179. iNotFirst = true;
  180. }
  181.  
  182. if(searchArticle.getArtikelNr() != null)
  183. {
  184. if(iNotFirst)
  185. conditionString += " AND ";
  186.  
  187. conditionString += "ARTIKELNR LIKE ( '%" +searchArticle.getArtikelNr() + "%')";
  188. iNotFirst=true;
  189. }
  190.  
  191. if(searchArticle.getBezeichnung() != null)
  192. {
  193. if(iNotFirst)
  194. conditionString += " AND ";
  195.  
  196. conditionString += "BEZEICHNUNG LIKE( '%" + searchArticle.getBezeichnung()+ "%')";
  197. iNotFirst=true;
  198. }
  199.  
  200. if(searchArticle.getArtikelgruppeID() != -1)
  201. {
  202. if(iNotFirst)
  203. conditionString += " AND ";
  204.  
  205. conditionString += "ARTIKELGRUPPEID LIKE( '%" + searchArticle.getArtikelgruppeID() + "%')";
  206. iNotFirst=true;
  207. }
  208. if(searchArticle.getLieferantID() != -1)
  209. {
  210. if(iNotFirst)
  211. conditionString += " AND ";
  212.  
  213. conditionString += "LIEFERANTID = '" + searchArticle.getLieferantID() + "'";
  214. iNotFirst=true;
  215. }
  216.  
  217. if(searchArticle.getBestandsmenge() != -1)
  218. {
  219. if(iNotFirst)
  220. conditionString += " AND ";
  221.  
  222. conditionString += " BESTANDSMENGE LIKE( '%" + searchArticle.getBestandsmenge()+ "%')";
  223. iNotFirst=true;
  224. }
  225.  
  226. if(searchArticle.getPreis() != -1)
  227. {
  228. if(iNotFirst)
  229. conditionString += " AND ";
  230.  
  231. conditionString += "PREIS LIKE( '%" + searchArticle.getPreis() + "%')";
  232. iNotFirst=true;
  233. }
  234.  
  235.  
  236.  
  237. if(conditionString.equals(" WHERE "))
  238. conditionString = null;
  239. }
  240. try
  241. {
  242. if(conditionString != null)
  243. selectString += conditionString;
  244.  
  245. ps = con.prepareStatement(selectString);
  246. rs = ps.executeQuery(selectString);
  247.  
  248. while (rs.next())
  249. {
  250. /*get ARTIKELID, ArtikelNr, Bezeichnung, ArtikelgruppeID, LieferantID, Bestandsmenge, Preis*/
  251. newArticle = new Article(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4),
  252. rs.getInt(5), rs.getInt(6), rs.getFloat(7));
  253.  
  254. Article_List.add(newArticle);
  255. }
  256. rs.close();
  257. ps.close();
  258. }
  259. catch (SQLException e)
  260. {
  261. e.printStackTrace();
  262. System.out.println ("Failure with SQL-Statement.");
  263. showSQLException (e);
  264. }
  265.  
  266. return Article_List;
  267. }
  268.  
  269.  
  270.  
  271. /**insertArticle
  272. *
  273. * @param articleToInsert
  274. * @return -1 = error, 0 = not inserted, 1 = inserted
  275. */
  276. public int insertArticle(Article articleToInsert)
  277. {
  278. int ret = 1;
  279. try
  280. {
  281. PreparedStatement ps = con.prepareStatement("INSERT INTO ARTIKEL (ARTIKELID, ARTIKELNR, BEZEICHNUNG, ARTIKELGRUPPEID, LIEFERANTID, BESTANDSMENGE, PREIS) " +
  282. "VALUES(?, ?, ?, ?, ?, ?, ?)");
  283.  
  284. ps.setInt(1, articleToInsert.getArticleID());
  285. ps.setString(2, articleToInsert.getArtikelNr());
  286. ps.setString(3, articleToInsert.getBezeichnung());
  287. ps.setInt(4, articleToInsert.getArtikelgruppeID());
  288. ps.setInt(5, articleToInsert.getLieferantID());
  289. ps.setInt(6, articleToInsert.getBestandsmenge());
  290. ps.setFloat(7, articleToInsert.getPreis());
  291.  
  292. ret = ps.executeUpdate();
  293.  
  294. ps.close();
  295.  
  296. }
  297. catch (SQLException e)
  298. {
  299. e.printStackTrace();
  300. System.out.println ("Failure with SQL-Statement.");
  301. showSQLException (e);
  302. return -1;
  303. }
  304. return ret;
  305. }
  306.  
  307.  
  308. /**updateArticle
  309. *
  310. * @param articleToUpdate
  311. * @return -1 = error, 0 = not inserted, 1 = inserted
  312. */
  313. public int updateArticle(Article articleToUpdate)
  314. {
  315. int ret = 1;
  316. try
  317. {
  318. PreparedStatement ps = con.prepareStatement("UPDATE ARTIKEL " +
  319. "SET ARTIKELNR = ?, BEZEICHNUNG = ?, ARTIKELGRUPPEID = ?, LIEFERANTID = ?, " +
  320. "BESTANDSMENGE = ?, PREIS = ? " +
  321. "WHERE ARTIKELID = ?");
  322.  
  323.  
  324. ps.setString(1, articleToUpdate.getArtikelNr());
  325. ps.setString(2, articleToUpdate.getBezeichnung());
  326. ps.setInt(3, articleToUpdate.getArtikelgruppeID());
  327. ps.setInt(4, articleToUpdate.getLieferantID());
  328. ps.setInt(5, articleToUpdate.getBestandsmenge());
  329. ps.setFloat(6, articleToUpdate.getPreis());
  330. ps.setInt(7, articleToUpdate.getArticleID());
  331.  
  332. ret = ps.executeUpdate();
  333.  
  334. ps.close();
  335.  
  336. }
  337. catch (SQLException e)
  338. {
  339. e.printStackTrace();
  340. System.out.println ("Failure with SQL-Statement.");
  341. showSQLException (e);
  342. return -1;
  343. }
  344. return ret;
  345. }
  346.  
  347. /**deleteArticle
  348. *
  349. * @param articleID
  350. * @return -1 = error, 0 = not inserted, 1 = inserted
  351. */
  352. public int deleteArticle(int articleID)
  353. {
  354. int ret = 1;
  355. try
  356. {
  357. PreparedStatement ps = con.prepareStatement("DELETE FROM ARTIKEL " +
  358. "WHERE ARTIKELID = ?");
  359.  
  360. ps.setInt(1, articleID);
  361.  
  362. ret = ps.executeUpdate();
  363.  
  364. ps.close();
  365.  
  366. }
  367. catch (SQLException e)
  368. {
  369. e.printStackTrace();
  370. System.out.println ("Failure with SQL-Statement.");
  371. showSQLException (e);
  372. return 0;
  373. }
  374. return ret;
  375. }
  376.  
  377. /**getAllOrders
  378. *
  379. * @return list of orders
  380. */
  381. List<Order> getAllOrders()
  382. {
  383. List<Order> orderList = new ArrayList<Order>();
  384. Statement statement = null;
  385. ResultSet rs = null;
  386. Order newOrder = null;
  387.  
  388. String selectString = "SELECT BESTELLID,KUNDEID,ARTIKELIDENT,MENGE,BESTELLDATUM,NAME,VORNAME,BEZEICHNUNG FROM BESTELLUNGEN,ARTIKEL,PERSON WHERE KUNDEID = PERSID AND ARTIKELIDENT = ARTIKELID ORDER BY BESTELLID";
  389. try
  390. {
  391. statement = con.createStatement();
  392. rs = statement.executeQuery(selectString);
  393.  
  394. while (rs.next())
  395. {
  396. String name = rs.getString(6) + " " + rs.getString(7);
  397. String bezeichnung = rs.getString(8);
  398. /*get bestellid, kundeid, artikelid, menge, bestalldatum, kundennr, name, vorname,strasse,nr,plz,ort,land*/
  399. newOrder = new Order(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getTimestamp(5),name,bezeichnung);
  400.  
  401. orderList.add(newOrder);
  402. }
  403. rs.close();
  404. statement.close();
  405. }
  406. catch (SQLException e)
  407. {
  408. e.printStackTrace();
  409. System.out.println ("Unable to disconnect a connection.");
  410. showSQLException (e);
  411. }
  412.  
  413. return orderList;
  414. }
  415.  
  416. /**searchOrders
  417. *
  418. * @param searchOrder
  419. * @return list of order
  420. */
  421. List<Order> searchOrders(Order searchOrder)
  422. {
  423. List<Order> orderList = new ArrayList<Order>();
  424. Order newOrder = null;
  425. boolean iNotFirst = false;
  426. PreparedStatement ps = null;
  427. ResultSet rs = null;
  428. String selectString = "SELECT BESTELLID,KUNDEID,ARTIKELIDENT,MENGE,BESTELLDATUM,NAME,VORNAME,BEZEICHNUNG FROM BESTELLUNGEN,ARTIKEL INNER JOIN PERSON ON KUNDEID = PERSID AND ARTIKELIDENT = ARTIKELID;";
  429. String conditionString = null;
  430.  
  431. if(searchOrder != null)
  432. {
  433. conditionString = "WHERE ";
  434. if(searchOrder.getBestellID() != -1)
  435. {
  436. conditionString += "BESTELLID LIKE( '%" + searchOrder.getBestellID() + "%')";
  437. iNotFirst = true;
  438. }
  439.  
  440. if(searchOrder.getKundeID() != -1)
  441. {
  442. if(iNotFirst)
  443. conditionString += " AND ";
  444.  
  445. conditionString += "KUNDEID LIKE( '%" + searchOrder.getKundeID() + "%')";
  446. iNotFirst=true;
  447. }
  448.  
  449. if(searchOrder.getArtikelID() != -1)
  450. {
  451. if(iNotFirst)
  452. conditionString += " AND ";
  453.  
  454. conditionString += "ARTIKELIDENT LIKE( '%" + searchOrder.getArtikelID() + "%')";
  455. iNotFirst=true;
  456. }
  457.  
  458. if(searchOrder.getMenge() != -1)
  459. {
  460. if(iNotFirst)
  461. conditionString += " AND ";
  462.  
  463. conditionString += "MENGE LIKE( '%" + searchOrder.getMenge() + "%')";
  464. iNotFirst=true;
  465. }
  466.  
  467. if(conditionString.equals(" WHERE "))
  468. conditionString = null;
  469. }
  470. try
  471. {
  472. if(conditionString != null)
  473. selectString += conditionString + " ORDER BY BESTELLID, BESTELLDATUM";
  474.  
  475. ps = con.prepareStatement(selectString);
  476. rs = ps.executeQuery(selectString);
  477.  
  478. while (rs.next())
  479. {
  480. String name = rs.getString(6) + " " + rs.getString(7);
  481. String bezeichnung = rs.getString(8);
  482. /*get bestellid, kundeid, artikelid, menge, bestalldatum, kundennr, name, vorname,strasse,nr,plz,ort,land*/
  483. newOrder = new Order(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4), rs.getTimestamp(5),name,bezeichnung);
  484.  
  485. orderList.add(newOrder);
  486. }
  487. rs.close();
  488. ps.close();
  489. }
  490. catch (SQLException e)
  491. {
  492. e.printStackTrace();
  493. System.out.println ("Failure with SQL-Statement.");
  494. showSQLException (e);
  495. }
  496.  
  497. return orderList;
  498. }
  499.  
  500.  
  501.  
  502.  
  503. /**updateOrder
  504. *
  505. * @param orderToUpdate
  506. * @return -1 = error, 0 = not inserted, 1 = inserted
  507. */
  508. int updateOrder(Order orderToUpdate)
  509. {
  510. int ret = 1;
  511. try
  512. {
  513. PreparedStatement ps = con.prepareStatement("UPDATE BESTELLUNGEN SET MENGE = ?,BESTELLDATUM = 'NOW',KUNDEID = ? WHERE BESTELLID = ?");
  514.  
  515. ps.setInt(1,orderToUpdate.getMenge());
  516. ps.setInt(2,orderToUpdate.getKundeID());
  517. ps.setInt(3,orderToUpdate.getBestellID());
  518.  
  519. ret = ps.executeUpdate();
  520. ps.close();
  521.  
  522. }
  523. catch (SQLException e)
  524. {
  525. e.printStackTrace();
  526. System.out.println ("Failure with SQL-Statement.");
  527. showSQLException (e);
  528. return -1;
  529. }
  530. return ret;
  531. }
  532.  
  533. /**deleteOrder
  534. *
  535. * @param orderID
  536. * @return -1 = error, 0 = not inserted, 1 = inserted
  537. */
  538. int deleteOrder(int orderID)
  539. {
  540. int ret = 1;
  541. try
  542. {
  543. PreparedStatement ps = con.prepareStatement("DELETE FROM BESTELLUNGEN " +
  544. "WHERE BESTELLID = ?");
  545. ps.setInt(1, orderID);
  546.  
  547. ret = ps.executeUpdate();
  548. ps.close();
  549. }
  550. catch (SQLException e)
  551. {
  552. e.printStackTrace();
  553. System.out.println ("Failure with SQL-Statement.");
  554. showSQLException (e);
  555. return 0;
  556. }
  557. return ret;
  558. }
  559. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement