📄 usermanager.java.svn-base
字号:
/* * @(#)UserManager.java * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package cn.edu.ynu.sei.dict.plugin.user;import cn.edu.ynu.sei.dict.core.exception.AlreadyExsistException;import cn.edu.ynu.sei.dict.core.exception.NotFoundUserException;import cn.edu.ynu.sei.dict.core.service.User;import cn.edu.ynu.sei.dict.plugin.db.connector.service.IDatabaseConnector;import cn.edu.ynu.sei.dict.plugin.user.service.IUserManager;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;/** * Implementation of <code>IuserManager</code>. * @author zy * @author 88250 * @version 1.1.0.1, Mar 14, 2008 * @see cn.edu.ynu.sei.dict.plugin.user.service.IUserManager */public class UserManager implements IUserManager { private IDatabaseConnector dbConnector; private Connection con = null; private Statement stm = null; private PreparedStatement preStm = null; private ResultSet rs = null; /** * Constructor with argument. * @param dbConnector database connector */ public UserManager(IDatabaseConnector dbConnector) { this.dbConnector = dbConnector; } @Override public List<String> findAllSubscriptions(String userName) { List<String> ns = new ArrayList<String>(); String sql = "select * from attention where user=?"; try { con = dbConnector.getDBConnection(); preStm = con.prepareStatement(sql); preStm.setString(1, userName); ResultSet rs = preStm.executeQuery(); while (rs.next()) { ns.add(rs.getString("attenuser")); } rs.close(); preStm.close(); con.close(); } catch (SQLException ex) { System.out.println("findAllSubscriptions"); } return ns; } @Override public void addSubscriptions(String userName, String attenName) throws AlreadyExsistException,NotFoundUserException { if(this.getUserByUserName(attenName) == null){ throw new NotFoundUserException("此用户不存在!"); } List<String> pusers = this.findAllSubscriptions(userName); for (String temp : pusers) { if (temp.equals(attenName)) { throw new AlreadyExsistException("此用户已经在您的列表里了!"); } } try { String sql = "insert into attention (user,attenuser) values(?,?)"; con = dbConnector.getDBConnection(); preStm = con.prepareStatement(sql); preStm.setString(1, userName); preStm.setString(2, attenName); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { System.out.println("addSubscriptions"); } } @Override public void deleteSubscriptions(String userName, String attenName) { try { String sql = "delete from attention where user=? and attenuser=?"; con = dbConnector.getDBConnection(); preStm = con.prepareStatement(sql); preStm.setString(1, userName); preStm.setString(2, attenName); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void persistUser(User user) { try { String sql = "insert into user (name,password,email) values(?,?,?)"; con = dbConnector.getDBConnection(); preStm = con.prepareStatement(sql); preStm.setString(1, user.name); preStm.setString(2, user.password); preStm.setString(3, user.email); preStm.executeUpdate(); preStm.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public List<String> getUserNames() { ArrayList<String> nameList = new ArrayList<String>(); String sql = "select name from user"; try { con = dbConnector.getDBConnection(); stm = con.createStatement(); rs = stm.executeQuery(sql); while (rs.next()) { nameList.add(rs.getString(1)); } stm.close(); con.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return nameList; } @Override public User getUserByUserName(String name) { User user = null; con = dbConnector.getDBConnection(); String sql = "select * from user where name=?"; try { preStm = con.prepareStatement(sql); preStm.setString(1, name); rs = preStm.executeQuery(); while (rs.next()) { String[] subs = null; if (rs.getString("subs") != null) { subs = rs.getString("subs").split("@"); } else { subs = new String[1]; } user = new User(rs.getString("name"), rs.getString("password"), rs.getString("email"), Arrays.asList(subs)); } preStm.close(); con.close(); rs.close(); } catch (SQLException ex) { System.out.println("getUserByUserName"); } return user; } @Override public void updateUserPersonalInfo(User user) { con = dbConnector.getDBConnection(); String sql = "update user set pssword=? where name=? and email=?"; try { preStm = con.prepareStatement(sql); preStm.setString(2, user.name); preStm.setString(1, user.password); preStm.setString(3, user.email); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } @Override public List<User> findAll() { List<User> userList = new ArrayList<User>(); User user = null; con = dbConnector.getDBConnection(); String sql = "select * from user"; try { stm = con.createStatement(); rs = stm.executeQuery(sql); while (rs.next()) { String[] subs = null; if (rs.getString("subs") != null) { subs = rs.getString("subs").split("@"); } else { subs = new String[1]; } user = new User(rs.getString("name"), rs.getString("password"), rs.getString("email"), Arrays.asList(subs)); userList.add(user); } stm.close(); con.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return userList; } @Override public void deleteUserByName(User user) { con = dbConnector.getDBConnection(); String sql = "delete from user where name=?"; try { preStm = con.prepareStatement(sql); preStm.setString(1, user.name); preStm.executeUpdate(); preStm.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -