達人の編纂するQQのチャットのサービスエンドコードDAOを読んで総括を設計します。
1、データアクセス層DAOの設計(本プロジェクトの例)
(1)まず一つのインターフェースを定義し、アルゴリズムの枠組みを規範化する。(サブクラスと基質には公共のものが多い場合、抽象的なものに設計されるべきです)
package comp.way.chat.dao
package comp.way.chat.dao.impl;
(1)まず一つのインターフェースを定義し、アルゴリズムの枠組みを規範化する。(サブクラスと基質には公共のものが多い場合、抽象的なものに設計されるべきです)
package comp.way.chat.dao
public interface UserDao {
// id
public int register(User u);
public ArrayList<User> login(User u);
public ArrayList<User> refresh(int id);
public void logout(int id);
}
(2)このインターフェースの実装クラスにおいて、データベース操作オブジェクトを提供するために、一例の静的工場法を定義する。import com.way.chat.dao.UserDao;
public class UserDaoFactory {
private static <span style="color:#ff0000;">UserDa</span>o dao;
public static UserDao getInstance() {
if (dao == null) {
dao = new <span style="color:#ff0000;">UserDaoImpl()</span>;
}
return dao;
}
}
(3)当該インターフェースの具体的な実現類package comp.way.chat.dao.impl;
public class UserDaoImpl implements UserDao {
@Override
public int register(User u) {
int id;
Connection con = DButil.connect();
String sql1 = "insert into user(_name,_password,_email,_time) values(?,?,?,?)";
String sql2 = "select _id from user";
try {
PreparedStatement ps = con.prepareStatement(sql1);
ps.setString(1, u.getName());
ps.setString(2, u.getPassword());
ps.setString(3, u.getEmail());
ps.setString(4, MyDate.getDateCN());
int res = ps.executeUpdate();
if (res > 0) {
PreparedStatement ps2 = con.prepareStatement(sql2);
ResultSet rs = ps2.executeQuery();
if (rs.last()) {
id = rs.getInt("_id");
createFriendtable(id);// , id ,
return id;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButil.close(con);
}
return Constants.REGISTER_FAIL;
}
@Override
public ArrayList<User> login(User u) {
Connection con = DButil.connect();
String sql = "select * from user where _id=? and _password=?";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, u.getId());
ps.setString(2, u.getPassword());
ResultSet rs = ps.executeQuery();
if (rs.first()) {
setOnline(u.getId());//
ArrayList<User> refreshList = refresh(u.getId());
return refreshList;
}
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
return null;
}
/**
*
*/
public User findMe(int id) {
User me = new User();
Connection con = DButil.connect();
String sql = "select * from user where _id=?";
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.first()) {
me.setId(rs.getInt("_id"));
me.setEmail(rs.getString("_email"));
me.setName(rs.getString("_name"));
me.setImg(rs.getInt("_img"));
}
return me;
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
return null;
}
/**
*
*/
public ArrayList<User> refresh(int id) {
ArrayList<User> list = new ArrayList<User>();
User me = findMe(id);
list.add(me);//
Connection con = DButil.connect();
String sql = "select * from _? ";
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.first()) {
do {
User friend = new User();
friend.setId(rs.getInt("_qq"));
friend.setName(rs.getString("_name"));
friend.setIsOnline(rs.getInt("_isOnline"));
friend.setImg(rs.getInt("_img"));
friend.setGroup(rs.getInt("_group"));
list.add(friend);
} while (rs.next());
}
return list;
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
return null;
}
/**
*
*
* @param id
*/
public void setOnline(int id) {
Connection con = DButil.connect();
try {
String sql = "update user set _isOnline=1 where _id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
updateAllOn(id);//
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
* , ,
*
* @param id
*/
public void createFriendtable(int id) {
Connection con = DButil.connect();
try {
String sql = "create table _" + id
+ " (_id int auto_increment not null primary key,"
+ "_name varchar(20) not null,"
+ "_isOnline int(11) not null default 0,"
+ "_group int(11) not null default 0,"
+ "_qq int(11) not null default 0,"
+ "_img int(11) not null default 0)";
PreparedStatement ps = con.prepareStatement(sql);
int res = ps.executeUpdate();
System.out.println(res);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButil.close(con);
}
}
@Override
/**
*
*/
public void logout(int id) {
Connection con = DButil.connect();
try {
String sql = "update user set _isOnline=0 where _id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
updateAllOff(id);
// System.out.println(res);
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
*
*
* @param id
*/
public void updateAllOff(int id) {
Connection con = DButil.connect();
try {
String sql = "update _? set _isOnline=0 where _qq=?";
PreparedStatement ps = con.prepareStatement(sql);
for (int offId : getAllId()) {
ps.setInt(1, offId);
ps.setInt(2, id);
ps.executeUpdate();
}
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
*
*
* @param id
*/
public void updateAllOn(int id) {
Connection con = DButil.connect();
try {
String sql = "update _? set _isOnline=1 where _qq=?";
PreparedStatement ps = con.prepareStatement(sql);
for (int OnId : getAllId()) {
ps.setInt(1, OnId);
ps.setInt(2, id);
ps.executeUpdate();
}
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
public List<Integer> getAllId() {
Connection con = DButil.connect();
List<Integer> list = new ArrayList<Integer>();
try {
String sql = "select _id from user";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.first()) {
do {
int id = rs.getInt("_id");
list.add(id);
} while (rs.next());
}
// System.out.println(list);
return list;
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
return null;
}
public static void main(String[] args) {
User u = new User();
UserDaoImpl dao = new UserDaoImpl();
// u.setId(2016);
// u.setName("qq");
// u.setPassword("123");
// u.setEmail("[email protected]");
// System.out.println(dao.register(u));
// // System.out.println(dao.login(u));
// // dao.logout(2016);
// dao.setOnline(2016);
// // dao.getAllId();
List<User> list = dao.refresh(2016);
System.out.println(list);
}
}
(4)データベースは、プロパティファイルを利用して、接続をオフにするためのツールクラスをロード駆動します。public class DButil {
/**
*
*
* @return
*/
public static Connection connect() {
Properties pro = new Properties();
String driver = null;
String url = null;
String username = null;
String password = null;
try {
InputStream is = DButil.class.getClassLoader()
.getResourceAsStream("DB.properties");
// System.out.println(is.toString());
pro.load(is);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
// System.out.println(driver + ":" + url + ":" + username + ":"
// + password);
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username,
password);
return conn;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
*
*
* @param conn
*
*/
public static void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// public static void main(String[] args) {
// Connection con = new DButil().connect();
// System.out.println(con);
// }
}
対応配置ファイル:DB.propertiesdriver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/qq?useUnicode=true&characterEncoding=utf-8
username=root
password=admin