55import java .sql .Connection ;
66import java .sql .Date ;
77import java .sql .DriverManager ;
8+ import java .sql .PreparedStatement ;
89import java .sql .ResultSet ;
910import java .sql .SQLException ;
1011import java .sql .Statement ;
2324
2425import com .sun .net .ssl .internal .ssl .Provider ;
2526
27+ import database .bean .student .Student ;
28+
2629/**
2730 * This class contains static methods that would be used to establish
2831 * a connection to the MySql database. <br>
@@ -91,38 +94,38 @@ public static boolean sendMail( String[] recipient, String body, String subject)
9194 //Compose the message
9295 List <InternetAddress > addresses =
9396 Arrays .stream (recipient )
94- .map (elem -> {
95- try
96- {
97- return new InternetAddress (elem );
98- }
99- catch (AddressException e )
100- {
101- // TODO Auto-generated catch block
102- e .printStackTrace ();
103- }
104- return null ;
105- })
106- .filter (elem -> elem != null )
107- .collect (Collectors .toList ());
108-
97+ .map (elem -> {
98+ try
99+ {
100+ return new InternetAddress (elem );
101+ }
102+ catch (AddressException e )
103+ {
104+ // TODO Auto-generated catch block
105+ e .printStackTrace ();
106+ }
107+ return null ;
108+ })
109+ .filter (elem -> elem != null )
110+ .collect (Collectors .toList ());
111+
109112 InternetAddress [] addrArray =
110- addresses .toArray (new InternetAddress [addresses .size ()]);
111-
113+ addresses .toArray (new InternetAddress [addresses .size ()]);
114+
112115 MimeMessage message = new MimeMessage (mailSession );
113116 message .setFrom (new InternetAddress (user ));
114-
117+
115118 message .addRecipients (Message .RecipientType .TO ,addrArray );
116119 message .setSubject (subject );
117120 message .setSentDate ( new java .util .Date ());
118121 message .setText ( body );
119-
122+
120123 //send the message
121124 Transport transport = mailSession .getTransport ("smtp" );
122125 transport .connect (host , user , password );
123-
126+
124127 transport .sendMessage (message , message .getAllRecipients ());
125-
128+
126129 transport .close ();
127130
128131 }
@@ -145,13 +148,14 @@ protected static boolean openConnection()
145148 try
146149 {
147150 conn = DriverManager .getConnection (MYSQLURL , USERNAME , PASSWORD );
148-
151+
149152 ResultSet result = null ;
150153 try ( Statement stmt = conn .createStatement ();)
151154 {
152155 result = stmt .executeQuery ("SELECT NOW();" );
153156 if ( result .next ()){
154157 currentDate = result .getDate (1 );
158+ initialiseStudentFile ();
155159 System .out .println ( "Connected" );
156160 return true ;
157161 }
@@ -169,6 +173,40 @@ protected static boolean openConnection()
169173
170174
171175
176+ private static void initialiseStudentFile ()
177+ {
178+ String sql = "SELECT DefaultImage FROM resources " ;
179+
180+ ResultSet result = null ;
181+ try (PreparedStatement stmt = DatabaseManager .getPreparedStatement (sql );){
182+ result = stmt .executeQuery ();
183+ if (result .next ()){
184+ Student .setDefaultImage (result .getBinaryStream (1 ));
185+ }
186+
187+ }
188+ catch (SQLException e )
189+ {
190+ // TODO Auto-generated catch block
191+ e .printStackTrace ();
192+ }finally {
193+ if ( result !=null )
194+ try
195+ {
196+ result .close ();
197+ }
198+ catch (SQLException e )
199+ {
200+ // TODO Auto-generated catch block
201+ e .printStackTrace ();
202+ }
203+ }
204+
205+
206+
207+ }
208+
209+
172210 /**THis gets the only {@code Connection } object in this project*/
173211 public Connection getConnection ()
174212 {
0 commit comments