diff --git a/pom.xml b/pom.xml
index 899ceb1..357401d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,9 +47,8 @@
8.0.33
- org.springframework
- spring-jdbc
- 5.3.14
+ org.springframework.boot
+ spring-boot-starter-jdbc
org.springframework
diff --git a/src/main/java/entgra/mailsender/Config/DataSourceConfig.java b/src/main/java/entgra/mailsender/Config/DataSourceConfig.java
new file mode 100644
index 0000000..da42147
--- /dev/null
+++ b/src/main/java/entgra/mailsender/Config/DataSourceConfig.java
@@ -0,0 +1,35 @@
+package entgra.mailsender.Config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+import javax.sql.DataSource;
+
+@Configuration
+public class DataSourceConfig {
+ @Value("${spring.datasource.url}")
+ private String dbUrl;
+
+ @Value("${spring.datasource.username}")
+ private String dbUserName;
+
+ @Value("${spring.datasource.password}")
+ private String dbPassword;
+
+ @Value("${spring.datasource.driver-class-name}")
+ private String dbDriver;
+
+ @Primary
+ @Bean
+ public DataSource dataSource(){
+ return DataSourceBuilder.create()
+ .url(dbUrl)
+ .username(dbUserName)
+ .password(dbPassword)
+ .driverClassName(dbDriver)
+ .build();
+ }
+}
diff --git a/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java
index 4f32bbf..fd0dd30 100644
--- a/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java
+++ b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java
@@ -9,6 +9,7 @@ import entgra.mailsender.exception.FileConversionException;
import entgra.mailsender.exception.MailProcessingException;
import org.springframework.stereotype.Component;
+import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -28,16 +29,22 @@ public class MailDAOImpl implements MailDAO {
Logger logger = Logger.getLogger(String.valueOf(MailDAOImpl.class));
- private Connection getConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/email_sending", "root", "StrongPassword123!");
+ private DataSource dataSource;
+
+ public MailDAOImpl(DataSource dataSource){
+ this.dataSource = dataSource;
}
+// private Connection getConnection() throws SQLException {
+// return DriverManager.getConnection("jdbc:mysql://localhost:3306/email_sending", "root", "StrongPassword123!");
+// }
+
@Override
public int addMail(MailModel mailModel) throws SQLException {
try {
PreparedStatement stmt;
int generatedId;
- Connection conn = this.getConnection();
+ Connection conn = dataSource.getConnection();
stmt = conn.prepareStatement("INSERT INTO EMAIL (EMAIL_ADDRESS, MSG_TEMPLATE, PRIORITY, ATTACHMENT, PARAMETERS, TIME, FILENAME, EXPIRY_AT) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS);
Timestamp current_time = new Timestamp(System.currentTimeMillis());
String parametersJson = new ObjectMapper().writeValueAsString(mailModel.getParameters());
@@ -74,7 +81,7 @@ public class MailDAOImpl implements MailDAO {
List unsentMails = new ArrayList<>();
- try (Connection connection = this.getConnection();
+ try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
@@ -98,7 +105,7 @@ public class MailDAOImpl implements MailDAO {
MailModel mailModel = new MailModel();
try {
- Connection conn = this.getConnection();
+ Connection conn = dataSource.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, mail_id);
try (ResultSet rs = stmt.executeQuery()) {
@@ -160,7 +167,7 @@ public class MailDAOImpl implements MailDAO {
public void addToSentMail(Integer mail_id) {
try {
PreparedStatement stmt;
- Connection connection = this.getConnection();
+ Connection connection = dataSource.getConnection();
stmt = connection.prepareStatement("INSERT INTO SENTEMAIL (EMAIL_ID,SENT_TIME) VALUE (?,?)");
stmt.setInt(1, mail_id);
stmt.setDate(2, Date.valueOf(LocalDate.now()));