注册于登录

DBUtil

package day_8_3_1;

import java.io.IOException;
import java.util.Properties;
/**
 * 
 * 只加载一次
 * @author 啊超
 * @Date 2021年8月3日
 */
public class PropertiesUtil {
	private static Properties properties = null;

	public static Properties getProperties() {
		if (properties == null) {
			properties = new Properties();
			// DBUtil.class : 获取运行时类
			// getClassLoader : 获取类加载器
			// getResourceAsStream : 把对应的资源转换为流对象
			// 直接写名字 默认定义为src下
			try {
				properties.load(DBUtil.class.getClassLoader()
						.getResourceAsStream("jdbc.properties"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return properties;
	}
}
package day_8_3_1;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
		public static Connection getConnection() throws SQLException,
				ClassNotFoundException, IOException {
			Connection conn = null;
			Properties properties = PropertiesUtil.getProperties();
			// 1 加载驱动/驱动注册
			Class.forName(properties.getProperty("driver"));
			// 2 链接对象 , 所有的包 全都是java.sql的
			conn = DriverManager.getConnection(properties.getProperty("url"),
					properties.getProperty("username"),
					properties.getProperty("password"));
			return conn;
		}

		public static void close(AutoCloseable obj) {
			// 6 关闭资源
			if (obj != null) {
				try {
					obj.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root

Client

package day_8_3_1;

import java.util.Scanner;





/**
 * 
 * 用户端
 * @author 啊超
 * @Date 2021年8月4日
 */
public class Client {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		while(true){
			System.out.println("请输入 1登录  2注册 ");
			int no=scanner.nextInt();
			if(no==1){
				//登录
				System.out.println("请输入用户名");
				String username =scanner.next();
				System.out.println("请输入密码");
				String password=scanner.next();
				Seriver.login(username,password);
			}else if(no==2){
				System.out.println("请输入用户名");
				String username=scanner.next();
				System.out.println("请输入密码");
				String password=scanner.next();
				System.out.println("请确认密码");
				String newpassword=scanner.next();
				while(!password.equals(newpassword)){
					System.out.println("请输入密码");
					 password=scanner.next();
					System.out.println("请确认密码");
					newpassword=scanner.next();
				}
				Seriver.register(username,password);
			}else{
				System.out.println("没有该功能");
			}	
		}
	}
}

Seriver

package day_8_3_1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import day_8_3_1.DBUtil;

public class Seriver {
	public static void login(String username,String password){
		//提供方法
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		//查询数据库
		try{
			connection=DBUtil.getConnection();
			preparedStatement=connection.prepareStatement("select*from user where username=?");
			preparedStatement.setString(1,username);
			resultSet=preparedStatement.executeQuery();
			if(resultSet.next()){
				//到这里是用户名正确,该查看密码了
				String oldPassword =resultSet.getString("password");
				if(oldPassword.equals(password)){
					System.out.println("登陆成功");
				}else{
					System.out.println("密码错误");
				}
				
				
			}else{
				System.out.println("用户名不存在");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtil.close(resultSet);
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
		
	}
	public static void register(String username,String password){
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection
					.prepareStatement("select * from user where username = ?");
			preparedStatement.setString(1, username);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				System.err.println("用户名已存在");
			} else {
				preparedStatement.close();
				preparedStatement=connection.prepareStatement("insert into user (username,password) values (?,?)");
				preparedStatement.setString(1, username);
				preparedStatement.setString(2, password);
				preparedStatement.executeUpdate();
				System.out.println("注册成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(resultSet);
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
	}	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值