MD5密码隐藏

在这里插入图片描述
log4j.properties:

# DEBUG\u8BBE\u7F6E\u8F93\u51FA\u65E5\u5FD7\u7EA7\u522B\uFF0C\u7531\u4E8E\u4E3ADEBUG\uFF0C\u6240\u4EE5ERROR\u3001WARN\u548CINFO \u7EA7\u522B\u65E5\u5FD7\u4FE1\u606F\u4E5F\u4F1A\u663E\u793A\u51FA\u6765
log4j.rootLogger=DEBUG,Console,RollingFile

#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern= [%-5p]-[%d{yyyy-MM-dd HH:mm:ss}] -%l -%m%n
#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u64CD\u4F5C\u7CFB\u7EDFD\u76D8\u6839\u76EE\u5F55\u4E0B\u7684log.log\u6587\u4EF6\u4E2D
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=D://log.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p %-40.40c %X{traceId}-%m%n

DBLink.java:

package com.jd.tool.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.jd.tool.PropertiesTool;

/**
 * 鏁版嵁搴撶鐞嗗伐鍏风被
 *
 * @author GaoHuanjie
 */
public class DBLink {
	
	private Logger logger = Logger.getLogger(DBLink.class);
	
	/**
	 * 鑾峰彇鏁版嵁搴撹繛鎺�
	 *
	 * @author GaoHuanjie
	 */
	private Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
			String userName = PropertiesTool.getValue("db.username");
			String password = PropertiesTool.getValue("db.password");
			String url = PropertiesTool.getValue("db.url");
			return DriverManager.getConnection(url, userName, password);//鑾峰彇杩炴帴
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 鍒ゆ柇SQL璇彞鏄惁鑳芥煡鍑烘暟鎹�
	 *
	 * @author GaoHuanjie
	 */
	public boolean exist(String sql) {
		Connection connection = null;
		Statement statement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//鑾峰彇杩炴帴
			statement = connection.createStatement();
			resultSet= statement.executeQuery(sql);//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
			return resultSet.next();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(resultSet,statement,connection);
		}
		return false;
	}
	
	/**
	 * 鍒ゆ柇SQL璇彞鏄惁鑳芥煡鍑烘暟鎹�
	 *
	 * @author GaoHuanjie
	 */
	public boolean exist(String sql, Object ...params) {
		Connection connection = null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//鑾峰彇杩炴帴
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);
			}
			resultSet= preparedStatement.executeQuery();//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
			return resultSet.next();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(resultSet,preparedStatement,connection);
		}
		return false;
	}
	
	/**
	 * 鏌ヨ鏁版嵁
	 *
	 * @author GaoHuanjie
	 */
	public void select(String sql,IRowMapper rowMapper) {//鎺ュ彛鏃犳硶鍒涘缓瀵硅薄锛屾墍浠owMapper鍙傛暟涓�瀹氭寚鍚慖RowMapper鎺ュ彛瀹炵幇绫诲璞�
		Connection connection = null;
		Statement statement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//鑾峰彇杩炴帴
			statement = connection.createStatement();
			resultSet= statement.executeQuery(sql);//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
			rowMapper.rowMapper(resultSet);//鍥犱负rowMapper鍙傛暟鎸囧悜IRowMapper鎺ュ彛瀹炵幇绫诲璞★紝鎵�浠ユ澶勫皢璋冪敤鎺ュ彛瀹炵幇绫讳腑鎵�瀹炵幇鐨剅owMapper鏂规硶  澶氭��
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//閲婃斁璧勬簮
			close(resultSet,statement,connection);
		}
	}
	
	/**
	 * 鏌ヨ鏁版嵁
	 *
	 * @author GaoHuanjie
	 */
	public void select(String sql,IRowMapper rowMapper,Object ...params) {
		Connection connection = null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//鑾峰彇杩炴帴
			preparedStatement= connection.prepareStatement(sql);//鍚湁锛熷彿鍗犱綅绗︾殑sql
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);//涓猴紵鍙疯祴鍊�
			}
			resultSet= preparedStatement.executeQuery();//鎵цsql
			rowMapper.rowMapper(resultSet);//鍥犱负rowMapper鍙傛暟鎸囧悜IRowMapper鎺ュ彛瀹炵幇绫诲璞★紝鎵�浠ユ澶勫皢璋冪敤鎺ュ彛瀹炵幇绫讳腑鎵�瀹炵幇鐨剅owMapper鏂规硶  澶氭��
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//閲婃斁璧勬簮
			close(resultSet,preparedStatement,connection);
		}
	}

	/**
	 * 淇敼锛坕nsert銆乽pdate鍜宒elete锛夋暟鎹�
	 *
	 * @author GaoHuanjie
	 */
	public boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();//鑾峰彇鏁版嵁搴撹繛鎺ュ璞★紝涓�涓璞¤〃绀轰竴娆℃暟鎹簱杩炴帴
			statement = connection.createStatement();//鑾峰彇Statement瀵硅薄
			int result = statement.executeUpdate(sql);//鎵цsql璇彞锛岃繑鍥炲彈褰卞搷鐨勮鏁帮紝浠呴檺浜庢暟鎹甶nsert銆乽pdate鍜宒elete
			return result>0;//澶勭悊缁撴灉
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//鍗充究鏈夊紓甯镐篃浼氭墽琛屼唬鐮�
			close(statement,connection);
		}
		return false;
	}
	
	/**
	 * 淇敼锛坕nsert銆乽pdate鍜宒elete锛夋暟鎹�
	 *
	 * @author GaoHuanjie
	 */
	public boolean update(String sql,Object ...params) {
		Connection connection= null;
		PreparedStatement preparedStatement= null;
		try {
			connection= getConnection();
			preparedStatement= connection.prepareStatement(sql);//鍚湁锛熷彿鍗犱綅绗︾殑sql
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);//涓猴紵鍙疯祴鍊�
			}
			return preparedStatement.executeUpdate()>0;
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(preparedStatement,connection);
		}
		return false;
	}
	
	/**
	 * 閲婃斁璧勬簮
	 *
	 * @author GaoHuanjie
	 */
	private void close(Statement statement,Connection connection) {
		try {
			if(statement!=null) {//鏈夊彲鑳界敱浜庡紓甯稿鑷磗tatement娌℃湁璧嬪�硷紝姣斿url鍑洪敊
				statement.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		try {
			if(connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
	}
	
	/**
	 * 閲婃斁璧勬簮
	 *
	 * @author GaoHuanjie
	 */
	private void close(ResultSet resultSet,Statement statement,Connection connection) {//閲嶈浇
		try {
			if(resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		close(statement,connection);
	}
}

IRowMapper.java:

package com.jd.tool.db;

import java.sql.ResultSet;

public interface IRowMapper {

	void rowMapper(ResultSet rs);
}

PropertiesTool.java:

package com.jd.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesTool {

	private static Properties properties = new Properties();
	
	static {
		InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");//灏哾b.properties鍙樹负javaIO娴佸璞�
		try {
			properties.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getValue(String key) {
		return properties.getProperty(key);
	}
}

MD5Tool.java:

package com.jd.tool;

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

public class MD5Tool {
	
	public static String encrypt(String password) {
		byte[] bytes = null;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(password.getBytes());//鍔犲瘑
			bytes = messageDigest.digest();//鑾峰緱鍔犲瘑缁撴灉
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String result = new BigInteger(1, bytes).toString(16);// 灏嗗姞瀵嗗悗鐨勬暟鎹浆鎹负16杩涘埗鏁板瓧
		// 鐢熸垚鏁板瓧鏈弧32浣嶏紝鍒欏墠闈㈣ˉ0
		for (int i = 0; i < 32 - result.length(); i++) {
			result = "0" + result;
		}
		return result;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值