标签:
源代码下载链接:http://download.csdn.net/detail/sky453589103/9514686
我设计的时候是先从服务器端开始设计的,服务器端先给出接口,然后客户端针对接口编程。
在说服务器的代码之前先说说数据库的设计吧。
数据的设计是简单的,只有两个表:
create database SimpleChat; CREATE TABLE IF NOT EXISTS Account ( username varchar(50) NOT NULL, userpassword varchar(20) NOT NULL, sex ENUM('male', 'female') NOT NULL, status ENUM('on', 'off', 'leave', 'busy') NOT NULL, constraint A_PK primary key(username) ); CREATE TABLE IF NOT EXISTS Friend( user_1 varchar(50) NOT NULL, user_2 varchar(50) NOT NULL, constraint F_PK primary key (user_1, user_2), constraint F_FK1 foreign key (user_1) references Account(username), constraint F_FK2 foreign key (user_2) references Account(username) );
在源代码中,服务器端的项目名称是SimpleChatServer,
public ResponseMessage SignUp(String name, String password, Sex sex) { String sexValue = ""; String statusValue = "off"; if (Sex.MALE == sex) { sexValue = "male"; } else if (Sex.FEMALE == sex) { sexValue = "female"; } ResponseMessage response = new ResponseMessage(0, "Success"); try { if (username.matches("[; ,]") || password.matches("[; ,]")) { response.setCode(1); response.setDescription("username or password has illigal charactor!"); return response; } final String sql = "insert into Account values(\'" + name + "\', \'" + password + "\', \'" + sexValue + "\', \'" + statusValue + "\');"; Statement statement = conn.createStatement(); statement.executeUpdate(sql); } catch (SQLException e) { response.setCode(1); response.setDescription(e.getMessage()); } return response; }
final String sql = "insert into Account values(\'" + name + "\', \'" + password + "\', \'" + sexValue + "\', \'" + statusValue + "\');"; Statement statement = conn.createStatement(); statement.executeUpdate(sql);
public void AddUser(String name, Socket s) { userBuf.put(name, s); } public void RemoveUser(String name) { userBuf.remove(name); }其实这两个函数只是吧userBuf中的响应的键值对给删除了。不会访问数据库。写这两个方法的原因是为了以后如果要改变userBuf的表现形式,可以不用改变客户端的代码。
标签:
原文地址:http://blog.csdn.net/sky453589103/article/details/51351803