javaweb中的常用util类

目录

    • CommonsUtils.java
    • DataSourceUtils.java
    • MailUtils.java
    • MD5Utils.java

CommonsUtils.java

作用:随机生成id

package com.itheima.utils;

import java.util.UUID;

public class CommonsUtils {
     
	
	//生成uid 
	public static String getUUID() {
     
		return UUID.randomUUID().toString();
	}
}

DataSourceUtils.java

package com.itheima.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceUtils {
     
	
	
	private static DataSource dataSource = new ComboPooledDataSource();

	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	// 直接可以获取一个连接池
	public static DataSource getDataSource() {
     
		return dataSource;
	}

	// 获取连接对象
	public static Connection getConnection() throws SQLException {
     

		Connection con = tl.get();
		if (con == null) {
     
			con = dataSource.getConnection();
			tl.set(con);
		}
		return con;
	}

	// 开启事务
	public static void startTransaction() throws SQLException {
     
		Connection con = getConnection();
		if (con != null) {
     
			con.setAutoCommit(false);
		}
	}

	// 事务回滚
	public static void rollback() throws SQLException {
     
		Connection con = getConnection();
		if (con != null) {
     
			con.rollback();
		}
	}

	// 提交并且 关闭资源及从ThreadLocall中释放
	public static void commitAndRelease() throws SQLException {
     
		Connection con = getConnection();
		if (con != null) {
     
			con.commit(); // 事务提交
			con.close();// 关闭资源
			tl.remove();// 从线程绑定中移除
		}
	}

	// 关闭资源方法
	public static void closeConnection() throws SQLException {
     
		Connection con = getConnection();
		if (con != null) {
     
			con.close();
		}
	}

	public static void closeStatement(Statement st) throws SQLException {
     
		if (st != null) {
     
			st.close();
		}
	}

	public static void closeResultSet(ResultSet rs) throws SQLException {
     
		if (rs != null) {
     
			rs.close();
		}
	}

}

MailUtils.java

作用:发生邮件

package com.itheima.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {
     
	

	public static void sendMail(String email, String emailMsg)
			throws AddressException, MessagingException {
     
		// 1.创建一个程序与邮件服务器会话对象 Session

		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");
		props.setProperty("mail.host", "smtp.qq.com");
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true

		// 创建验证器
		Authenticator auth = new Authenticator() {
     
			public PasswordAuthentication getPasswordAuthentication() {
     
				return new PasswordAuthentication("[email protected]", "fmlhbsoeglfcdhdd");
			}
		};

		Session session = Session.getInstance(props, auth);

		// 2.创建一个Message,它相当于是邮件内容
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("[email protected]")); // 设置发送者

		message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

		message.setSubject("用户激活");
		// message.setText("这是一封激活邮件,请点击");

		message.setContent(emailMsg, "text/html;charset=utf-8");

		// 3.创建 Transport用于将邮件发送

		Transport.send(message);
	}

}

MD5Utils.java

package com.itheima.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
     
	
	
	/**
	 * 使用md5的算法进行加密
	 */
	public static String md5(String plainText) {
     
		byte[] secretBytes = null;
		try {
     
			secretBytes = MessageDigest.getInstance("md5").digest(
					plainText.getBytes());
		} catch (NoSuchAlgorithmException e) {
     
			throw new RuntimeException("没有md5这个算法!");
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
		// 如果生成数字未满32位,需要前面补0
		for (int i = 0; i < 32 - md5code.length(); i++) {
     
			md5code = "0" + md5code;
		}
		return md5code;
	}

	public static void main(String[] args) {
     
		System.out.println(md5("123"));
	}

}

你可能感兴趣的:(java,java,javamail)