JDBC configuration file, mail, password encryption tool class

Source: Internet
Author: User
Tags uuid

Configuration file

Url=jdbc:mysql:///sys
Classname=com.mysql.jdbc.driver
Username=root
password=123456
Maxactive=5
maxwait=5000
Initsize=3

Import java.io.IOException;
Import Java.security.MessageDigest;
Import java.util.Properties;
Import Java.util.UUID;

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.mail.internet.MimeMessage.RecipientType;

Import Org.apache.commons.dbcp.BasicDataSource;

Import Sun.misc.BASE64Encoder;

public class Jdbcutils {

//configuration file loading
public static Basicdatasource DS;
static{
Properties P = new properties ();
try {
P.load (JdbcUtils.class.getClassLoader (). getResourceAsStream ("properties/myconfig.properties"));
Ds=new Basicdatasource ();
Ds.seturl (p.getproperty ("url"));
Ds.setusername (P.getproperty ("username"));
Ds.setpassword (P.getproperty ("password"));
Ds.setdriverclassname (P.getproperty ("ClassName"));
Ds.setmaxactive (Integer.parseint (P.getproperty ("maxactive"));
Ds.setmaxwait (Long.parselong (P.getproperty ("maxwait"));
Ds.setinitialsize (Integer.parseint (P.getproperty ("initsize"));
} catch (IOException e) {

E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("File load failed ...");
}
}

public static Basicdatasource Getdatasource () {

return DS;
}
//Password encryption
public static string MD5 (String str) {
String Md5str=null;
try {
MessageDigest MD5 = messagedigest.getinstance ("MD5");
Base64encoder encode = new Base64encoder ();
Md5str=encode.encode (Md5.digest (Str.getbytes ("Utf-8"));
Md5str=md5str.substring (1,md5str.length ()-5);
StringBuilder sb = new StringBuilder (MD5STR);
Md5str=sb.reverse (). toString ();

} catch (Exception e) {
E.printstacktrace ();
}
return md5str;


}

Sending a message


Email Recipient's Address
Content information sent by message

public static void SendMail (String email,string content) {
try{
Create a program and mail server session object sessions
Properties Props = new properties ();
Set the protocol to send
Props.setproperty ("Mail.transport.protocol", "SMTP");
Set up a server to send mail to
Props.setproperty ("Mail.host", "smtp.sina.com");
Props.setproperty ("Mail.smtp.auth", "true");
Creating validators
Authenticator auth = new Authenticator () {

Public Passwordauthentication getpasswordauthentication () {
Set up sender's account and password
return new Passwordauthentication ("[Email protected]", "");
}
};
Create a session through the configuration of the server and the login validation object passed in
Send a message through this session
Session session = Session.getinstance (props, auth);
2. Create a message that is equivalent to the message content
Message message = new MimeMessage (session);
Set Sender
Message.setfrom (New InternetAddress ("[email protected]");
Set how and who to send
Message.setrecipient (recipienttype.to, new internetaddress (email));
Set up message delivery topics
Message.setsubject ("User activation");
Set up message content
Message.setcontent (Content, "text/html;charset=utf-8");
3 Create transport for sending messages
Transport.send (message);

}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("message sent Failed");

}


}

//random generation of a UUID string
public static String Getuuidstr () {

UUID uuid = Uuid.randomuuid ();
Return uuid.tostring (). ReplaceAll ("-", "" "). toUpperCase ();
}

}

JDBC configuration file, mail, password encryption tool class

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.