data connection fixed

pull/10/head
parent 88187c3c98
commit 1442a04744

@ -47,9 +47,8 @@
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.14</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>

@ -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();
}
}

@ -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<MailModel> 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()));

Loading…
Cancel
Save