DanielMaiaRocha / Sistema

0 stars 0 forks source link

Banco de dados projeto JAVA #2

Open DanielMaiaRocha opened 1 year ago

DanielMaiaRocha commented 1 year ago

import javax.swing.; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.date; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.JFrame; import java.util.Properties; import javax.mail.Address; 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; import org.joda.time.DateTime;

public class JavaMailApp {

private Session criarSessionMail() { Properties props = new Properties();

props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", 465); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", true); props.put("mail.smtp.port", 465);

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication (sistema.slxadm@gmail.com, 8520147);
    }

}); session.setDebug(true); return session; }

public static void main(String[] args){}

public class ClienteDAO{ private Connection conn; private Statement stmt;

public void dbConnect() {
    try {
        conn = DriverManager.getConnection("jdbc:sqlite:bancoslx.db");
        stmt = conn.createStatement();
        System.out.println("Conectado ao banco");

    } cacth (SQLException e) {
        e.printlnStackTrace();

    }
}

public void dbDisconnect() {
    try {
        if(stmt != null){
            stmt.close();

        }

        if(conn != null){
            conn.close();

        }
        System.out.println("Desconectado do banco");
    } cacth (SQLException e) {
        e.printlnStackTrace();
    }
}   

public void dbCreate() {
    dbConnect();
    try {

        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS clientes ("
            + "cod INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "nome_proprietario CHAR(40) NOT NULL, "
            + "nome_cliente CHAR(40) NOT NULL, "
            + "cpf VARCHAR(25) NOT NULL, "
            + "endereco CHAR(40) NOT NULL, "
            + "inicio_contrato VARCHAR(12) NOT NULL, "
            + "aniversario_contrato VARCHAR(12) NOT NULL, "
            + "fim_contrato VARCHAR(12) NOT NULL"
            + ");");
        System.out.println("Tabela criada com sucesso.");    
    } cacth (SQLException e) { 
        e.printlnStackTrace();
    } finally {
        dbDisconnect();
    }
}

public void addNewClient(String nomeProprietario, String nomeCliente,  String endereco, String cpf,
String inicioContrato, String aniversarioContrato, String fimContrato) { 
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    try {
        Date start = sdf.parse(inicioContrato);
        Data renew = sdf.parse(aniversarioContrato);
        Data end = sdf.parse(fimContrato);

        dbConnect();
        PreparedStatement, pstmt = conn.PreparedStatement("INSERT INTO clientes (nome_proprietario, nome_cliente, endereco, cpf, inicio_contrato, aniversario_contrato, fim_contrato) VALUES (?, ?, ?, ?, ?, ?, ?);");
        pstmt.setString(1, nomeProprietario)
        pstmt.setString(2, nomeCliente)
        pstmt.setString(3, endereco)
        pstmt.setString(4, cpf)
        pstmt.setString(5, new java.sql.Date(start.getTime()));
        pstmt.setString(6, new java.sql.Date(renew.getTime()));
        pstmt.setString(7, new java.sql.Date(end.getTime()));

        pstmt.executeUpdate();

        System.out.println("Novo Cliente adicionado com sucesso.")
    } cacth (Exeption e) {
        e.printlnStackTrace();
    } finally {
        dbDisconnect();
    }
}

public static void main(String[] args) {
    ClienteDAO dao = new ClienteDAO();
    dao.dbCreate();
    dao.addNewClient("Proprietario", "Cliente", "Endereco", "123456789", "01/01/2023", "01/01/2024", "01/01/2025")
}

}

private void enviarEmail() throws AddressException, MessagingException {
}

String msg = jTextAreaMensagem.getText(); String assunto = jTextFieldAssunto.getText(); String email = jTextFieldEmailDestino.getText();

String remetente = "remetente@gmail.com"; System.out.println("Enviando email DE: " + remetente + " PARA: " + email); System.out.println("Assunto: " + assunto);

Message message = new MimeMessage(criarSessionMail()); message.setFrom(new InternetAddress(remetente)); // Remetente

Address[] toUser = InternetAddress // Destinatário(s) .parse(email.trim().toLowerCase());

message.setRecipients(Message.RecipientType.TO, toUser); message.setSubject(assunto);// Assunto message.setContent(msg, "text/html"); /* Método para enviar a mensagem criada / Transport.send(message);

System.out.println("Email enviado com sucesso !"); System.out.println("__")