Java Basics-Send emai and access MySQL database (vii)

Source: Internet
Author: User
Tags stmt

Overview

  The Java program sends the e-mail very simple, but first you should install the JavaMail API and the Java Activation Framework (JAF) on your machine. Java Access data requires the use of JDBC to connect to the MySQL database. JDBC plays a database-driven role.

E-mail delivery

  Third-party packages, and then import the library file in the corresponding project after loading.

    • Visit JavaMail website to download the latest jar files;
    • Visit JAF (version 1.1.1) website to download the latest JAR file;

Following is the download, write test code, can use the NetEase SMTP service to send mail:

Package Commclass;import Java.util.properties;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; Public classEmailcommclass { Public Static voidSendEmail () {String to="[email protected]"; String from="[email protected]"; String Host="smtp.163.com"; Properties Properties=system.getproperties (); //setting up a mail serverProperties.setproperty ("Mail.smtp.host", host); Properties.put ("Mail.smtp.auth","true"); Properties.setproperty ("Mail.user","[email protected]"); Properties.setproperty ("Mail.password","fxxxx"); //get the default Session objectSession session = Session.getdefaultinstance (properties,NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("[email protected]","fxxxx");//Sender Mail user name, password            }        }); Try{mimemessage message=NewMimeMessage (session); Message.setfrom (NewInternetAddress ( from)); Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); Message.setsubject ("The Subject line!"); Message.settext ("This is actual message");            Transport.send (message); System. out. println ("Sent Message successfully ...."); }        Catch(Exception ex) {ex.printstacktrace (); }    }}

MySQL Link

  Java Connection MySQL needs a driver package, the latest version is:http://dev.mysql.com/downloads/connector/j/, extract the jar library file, and then import the library file in the corresponding project.

The following example uses JDBC to connect to the MySQL database:

Package Commclass;import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import java.sql.Statement; Public classMysqlhelper {//JDBC driver name and database URL    StaticFinal String Jdbc_driver ="Com.mysql.jdbc.Driver"; StaticFinal String Db_url ="Jdbc:mysql://192.168.1.102:3306/ruidb"; //The user name and password of the database need to be set according to their own    StaticFinal String USER ="Root"; StaticFinal String PASS ="Root";  Public Static voidOpeartdb () {Connection conn=NULL; Statement stmt=NULL; Try{//Class.forName (jdbc_driver);System. out. println ("Connect to Database ..."); Conn=drivermanager.getconnection (Db_url,user,pass); System. out. println ("Instantiate statement to ...."); stmt=conn.createstatement (); String SQL="SELECT * from T_student"; ResultSet RS=stmt.executequery (SQL);  while(Rs.next ()) {intId=rs.getint ("ID"); String Tname=rs.getstring ("Name"); System. out. printf ("id:%d,num:%s", Id,tname); }        }        Catch(Exception ex) {ex.printstacktrace (); }        finally{            //Close Resource            Try{                if(stmt!=NULL) Stmt.close (); }Catch(SQLException se2) {}//don't do anything.            Try{                if(conn!=NULL) Conn.close (); }Catch(SQLException se) {se.printstacktrace (); }        }            }}

Java Basics-Send emai and access MySQL database (vii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.