今天確定了組內第一階段的任務是基本完成sos和幫扶兩個核心功能以及登入註冊等常規功能
完成登入和註冊的基本頁面以及資料庫的設計
package com.example.helppeople.entity; public class Student { private String id; private String name; private String phone; private String password; public Student() { } public Student(String id, String name, String phone, String password) { this.id = id; this.name = name; this.phone = phone; this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.example.helppeople.dao; import android.util.Log; import com.example.helppeople.entity.Student; import com.example.helppeople.utils.JDBCUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashMap; public class StudentDao { private static final String TAG = "mydb-StudentDao"; private static String currentLoggedInUserId; public int login(String id, String password) { HashMap<String, Object> map = new HashMap<>(); Connection connection = JDBCUtils.getConn(); int msg = 0; try { String sql = "select * from student where name = ?"; if (connection != null) { PreparedStatement ps = connection.prepareStatement(sql); if (ps != null) { Log.e(TAG, "賬號:" + id); ps.setString(1, id); ResultSet rs = ps.executeQuery(); int count = rs.getMetaData().getColumnCount(); while (rs.next()) { for (int i = 1; i <= count; i++) { String field = rs.getMetaData().getColumnName(i); map.put(field, rs.getString(field)); } } connection.close(); ps.close(); if (map.size() != 0) { StringBuilder s = new StringBuilder(); for (String key : map.keySet()) { if (key.equals("password")) { if (password.equals(map.get(key))) { msg = 1; currentLoggedInUserId = id; } else msg = 2; break; } } } else { Log.e(TAG, "查詢結果為空"); msg = 3; } } else { msg = 0; } } else { msg = 0; } } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "異常login:" + e.getMessage()); msg = 0; } return msg; } public boolean register(Student student) { HashMap<String, Object> map = new HashMap<>(); Connection connection = JDBCUtils.getConn(); try { String sql = "insert into student(id, name, phone,password) values (?,?,?,?)"; if (connection != null) { PreparedStatement ps = connection.prepareStatement(sql); if (ps != null) { ps.setString(1, student.getId()); ps.setString(2, student.getName()); ps.setString(3, student.getPhone()); ps.setString(4, student.getPassword()); int rs = ps.executeUpdate(); if (rs > 0) return true; else return false; } else { return false; } } else { return false; } } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "異常register:" + e.getMessage()); return false; } } public Student findStudent(String studentId) { Connection connection = JDBCUtils.getConn(); Student student = null; try { String sql = "select * from student where id = ?"; if (connection != null) { PreparedStatement ps = connection.prepareStatement(sql); if (ps != null) { ps.setString(1, studentId); ResultSet rs = ps.executeQuery(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); String phone = rs.getString(3); String password = rs.getString(4); student = new Student(id, name, phone, password); } } } } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "異常findUser:" + e.getMessage()); return null; } return student; } }