第二个项目注册

package com.jd.util.tool;

import java.util.Scanner;
import com.jd.util.tool.DBUtil;
import com.jd.util.tool.MD5Util;

public class Main {

	public static void main(String[] args) {
		System.out.println("*********************************");
		System.out.println("*\t\t\t\t*");
		System.out.println("*\t欢迎使用学生信息管理系统\t*");
		System.out.println("*\t\t\t\t*");
		System.out.println("*********************************");
		while (true) {
			menu();
		}
	}

	public static void menu() {
		System.out.println("1、注册");
		System.out.println("2、登录");
		System.out.println("请输入操作,以Enter键结束:");
		Scanner scanner = new Scanner(System.in);
		int option = Integer.parseInt(scanner.nextLine());
		switch (option) {
		case 1: {
			System.out.println("请输入用户名:");
			String userName = scanner.nextLine();
			if (userName.length() <= 6) {
				System.out.println("用户名不能小于6");
				return;
			}
			if (DBUtil.exist("select user_name from user_info where user_name=?", userName)) {
				System.out.println("用户已存在");
				return;
			}
			System.out.println("请输入密码:");
			String password = scanner.nextLine();
			if (password.length() <= 8) {
				System.out.println("密码必须》8");
				return;
			}
			System.out.println("请再次输入密码:");
			String repassword = scanner.nextLine();
			if (!repassword.equals(password)) {
				System.out.println("密码与确认密码不一致");
				return;
			}
			String sql = "insert into user_info(id,user_name,password)value(?,?,?)";
			if (DBUtil.update(sql, StringUtil.getId(), userName, password)) {
				System.out.println("注册成功");
				return;
			}
			System.out.println("注册失败");
			break;
		}
		case 2: {
			System.out.println("请输入用户名:");
			String userName = scanner.nextLine();
			System.out.println("请输入密码:");
			String password = scanner.nextLine();
			if (DBUtil.exist("select user_name,password from user_info where user_name=?and password=?", userName, password)) {
				System.out.println("登陆成功");
				return;
			}
			System.out.println("登录失败");
			break;
		}
		default:
			System.out.println("I'm Sorry,there is not the " + option + " option,please try again.");
		}
	}
}

又添加了一个类: 用于随机产生独立的id。

package com.jd.userinfo;

import java.util.UUID;

public class StringUtil {

	public static String getId() {
		return UUID.randomUUID().toString();
	}
}

工具类:

package com.jd.util.tool;

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 java.util.UUID;

	/**
	 * sql连接工具类
	 * @author mm
	 */
public class DBUtil {
	
	static {
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	}//仅用驱动一次就行

	/**
	 * 获取数据库连接
	 * @author mm
	 */
	public static Connection getConnection() {
		
		try {
			String url=PropertiesUtil.getValues("jdbc.url");
			String user_name=PropertiesUtil.getValues("jdbc.user_name");
			String password=PropertiesUtil.getValues("jdbc.password");
			
			return DriverManager.getConnection(url,user_name,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}//获取连接,鉴于地址可变但已编译成的class文件不可更改,外建了 个文件地址等便于更改,此时PropertiesUtil相当于一个工具调用此文件
	
	/**
	 * 判断数据是否存在
	 * @author mm
	 */
	public static boolean exist(String sql) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql, rowMapper);
		return rowMapper.state;
	}//电脑自动输入方法
	
	/**
	 * 判断数据是否存在
	 * @author mm
	 */
	public static boolean exist(String sql, Object... shuzu) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql, rowMapper, shuzu);
		return rowMapper.state;
	}//手动输入用此方法

	/**
	 * 修改数据
	 * @author mm
	 */
	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();
			statement = connection.createStatement();
			int result = statement.executeUpdate(sql);
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {//释放资源
			close(statement, connection);
		}
		return false;
	}
	
	/**
	 * 修改数据
	 * @author mm
	 */
	public static boolean update(String sql, Object... shuzu) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= shuzu.length; i++) {
				preparedStatement.setObject(i, shuzu[i - 1]);
			}
			int result = preparedStatement.executeUpdate();
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement, connection);
		}
		return false;
	}
	
	/**
	 * 查询数据
	 * @author mm
	 */
	public static void select(String sql, IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			statement = connection.createStatement();// 创建语句
			resultSet = statement.executeQuery(sql);// 执行语句
			rowMapper.rowMapper(resultSet);//&&&此处很多内幕,调用了实现类改写后的方法
		} catch (Exception e) {
			e.printStackTrace();
		} finally {// 释放资源
			close(statement, resultSet, connection);
		}
	}
	
	/**
	 * 查询数据
	 * @author mm
	 */
	public static void select(String sql, IRowMapper rowMapper, Object... shuzu) {

		Connection connection = null;
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= shuzu.length; i++) {
				preparedStatement.setObject(i, shuzu[i - 1]);
			}
			resultSet = preparedStatement.executeQuery();
			rowMapper.rowMapper(resultSet);//%%%此处很多内幕,调用了实现类改写后的方法
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement, resultSet, connection);
		}
	}
	
	/**
	 * 释放资源
	 * @author mm
	 */
	public static void close(Statement statement, Connection connection) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 释放资源
	 * @author mm
	 */
	public static void close(Statement statement, ResultSet resultSet, Connection connection) {
		close(statement, connection);
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 批量处理
	 * @author mm
	 */
		public static boolean batch(String... sqls) {
			Connection connection = null;
			Statement statement =null;
			try {
				connection = getConnection();
				connection.setAutoCommit(false);// 开启事务,等待提交
				statement = (Statement) connection.createStatement();
				for (String sql : sqls) {
					statement.addBatch(sql);
				}
				statement.executeBatch();// 执行语句。一错直接到catch,
				connection.commit();// 没错提交事务
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				if (connection != null) {
					try {
						connection.rollback();// 有错等待返回
					} catch (SQLException e1) {
						e1.printStackTrace();
					} finally {
						close(statement,connection);
					}
				}
			}
			return false;
		}
		
		/**
		 * 批量处理
		 * @author mm
		 */
			public static boolean batch(String sql,Object[]...params) {
				Connection connection = null;
				PreparedStatement preparestatement =null;
				try {
					connection = getConnection();
					connection.setAutoCommit(false);// 开启事务,等待提交
					preparestatement=connection.prepareStatement(sql);
					for (int i=0;i<params.length;i++) {
						for(int j=1;j<=params[i].length;j++) {
							preparestatement.addBatch();
						}
					}
					preparestatement.executeBatch();// 执行语句。一错直接到catch,
					connection.commit();// 没错提交事务
					return true;
				} catch (Exception e) {
					e.printStackTrace();
					if (connection != null) {
						try {
							connection.rollback();// 有错等待返回
						} catch (SQLException e1) {
							e1.printStackTrace();
						} finally {
							close(preparestatement,connection);
						}
					}
				}
				return false;
			}
			/**
			 * 获取随机产生的独立iD
			 * @return
			 */
			public static String getId() {
				return UUID.randomUUID().toString();
			}
}


 由于数据库的连接路径常变,这是外建的文件db.properties,便于更换地址

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

下面由  来引用文件:

package com.jd.util.tool;

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


public class PropertiesUtil {

	static Properties properties=new Properties();
	static {
		InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
		try {
			(properties).load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static String getValues(String key) {
		return properties.getProperty(key);
	}
}

再由万能工具类调用上面方法:

package com.jd.util.tool;

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 java.util.UUID;

	/**
	 * sql连接工具类
	 * @author mm
	 */
public class DBUtil {
	
	static {
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	}//仅用驱动一次就行

	/**
	 * 获取数据库连接
	 * @author mm
	 */
	public static Connection getConnection() {
		
		try {
			String url=PropertiesUtil.getValues("jdbc.url");
			String user_name=PropertiesUtil.getValues("jdbc.user_name");
			String password=PropertiesUtil.getValues("jdbc.password");
			
			return DriverManager.getConnection(url,user_name,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}//获取连接,鉴于地址可变但已编译成的class文件不可更改,外建了 个文件地址等便于更改,此时PropertiesUtil相当于一个工具调用此文件
	
	/**
	 * 判断数据是否存在
	 * @author mm
	 */
	public static boolean exist(String sql) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql, rowMapper);
		return rowMapper.state;
	}//电脑自动输入方法
	
	/**
	 * 判断数据是否存在
	 * @author mm
	 */
	public static boolean exist(String sql, Object... shuzu) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql, rowMapper, shuzu);
		return rowMapper.state;
	}//手动输入用此方法

