Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class TestSendEmails {
- private String emailTo;
- private String emailSubject;
- private String emailBody;
- private String emailAttachments;
- public TestSendEmails(){
- }
- public TestSendEmails(String emailTo, String emailSubject, String emailBody, String emailAttachments){
- super();
- this.emailTo = emailTo;
- this.emailSubject = emailSubject;
- this.emailBody = emailBody;
- this.emailAttachments = emailAttachments;
- }
- public String getEmailTo(){
- return emailTo;
- }
- public void setEmailTo(String emailTo){
- this.emailTo = emailTo;
- }
- public String getEmailSubject(){
- return emailSubject;
- }
- public void setEmailSubject(String emailSubject){
- this.emailSubject = emailSubject;
- }
- public String getEmailBody(){
- return emailBody;
- }
- public void setEmailBody(String emailBody){
- this.emailBody = emailBody;
- }
- public String getEmailAttachments(){
- return emailAttachments;
- }
- public void setEmailAttachments(String emailAttachments){
- this.emailAttachments = emailAttachments;
- }
- }
- class TestSendEmailD{
- private Connection con;
- private static final String GET_EMAILS = "Select* From Emails";
- private void connect() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
- con = DriverManager.getConnection("jdbc:sqlserver://100.000.000.00\:3333;databaseName=Test;user=mmmm;password=1234");
- }
- public List<TestSendEmails> getTestSendEmails() throws Exception{
- connect();
- PreparedStatement ps = con.prepareStatement(GET_EMAILS);
- ResultSet rs = ps.executeQuery();
- List<TestSendEmails> result = new ArrayList<TestSendEmails>();
- while(rs.next()){
- result.add(new TestSendEmails(rs.getString("emailTo"), rs.getString("emailSubject"),rs.getString("emailBody"),rs.getString("emailAttachments")));
- }
- disconnect();
- return result;
- }
- private void disconnect() throws SQLException{
- if(con != null){
- con.close();
- }
- }
- }
- class EmailSender{
- private Session session;
- private void init(){
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "100.000.000.00");
- props.put("mail.smtp.port", "678");
- session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("test@gmail.com", "123");
- }
- });
- }
- public void sendEmail(TestSendEmails s) throws MessagingException{
- init();
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("test@gmail.com"));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(s.getEmailTo().replace(";", ",")));
- message.setSubject(s.getEmailSubject());
- message.setText(s.getEmailBody());
- message.setContent(s.getEmailBody(),"text/html");
- Transport.send(message);
- System.out.println("Done");
- }
- public void sendEmail(List<TestSendEmails> emails) throws MessagingException{
- for(TestSendEmails TestSendEmails:emails ){
- sendEmail(TestSendEmails);
- }
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.util.Date;
- public class UpdateEmail {
- public static Connection getConnection() throws Exception {
- String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- String url = "jdbc:sqlserver://100.000.000.00\:3333;databaseName=Test";
- String username = "mmmm";
- String password = "1234";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, username, password);
- return conn;
- }
- public static void main(String[] args) throws Exception {
- java.util.Date date = new Date();
- Connection conn = null;
- PreparedStatement pstmt = null;
- try {
- conn = getConnection();
- String query = "update Emails set SentOn = ? where Id = ? ";
- pstmt = conn.prepareStatement(query); // create a statement
- pstmt.setTimestamp(1, new java.sql.Timestamp(date.getTime()));
- pstmt.setInt(2, 200); // In this line I want to use my array-list to update my table.
- pstmt.executeUpdate(); // execute update statement
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- } finally {
- pstmt.close();
- conn.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement