Read the server-side code DAO design summary written by experts similar to QQ chat, server-side dao

Source: Internet
Author: User

Read the server-side code DAO design summary written by experts similar to QQ chat, server-side dao

1. Data access layer DAO design (this project has been used as an example)

(1) define an interface to standardize the algorithm framework. (If the subclass and base classes have many common things, they should be designed as abstract classes)

Package com. way. chat. dao

Public interface UserDao {// return the User idpublic int register (User u); public ArrayList <User> login (User u); public ArrayList <User> refresh (int id ); public void logout (int id );}

(2) define a single-instance static factory method in the implementation class of this interface (to avoid repeated database operation objects) to provide database operation objects.




 

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) specific implementation class of this interface


Package com. way. chat. dao. impl;

Public class UserDaoImpl implements UserDao {@ Overridepublic 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.exe cuteUpdate (); if (res> 0) {PreparedStatement ps2 = con. prepareStatement (sql2); ResultSet rs = ps2.executeQuery (); if (rs. last () {id = rs. getInt ("_ id"); createFriendt Able (id); // after successful registration, create a table with the table name as the user id for storing the friend information return id; }}} catch (SQLException e) {e. printStackTrace ();} finally {DButil. close (con);} return Constants. REGISTER_FAIL ;}@ Overridepublic 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.exe cuteQuery (); if (rs. first () {setOnline (u. getId (); // update the table status to online ArrayList <User> refreshList = refresh (u. getId (); return refreshList;} catch (SQLException e) {// e. printStackTrace ();} finally {DButil. close (con);} return null;}/*** find yourself */public User findimethyl (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.exe cuteQuery (); 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;}/*** refresh friend list */public ArrayList <User> refresh (Int id) {ArrayList <User> list = new ArrayList <User> (); User me = findimethyl (id); list. add (me); // first add your own Connection con = DButil. connect (); String SQL = "select * from _? "; PreparedStatement ps; try {ps = con. prepareStatement (SQL); ps. setInt (1, id); ResultSet rs = ps.exe cuteQuery (); 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 (SQLE Xception e) {// e. printStackTrace ();} finally {DButil. close (con);} return null;}/*** set the status to online ** @ 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, id1_0000ps.exe cuteUpdate (); updateAllOn (id); // update the status of all tables to online} catch (SQLException e) {// e. printStackTrace ();} finally {DButil. close (con) ;}}/*** after successful registration, create a user table and save the user's friend ** @ 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.exe cuteUpdate (); System. out. println (res);} catch (SQLException e) {e. printStackTrace ();} finally {DButil. close (con) ;}@ Override/*** offline Update Status is offline */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, id1_0000ps.exe cuteUpdate (); updateAllOff (id); // System. out. println (res);} catch (SQLException e) {// e. printStackTrace ();} finally {DButil. close (con) ;}}/*** update the status of all user tables to offline ** @ 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, id1_0000ps.exe cuteUpdate () ;}} catch (SQLException e) {// e. printStackTrace ();} finally {DButil. close (con) ;}}/*** update all users to the online status ** @ 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, id1_0000ps.exe cuteUpdate () ;}} 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.exe cuteQuery (); 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 ("158342219@qq.com"); // 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) tools used by the database to load the driver and disable the connection:


Public class DButil {/*** connect to database ** @ return database Connection object */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. getProp Erty ("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 (ClassNotFoundExcept Ion e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();} return null;}/*** close database ** @ param conn * Incoming database Connection object */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 );//}}


Corresponding configuration file: DB. properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/qq?useUnicode=true&characterEncoding=utf-8username=rootpassword=admin





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.