MD5加密

 项目结构如下:

MD5加密代码如下: 

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;
	}
}

 

在实际登录操作中:

package com.jd.test;

import java.util.Scanner;
import java.util.UUID;

import com.jd.tool.MD5Tool;
import com.jd.tool.db.DBLink;

public class Main {

	public static void main(String[] args) {
		System.out.println("*********************************");
		System.out.println("*\t\t\t\t*");
		System.out.println("*\t欢迎使用注册登录系统\t\t*");
		System.out.println("*\t\t\t\t*");
		System.out.println("*********************************");
		while (true) {
			menu();
		}
	}

	static void menu() {
		DBLink db = new DBLink();
		System.out.println("1、注册");//用户名  密码  确认密码
		System.out.println("2、登录");//用户名和密码
		System.out.println("3、退出");//System.exit(0);
		System.out.println("请输入操作,以Enter键结束:");
		Scanner scanner = new Scanner(System.in);
		int option  = scanner.nextInt();
		switch (option) {
			case 1:{
				System.out.println("请输入用户名");
				String user_name = scanner.next();
				String sql = "select id from user_info where user_name = ?";
				if(db.exist(sql,user_name)) {
					System.out.println("用户名已存在,操作中止!");
					return;
				}
				System.out.println("请输入用密码");
				String password = scanner.next();
				System.out.println("请再次输入密码");
				String repassword = scanner.next();
				
				if(!password.equals(repassword)) {
					System.out.println("两次输入密码不一致,操作中止");
					return;
				}
				String id = UUID.randomUUID().toString();
				password = MD5Tool.encrypt(password);//在注册时为防止管理员可以直接在后台看到密码,采用MD5加密,将所有的输入密码转换为32位的代码。
				sql = "insert into user_info (id,user_name,password) value ('"+id+"',?,?)";
				if(db.update(sql,user_name,password)) {
					System.out.println("注册成功");
					return;
				}
				System.out.println("注册失败");
				break;
				
			}
			case 2:
				System.out.println("请输入用户名");
				String user_name = scanner.next();
				System.out.println("请输入用密码");
				String password = scanner.next();
				password = MD5Tool.encrypt(password);//在进行登陆操作时,由于数据库中存入的是加密后的32位代码,因此需要在将输入的密码转化为32位代码与数据库中的信息进行比对。成功即可登录。
				String sql = "select id from user_info where user_name = ? and password = ?";
				if(db.exist(sql,user_name,password)) {
					System.out.println("登录成功");
					break;
				}
				System.out.println("登录失败");
				break;
			case 3:
				System.out.println("退出登录");
				System.exit(0);
				break;
			default:
				System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
		}
	}
}

加密解析如上。 

DBLink代码:

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) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类对象
		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接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法  多态
		} 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接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法  多态
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//释放资源
			close(resultSet,preparedStatement,connection);
		}
	}

	/**
	 * 修改(insert、update和delete)数据
	 *
	 * @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语句,返回受影响的行数,仅限于数据insert、update和delete
			return result>0;//处理结果
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//即便有异常也会执行代码
			close(statement,connection);
		}
		return false;
	}
	
	/**
	 * 修改(insert、update和delete)数据
	 *
	 * @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) {//有可能由于异常导致statement没有赋值,比如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);
	}
}

Properties代码:

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");//将db.properties变为javaIO流对象
		try {
			properties.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getValue(String key) {
		return properties.getProperty(key);
	}
}

IRowMapper代码:

package com.jd.tool.db;
import java.sql.ResultSet;

public interface IRowMapper {

	void rowMapper(ResultSet rs);
}

db.properties:

db.username = root
db.password = root
db.url =jdbc:mysql://127.0.0.1:3306/test

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值