From c8746f9e729bb92cf7de4fe03646c3799a8291e2 Mon Sep 17 00:00:00 2001 From: zhai_lw Date: Thu, 10 Jan 2019 21:16:34 +0800 Subject: [PATCH] =?UTF-8?q?1,=E5=90=8E=E7=AB=AF=E6=B3=A8=E5=86=8C=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/user/utils/AccountManagement.java | 34 +++++++++++++--------- src/init/Init.java | 4 +++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/core/user/utils/AccountManagement.java b/src/core/user/utils/AccountManagement.java index e9f934c..23b738d 100644 --- a/src/core/user/utils/AccountManagement.java +++ b/src/core/user/utils/AccountManagement.java @@ -10,6 +10,7 @@ import utils.Utils; import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; public interface AccountManagement { @@ -41,15 +42,24 @@ public interface AccountManagement { try { if(!rs.getString(2).equals(password)) throw new GExcptAccount("password wrong"); - return createUser(userType,rs); + List necessaryInfo = new ArrayList<>(); + for(int i=0;i<4;i++){ + necessaryInfo.add(rs.getString(i)); + } + return createUser(userType,necessaryInfo); } catch (SQLException e) { e.printStackTrace(); } return null; } - static User register(List necessaryInfo, String User){ - //todo - return null; + static User register(String userType, List necessaryInfo) throws GExcptSQL { + String sql = "INSERT INTO "+userType+" VALUES(\'"; + for(String info:necessaryInfo){ + sql+=info+"\', \'"; + } + sql =sql.substring(0,sql.length()-3)+")"; + DBManagement.update(sql); + return createUser(userType, necessaryInfo); } static void logout(User user){ //todo @@ -59,7 +69,7 @@ public interface AccountManagement { } static User getUser(String userType){ try { - return (Administrator)Class.forName("core.user."+ Utils.toUpperFristChar(userType)).getDeclaredConstructor().newInstance(); + return (User) Class.forName("core.user."+ Utils.toUpperFristChar(userType)).getDeclaredConstructor().newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { @@ -73,16 +83,12 @@ public interface AccountManagement { } return null; } - static User createUser(String userType, ResultSet rs) { + static User createUser(String userType, List necessaryInfo) { User user = getUser(userType); - try { - user.setId(rs.getString("id")); - user.setName(rs.getString("name")); - user.setE_mail_location(rs.getString("e_mail_location")); - user.setPhone_number(rs.getString("phone_number")); - } catch (SQLException e) { - e.printStackTrace(); - } + user.setId(necessaryInfo.get(0)); + user.setName(necessaryInfo.get(1)); + user.setE_mail_location(necessaryInfo.get(2)); + user.setPhone_number(necessaryInfo.get(3)); return user; } } diff --git a/src/init/Init.java b/src/init/Init.java index 0e284b7..8654112 100644 --- a/src/init/Init.java +++ b/src/init/Init.java @@ -1,6 +1,8 @@ package init; +import core.user.utils.AccountManagement; import dao.DBManagement; +import error.GExcptSQL; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; @@ -8,6 +10,8 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; @WebServlet(name = "Init") public class Init extends HttpServlet {