	/**
	 * 修改数据
	 * @author mm
	 */
	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();
			statement = connection.createStatement();
			int result = statement.executeUpdate(sql);
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {//释放资源
			close(statement, connection);
		}
		return false;
	}
	
	/**
	 * 修改数据
	 * @author mm
	 */
	public static boolean update(String sql, Object... shuzu) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= shuzu.length; i++) {
				preparedStatement.setObject(i, shuzu[i - 1]);
			}
			int result = preparedStatement.executeUpdate();
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement, connection);
		}
		return false;
	}
	
	/**
	 * 查询数据
	 * @author mm
	 */
	public static void select(String sql, IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			statement = connection.createStatement();// 创建语句
			resultSet = statement.executeQuery(sql);// 执行语句
			rowMapper.rowMapper(resultSet);//&&&此处很多内幕,调用了实现类改写后的方法
		} catch (Exception e) {
			e.printStackTrace();
		} finally {// 释放资源
			close(statement, resultSet, connection);
		}
	}
	
	/**
	 * 查询数据
	 * @author mm
	 */
	public static void select(String sql, IRowMapper rowMapper, Object... shuzu) {

		Connection connection = null;
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= shuzu.length; i++) {
				preparedStatement.setObject(i, shuzu[i - 1]);
			}
			resultSet = preparedStatement.executeQuery();
			rowMapper.rowMapper(resultSet);//%%%此处很多内幕,调用了实现类改写后的方法
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement, resultSet, connection);
		}
	}
	
	/**
	 * 释放资源
	 * @author mm
	 */
	public static void close(Statement statement, Connection connection) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 释放资源
	 * @author mm
	 */
	public static void close(Statement statement, ResultSet resultSet, Connection connection) {
		close(statement, connection);
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 批量处理
	 * @author mm
	 */
		public static boolean batch(String... sqls) {
			Connection connection = null;
			Statement statement =null;
			try {
				connection = getConnection();
				connection.setAutoCommit(false);// 开启事务,等待提交
				statement = (Statement) connection.createStatement();
				for (String sql : sqls) {
					statement.addBatch(sql);
				}
				statement.executeBatch();// 执行语句。一错直接到catch,
				connection.commit();// 没错提交事务
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				if (connection != null) {
					try {
						connection.rollback();// 有错等待返回
					} catch (SQLException e1) {
						e1.printStackTrace();
					} finally {
						close(statement,connection);
					}
				}
			}
			return false;
		}
		
		/**
		 * 批量处理
		 * @author mm
		 */
			public static boolean batch(String sql,Object[]...params) {
				Connection connection = null;
				PreparedStatement preparestatement =null;
				try {
					connection = getConnection();
					connection.setAutoCommit(false);// 开启事务,等待提交
					preparestatement=connection.prepareStatement(sql);
					for (int i=0;i<params.length;i++) {
						for(int j=1;j<=params[i].length;j++) {
							preparestatement.addBatch();
						}
					}
					preparestatement.executeBatch();// 执行语句。一错直接到catch,
					connection.commit();// 没错提交事务
					return true;
				} catch (Exception e) {
					e.printStackTrace();
					if (connection != null) {
						try {
							connection.rollback();// 有错等待返回
						} catch (SQLException e1) {
							e1.printStackTrace();
						} finally {
							close(preparestatement,connection);
						}
					}
				}
				return false;
			}
			/**
			 * 获取随机产生的独立iD
			 * @return
			 */
			public static String getId() {
				return UUID.randomUUID().toString();
			}
}


工具类中有接口:

package com.jd.util.tool;

import java.sql.ResultSet;

public interface IRowMapper {
	void rowMapper(ResultSet resultSet);
}

接口的实现类:

package com.jd.util.tool;

import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class Row implements IRowMapper {
	
	public void rowMapper(ResultSet resultSet) {
		try {
			while (resultSet.next()) {
				String id = resultSet.getString("id");
				System.out.println(id);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值