长大Tips的第二步
由于期末将至的缘故,组员们对于这次项目都开始表现出了懈怠的情绪,故而这一次并没有完成许多实质性的任务,相较于上一次,此次增添了登陆以及注册的功能,说来惭愧,虽然已经学习了数据库编程,可惜自己学艺不精并没有能将用户的信息与数据库相衔接,所以就简单的使用了文件来存储这些用户信息。
用户注册部分
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7 package cn.itcast.pojo; 8 9 /** 10 * 11 * @author Administrator 12 */ 13 public class User { 14 private String username; 15 private String password; 16 17 public User(){} 18 19 /** 20 * @return the username 21 */ 22 public String getUsername() { 23 return username; 24 } 25 26 /** 27 * @param username the username to set 28 */ 29 public void setUsername(String username) { 30 this.username = username; 31 } 32 33 /** 34 * @return the password 35 */ 36 public String getPassword() { 37 return password; 38 } 39 40 /** 41 * @param password the password to set 42 */ 43 public void setPassword(String password) { 44 this.password = password; 45 } 46 47 48 }
利用文件存储用户信息
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package cn.itcast.dao.impl; 7 8 import cn.itcast.dao.UserDao; 9 import cn.itcast.pojo.User; 10 import java.io.BufferedReader; 11 import java.io.BufferedWriter; 12 import java.io.File; 13 import java.io.FileReader; 14 import java.io.FileWriter; 15 import java.io.IOException; 16 /** 17 * 18 * @author Administrator 19 */ 20 public class UserDaoImpl implements UserDao { 21 22 //定义文件 23 private static File file = new File("user.txt"); 24 25 //类加载的时候就把文件创建 26 static { 27 try { 28 file.createNewFile(); 29 } catch (IOException ex) { 30 ex.printStackTrace(); 31 } 32 } 33 34 @Override 35 public boolean login(String username, String password) { 36 boolean flag = false; 37 38 BufferedReader br = null; 39 try { 40 br = new BufferedReader(new FileReader(file)); 41 String line = null; 42 while ((line = br.readLine()) != null) { 43 String[] datas = line.split("="); 44 if (datas[0].equals(username) && datas[1].equals(password)) { 45 flag = true; 46 break; 47 } 48 } 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } finally { 52 try { 53 br.close(); 54 } catch (IOException ex) { 55 ex.printStackTrace(); 56 } 57 } 58 59 return flag; 60 } 61 62 @Override 63 public void regist(User user) { 64 BufferedWriter bw = null; 65 try { 66 bw = new BufferedWriter(new FileWriter(file, true)); 67 bw.write(user.getUsername() + "=" + user.getPassword()); 68 bw.newLine(); 69 bw.flush(); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } finally { 73 try { 74 bw.close(); 75 } catch (IOException ex) { 76 ex.printStackTrace(); 77 } 78 } 79 } 80 }
界面展示
登陆界面
注册界面
以上即为本次的项目进度……
持续更新中……