I'm 16 years old and no one to help me, no one to give any advice or constructive criticism, I'm aimless. I'll leave the link to my last code (github), a program that sends an email to everyone registered in the database. I would like some advice and project/content ideas for me to evolve. Evaluate my project sincerely.
UPDATE: According to the last suggestion (in the last post), the program was very dependent, not following the S.O.L.I.D concepts, one class was doing all the work, so I created 2 more classes to divide the functions (FactoryConnection and DAO).
Class DAO, this class is responsible for creating the SELECT statement (MySql) that connects to return users:
package project;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DAO {
static Connection conn;
static ResultSet users;
static PreparedStatement ps;
protected static ResultSet retriveUsers() {
FactoryConnection.closeConnections(conn, ps, users); // if there is any new connection, it will close the previous one
try {
conn = FactoryConnection.getConnectionToMySql();
ps = (PreparedStatement) conn.prepareStatement("select * from users");
users = ps.executeQuery();
return users;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Class FactoryConnection, this class will create the connection to MySql (getConnectionToMySql)database and close the connections (closeConnections) when called:
package project;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
public class FactoryConnection {
protected static Connection getConnectionToMySql() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emailproject?useTimezone=true&serverTimezone=UTC", "root", "");
return conn;
}
protected static void closeConnections(Connection conn, PreparedStatement ps, ResultSet users) {
DbUtils.closeQuietly(conn);
DbUtils.closeQuietly(users);
DbUtils.closeQuietly(ps);
}
}
Class JavaMailUtil, this class will send the email to the users (using the DAO class to get the users in database):
package project;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JavaMailUtil {
Connection conn;
PreparedStatement ps;
ResultSet rs;
String sender = "[email protected]";
String senderPassword = "******";
public void sendEmail(String title, String text) {
try {
ResultSet users = DAO.retriveUsers();
while (users.next()) {
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, senderPassword);
}
});
Message message = prepareMessage(session, sender, users.getString(2), text, title);
Transport.send(message);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error!");
e.printStackTrace();
return;
}
JOptionPane.showMessageDialog(null, "Email successfully sent!");
}
private static Message prepareMessage(Session session, String sender, String recepient, String text, String title) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
message.setSubject(title);
message.setText(text);
return message;
} catch (Exception e) {
Logger.getLogger(JavaMailUtil.class.getName()).log(Level.SEVERE, null, e);
}
return null;
}
}
Swing Part, the main class that will call the codes:
package windows;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import project.JavaMailUtil;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class MainWindow {
private JFrame frame;
private JTextField txtTitle;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null);
JLabel lbl1 = new JLabel("Title:");
lbl1.setFont(new Font("Tahoma", Font.PLAIN, 18));
lbl1.setBounds(10, 81, 43, 26);
frame.getContentPane().add(lbl1);
JLabel lbl2 = new JLabel("Text:");
lbl2.setFont(new Font("Tahoma", Font.PLAIN, 18));
lbl2.setBounds(10, 133, 43, 26);
frame.getContentPane().add(lbl2);
txtTitle = new JTextField();
txtTitle.setHorizontalAlignment(SwingConstants.CENTER);
txtTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
txtTitle.setBounds(54, 77, 342, 37);
frame.getContentPane().add(txtTitle);
txtTitle.setColumns(10);
JButton buttonSend = new JButton("Send Email");
buttonSend.setFont(new Font("Tahoma", Font.PLAIN, 16));
buttonSend.setBounds(269, 227, 127, 23);
frame.getContentPane().add(buttonSend);
JTextArea txtText = new JTextArea();
txtText.setFont(new Font("Tahoma", Font.BOLD, 15));
txtText.setBounds(54, 136, 342, 80);
frame.getContentPane().add(txtText);
txtText.setLineWrap(true);
txtText.setWrapStyleWord(true);
JLabel lblNewLabel = new JLabel("Automatic Sender");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 23));
lblNewLabel.setBounds(95, 22, 243, 37);
frame.getContentPane().add(lblNewLabel);
JButton buttonUsers = new JButton("Users");
buttonUsers.setFont(new Font("Tahoma", Font.PLAIN, 16));
buttonUsers.setBounds(54, 227, 83, 23);
frame.getContentPane().add(buttonUsers);
// TODO
buttonSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JavaMailUtil javaMailUtil = new JavaMailUtil();
buttonSend.setText("Sending...");
JOptionPane.showMessageDialog(null, "Sending... Wait the alert!");
javaMailUtil.sendEmail(txtTitle.getText(), txtText.getText());
buttonSend.setText("Send Email");
}
});
}
}
GitHub Link: https://github.com/DaviRibeiro-prog/JAVA/tree/main/EmailSenderProject/src