package com.shujia.day18.logindemo;
import com.shujia.day18.utils.MysqlTool;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
/*
登录注册java版:结合mysql数据库
*/
public class IOTest3 {
//TODO: 自己完善注册和修改的功能
private static Scanner sc = new Scanner(System.in);
public static void init() {
System.out.println("=============== ^_^ 欢迎进入魏一民婚恋介绍网站 ^_^ ==================");
System.out.println("1.登录 2.注册 3.修改 4.注销");
System.out.print("请输入您的操作编号:");
int choice = sc.nextInt();
switch (choice) {
case 1:
login();
break;
case 2:
regeist();
break;
case 3:
update();
break;
default:
System.out.println("您的输入有误!");
break;
}
}
public static void update() {
//查询所有的用户
ArrayList<User> allUsers = getAllUsers();
System.out.println("请输入您需要修改的用户名:");
String name = sc.next();
//查询用户是否存在
User user = getUserByName(name);
if(user!=null){
for(int i=0;i<allUsers.size();i++){
User u = allUsers.get(i);
if(u.getName().equals(name)){
allUsers.remove(u);
}
}
System.out.println("请选择您要修改的内容:1.姓名 2.密码");
int choice = sc.nextInt();
switch (choice){
case 1:
System.out.println("请输入新名称:");
String newName = sc.next();
user.setName(newName);
break;
case 2:
System.out.println("请输入新密码:");
String newPwd = sc.next();
user.setPassword(newPwd);
break;
default:
System.out.println("无效输入!");
break;
}
}else {
return;
}
//将修改后用户添加到集合中
allUsers.add(user);
BufferedWriter bw= null;
try {
//创建字符缓冲输出流
bw = new BufferedWriter(new FileWriter("src/com/shujia/day17/data/users.txt"));
for (User allUser : allUsers) {
bw.write(allUser.getName()+","+allUser.getPassword());
bw.newLine();
bw.flush();
}
System.out.println("用户修改成功!");
}catch (Exception e){
e.printStackTrace();
}finally {
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static User getUserByName(String name) {
BufferedReader br = null;
boolean flag = true;
User user = null;
try {
//创建字符缓冲输入流对象
//public FileReader(File file)
File file = new File("src/com/shujia/day17/data/users.txt");
//判断用户文件中是否用用户
if (file.length() > 0) {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
//魏一民,123456
String name1 = line.split(",")[0];
String password1 = line.split(",")[1];
//判断用户是否与登录的用户一样
if (name1.equals(name)) {
flag = false;
user = new User(name1,password1);
break;
}
}
if (flag) {
System.out.println("该用户未进行注册!");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return user;
}
public static void regeist() {
System.out.println("----------------- 欢迎注册魏一民婚恋介绍网站 --------------------");
ArrayList<User> allUsers = getAllUsers();
String password = null;
BufferedWriter bw = null;
System.out.print("请输入您的用户名:");
String name = sc.next();
for (User user : allUsers) {
if (user.getName().equals(name)) {
System.out.println("该用户名已被使用!");
return; // 方法已经结束了
}
}
System.out.println("请设置账户密码:");
password = sc.next();
try {
//将用户名和密码写到文件中
bw = new BufferedWriter(new FileWriter("src/com/shujia/day17/data/users.txt", true));
bw.newLine();
bw.write(name + "," + password);
bw.flush();
System.out.println("用户注册成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static ArrayList<User> getAllUsers() {
BufferedReader br = null;
//创建一个ArrList集合,用户存储读取到用户
ArrayList<User> users = new ArrayList<>(); // 新创建的集合中元素个数是0
try {
//创建字符缓冲输入流对象
//public FileReader(File file)
File file = new File("src/com/shujia/day17/data/users.txt");
//判断用户文件中是否用用户
if (file.length() > 0) {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
//魏一民,123456
String name1 = line.split(",")[0];
String password1 = line.split(",")[1];
//判断用户是否与登录的用户一样
users.add(new User(name1, password1));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return users;
}
public static void login() {
try {
Scanner sc2 = new Scanner(System.in);
System.out.print("请输入您的用户名:");
String name = sc2.nextLine();
System.out.print("请输入您的密码:");
String password = sc2.nextLine();
//去数据库中查询用户数据,若查到结果集是有数据的
// String url = "jdbc:mysql://192.168.136.100:3306/bigdata31?useUnicode=true&characterEncoding=utf-8&useSSL=false";
// String username2 = "root";
// String password2 = "123456";
//读取配置文件,获取键对应的值
//创建Properties类对象
Properties properties = new Properties();
//指定要读取的配置文件
properties.load(new BufferedReader(new FileReader("src/com/shujia/day18/confs/info.properties")));
//getProperty() 通过键获取对应的值
String url = properties.getProperty("url");
String username2 = properties.getProperty("user");
String password2 = properties.getProperty("password");
Connection conn = MysqlTool.getConnection(url, username2, password2);
System.out.println("成功获取与数据库的链接对象:"+conn);
/**
* 普通的Statement数据库操作对象,会存在sql注入的问题
* 原因是因为先进行sql拼接的,然后再做解析,会将sql中一些本来应该是值的内容解析成sql中的语法了
*
*/
// Statement state = conn.createStatement();
// //select * from users where username='小虎' and password='123456';
// //select * from users where username='小虎' and password='123 345'
// //select * from users where username='小虎' and password='qwew' or '1'='1'
// String sql = "select * from users where username='" + name + "' and password='" + password + "'";
// System.out.println(sql);
// ResultSet resultSet = state.executeQuery(sql);
/**
* 要想解决sql注入的问题,就不能使用普通的Statement数据库操作对象,改用预编译对象
*/
PreparedStatement prepareStatement = conn.prepareStatement("select * from users where username=? and password=?");
//传?的值
prepareStatement.setString(1,name);
prepareStatement.setString(2,password);
//执行sql语句
ResultSet resultSet = prepareStatement.executeQuery();
if(resultSet.next()){
System.out.println("登录成功!!");
}else {
System.out.println("登录失败!!");
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
init();
}
}