Advertisement
ZazoTazo

Database

Dec 9th, 2019
1,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeMessage;
  16.  
  17. public class DataBase {
  18.  
  19.     Connection con;
  20.     PreparedStatement pst2;
  21.     PreparedStatement pst;
  22.     PreparedStatement pst3;
  23.     java.sql.Statement stm;
  24.  
  25.     public DataBase() throws SQLException, ClassNotFoundException {
  26.         Class.forName("com.mysql.jdbc.Driver");
  27.         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/company", "root", "");
  28.         stm = con.createStatement();
  29.     }
  30.  
  31.     void insert() throws SQLException {
  32.         pst = con.prepareStatement("INSERT INTO `employee` (`Fname`, `Minit`, `Lname`, `Ssn`, `Bdate`, `Address`,"
  33.                 + " `Sex`, `Salary`, `Super_ssn`, `Dno`) VALUES ('alla', 'e', 'emad', '159442648', "
  34.                 + "'2019-11-14', 'dubai', 'F', '5000', '123456789', '1');");
  35.  
  36.     }
  37.  
  38.     void update() throws SQLException {
  39.         pst2 = con.prepareStatement("UPDATE `employee` SET `Fname` = 'Salwa' WHERE `Fname` = 'alla';");
  40.     }
  41.  
  42.     void delete() throws SQLException {
  43.         pst3 = con.prepareStatement("DELETE FROM `employee` WHERE SSN ='159442648';");
  44.     }
  45.  
  46.     String select() throws SQLException {
  47.         String s = "";
  48.         ResultSet rs = stm.executeQuery("select * from `employee`");
  49.         while (rs.next()) {
  50.             s += (rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + "\n"); //getInt()
  51.         }
  52.         return s;
  53.  
  54.     }
  55.    
  56.     void mailSent(){
  57.         final String username = "username@gmail.com";
  58.         final String password = "password";
  59.  
  60.         Properties prop = new Properties();
  61.         prop.put("mail.smtp.host", "smtp.gmail.com");
  62.         prop.put("mail.smtp.port", "465");
  63.         prop.put("mail.smtp.auth", "true");
  64.         prop.put("mail.smtp.socketFactory.port", "465");
  65.         prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  66.        
  67.         Session session;
  68.         session = Session.getInstance(prop,
  69.                 new javax.mail.Authenticator() {
  70.                     @Override
  71.                     protected PasswordAuthentication getPasswordAuthentication() {
  72.                         return new PasswordAuthentication(username, password);
  73.                     }
  74.                 });
  75.  
  76.         try {
  77.  
  78.             Message message = new MimeMessage(session);
  79.             message.setFrom(new InternetAddress("from@gmail.com"));
  80.             message.setRecipients(
  81.                     Message.RecipientType.TO,
  82.                     InternetAddress.parse("to_username_a@gmail.com, to_username_b@yahoo.com")
  83.             );
  84.             message.setSubject("Testing Mail");
  85.             message.setText("Email Sent!");
  86.  
  87.             Transport.send(message);
  88.  
  89.             System.out.println("Done");
  90.  
  91.         } catch (MessagingException e) {
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement