1-
21package data .databaseAccessObjects .mappers ;
32
43import data .databaseAccessObjects .DBConnector ;
1211
1312/**
1413 *
15- * @author
14+ * @author
1615 */
17- public class MaterialMapper {
16+ public class MaterialMapper
17+ {
1818
1919 private static MaterialMapper materialMapper ;
2020
21- private MaterialMapper () {
21+ private MaterialMapper ()
22+ {
23+
2224 }
2325
24- public static MaterialMapper getInstance () {
25- if (materialMapper == null ) {
26+ public static MaterialMapper getInstance ()
27+ {
28+ if (materialMapper == null )
29+ {
2630 materialMapper = new MaterialMapper ();
2731 }
2832 return materialMapper ;
@@ -36,22 +40,25 @@ public static MaterialMapper getInstance() {
3640 * @return name of the category.
3741 * @throws LoginException Should most likely throw something else.
3842 */
39- public String getCategory (int id ) throws LoginException {
43+ public String getCategory (int id ) throws LoginException
44+ {
4045 String SQL = "SELECT `category`.`category_name`\n "
4146 + "FROM `carportdb`.`category`\n "
4247 + "WHERE `category`.`id_category` = ?;" ;
43-
44- try {
45- Connection con = DBConnector . connection ();
46- PreparedStatement ps = con . prepareStatement ( SQL );
48+ // Using try-with resources, so they automatically close afterwards.
49+ try ( Connection con = DBConnector . connection ();
50+ PreparedStatement ps = con . prepareStatement ( SQL );)
51+ {
4752 String category = "" ;
4853 ps .setInt (1 , id );
4954 ResultSet rs = ps .executeQuery ();
50- while (rs .next ()) {
55+ while (rs .next ())
56+ {
5157 category = rs .getString ("category_name" );
5258 }
5359 return category ;
54- } catch (SQLException ex ) {
60+ } catch (SQLException ex )
61+ {
5562 // Should most likely be another exception.
5663 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
5764 }
@@ -66,24 +73,23 @@ public String getCategory(int id) throws LoginException {
6673 * @return MaterialModel
6774 * @throws LoginException Should probably be something else later on.
6875 */
69- public MaterialModel getMaterial (int id ) throws LoginException {
76+ public MaterialModel getMaterial (int id ) throws LoginException
77+ {
7078 MaterialModel material = new MaterialModel ();
7179
72- String SQL = "SELECT `description`, height, width, length, cost_price, unit, category_name \n "
73- + "FROM materials \n "
74- + "INNER JOIN `category` \n "
75- + "ON `materials`.`id_category` = `category`.`id_category` \n "
76- + "WHERE `materials`.`id_material` = ?;" ;
77-
78- try {
79- Connection con = DBConnector .connection ();
80- PreparedStatement ps = con .prepareStatement (SQL );
80+ String SQL = "SELECT `description`, height, width, length, cost_price, unit, category_name "
81+ + "FROM materials INNER JOIN `category` ON `materials`.`id_category` = `category`.`id_category`;" ;
82+ // Using try-with resources, so they automatically close afterwards.
83+ try (Connection con = DBConnector .connection ();
84+ PreparedStatement ps = con .prepareStatement (SQL );)
85+ {
8186 material .setID (id );
8287
8388 ps .setInt (1 , id );
8489 ResultSet rs = ps .executeQuery ();
8590
86- while (rs .next ()) {
91+ while (rs .next ())
92+ {
8793 String description = rs .getString ("description" );
8894 material .setDescription (description );
8995
@@ -103,10 +109,12 @@ public MaterialModel getMaterial(int id) throws LoginException {
103109 material .setUnit (unit );
104110
105111 String categoryname = rs .getString ("category_name" );
106- material .setCategory (categoryname );
112+ material .setCategory (categoryname );
107113
114+
108115 }
109- } catch (SQLException ex ) {
116+ } catch (SQLException ex )
117+ {
110118 // Should most likely be another exception.
111119 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
112120 }
@@ -123,21 +131,25 @@ public MaterialModel getMaterial(int id) throws LoginException {
123131 * @return name of the category.
124132 * @throws LoginException Should most likely throw something else.
125133 */
126- public String getOrderDetailsCategory (int id ) throws LoginException {
134+ public String getOrderDetailsCategory (int id ) throws LoginException
135+ {
127136 String SQL = "SELECT `order_details_category`.`details_category_name`\n "
128137 + "FROM `carportdb`.`order_details_category`\n "
129138 + "WHERE `order_details_category`.`id_order_details_category` = ?;" ;
130- try {
131- Connection con = DBConnector .connection ();
132- PreparedStatement ps = con .prepareStatement (SQL );
139+ // Using try-with resources, so they automatically close afterwards.
140+ try (Connection con = DBConnector .connection ();
141+ PreparedStatement ps = con .prepareStatement (SQL );)
142+ {
133143 String category = "" ;
134144 ps .setInt (1 , id );
135145 ResultSet rs = ps .executeQuery ();
136- while (rs .next ()) {
146+ while (rs .next ())
147+ {
137148 category = rs .getString ("details_category_name" );
138149 }
139150 return category ;
140- } catch (SQLException ex ) {
151+ } catch (SQLException ex )
152+ {
141153 // Should most likely be another exception.
142154 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
143155 }
@@ -147,27 +159,30 @@ public String getOrderDetailsCategory(int id) throws LoginException {
147159 // <editor-fold defaultstate="collapsed" desc="Get all Materials for an Order Details">
148160 /**
149161 * Get a List of Materials.
150- *
151162 * @param id of the Order Details.
152163 * @return List of MaterialModel.
153164 * @throws LoginException Should most likely throw something else.
154165 */
155- public PartslistModel getMaterials (int id ) throws LoginException {
166+ public PartslistModel getMaterials (int id ) throws LoginException
167+ {
156168 PartslistModel materials = new PartslistModel ();
157169 String SQL = "SELECT `order_details`.`id_material`\n "
158170 + "FROM `carportdb`.`order_details`\n "
159171 + "WHERE `order_details`.`id_order_details` = ?;" ;
160172
161- try {
162- Connection con = DBConnector .connection ();
163- PreparedStatement ps = con .prepareStatement (SQL );
173+ // Using try-with resources, so they automatically close afterwards.
174+ try (Connection con = DBConnector .connection ();
175+ PreparedStatement ps = con .prepareStatement (SQL );)
176+ {
164177 ps .setInt (1 , id );
165178 ResultSet rs = ps .executeQuery ();
166- while (rs .next ()) {
179+ while (rs .next ())
180+ {
167181 MaterialModel material = getMaterial (rs .getInt ("id_material" ));
168182 materials .addMaterial (material );
169183 }
170- } catch (SQLException ex ) {
184+ } catch (SQLException ex )
185+ {
171186 // Should most likely be another exception.
172187 throw new LoginException (ex .getMessage ()); // ex.getMessage() Should not be in production.
173188 }
0 commit comments