comparison, application and summary of single-case models

Source: Internet
Author: User

Related Excellent article: http://blog.csdn.net/beyond0525/article/details/22794221

http://www.360doc.com/content/11/0810/12/1542811_139352888.shtml

http://zz563143188.iteye.com/blog/1847029

Http://www.blogjava.net/ITdavid/archive/2008/01/22/176939.html

Http://www.cnblogs.com/yidianfeng/archive/2011/11/07/2239246.html

http://spiritfrog.iteye.com/blog/214986


Code Change comparison:

Singleton mode public class DAO {private static dao DAO = null;private Context context;private dao (Context contex) {This.context = Contex;} public static DAO getinstance (context context) {if (dao = = null) {DAO = new dao (Context.getapplicationcontext ());} return DAO;} Connection Database public Sqlitedatabase getconnection () {sqlitedatabase sqlitedatabase = null;try {sqlitedatabase = new DatabaseHel Per (context, databasehelper.dbname). Getreadabledatabase ();} catch (Exception e) {e.printstacktrace ();} return sqlitedatabase;} public static void CreateTable (Sqlitedatabase sqlitedatabase) {sqlitedatabase.execsql ("CREATE TABLE IF not EXISTS Priasetable (ID integer primary key autoincrement,clickid varchar), "+" clicktype varchar (+), Clickcount Integer, Nativeclick INTEGER) ");} public static void Createrecordtable (Sqlitedatabase sqlitedatabase) {sqlitedatabase.execsql ("CREATE TABLE IF not EXISTS Playrecordtable (ID integer PRIMARY key autoincrement,playid varchar (+), "+" playtype varchar (+), path varchar (100), ADPath VARchar (+), APIC varchar (+), Shareurl varchar (+), Title varchar (+), time varchar (100)); /** * Create upload server table uploadtable * mid-type STREAM-FTP file address on server uid-upload user ID * */public static void creatuploadtable (Sqlitedatabase s Qlitedatabase) {sqlitedatabase.execsql ("CREATE TABLE IF not EXISTS uploadtable (ID integer PRIMARY key autoincrement" + ", UID varchar (+), mid varchar (+), Title varchar (+), description varchar (255) "+", stream varchar (+), Time varchar ( 100)) ");} @SuppressLint ("SimpleDateFormat") public synchronized void insertuploadtable (Ftploadinfo ftploadinfo) { Sqlitedatabase database = getconnection (); try {String sql = "INSERT INTO uploadtable (" + "UID, Mid, title, description, str Eam,time) VALUES (?,?,?,?,?,?) ";D Ate date = new Date (); SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (); String time = Simpledateformat.format (date); object[] info = {Ftploadinfo.getuid (), Ftploadinfo.getmid (), Ftploadinfo.getname (), Ftploadinfo.getdesc (), Ftploadinfo.getstream (), Time};d atabase.execsql (sql, info);}catch (Exception e) {e.printstacktrace ();} finally {if (null! = database) {database.close ();}}} 

Post-Change Code:

Singleton mode 5 mode, recommended static internal use of the public class Dao {//context used to connect the database private static context context;//database library Manager private static Databasehelper mdatebasehelper;//Database private static sqlitedatabase mdatabase;/** * Inner class implementation Singleton mode * Deferred load, reduced memory overhead * Method Singleto N has a static inner class Singletonholder, the inner class does not load when it is loaded externally, and is not loaded until the call getinstance. * In addition, the Singletonholder class uses private adornments to ensure that external classes are inaccessible. * Pros: Lazy loading, thread safety (mutually exclusive during class loading in Java), also reduced memory consumption */private static class Daoholder{private static DAO DAO = new Dao ();} /** * Private Constructor * Deferred initialization */Private Dao () {//Lazy initialization Global context, initializing connection database eliminates each call method to connect server Mdatebasehelper = new Databasehelper (CO ntext, databasehelper.dbname); mdatabase = Mdatebasehelper.getwritabledatabase ();} public static Dao getinstance (context context) {Dao.context = Context;return Daoholder.dao;} Connect and open the database (open database in read-write mode) public void getconnection () {sqlitedatabase sqlitedatabase = null;try {mdatebasehelper = new Dat Abasehelper (context, databasehelper.dbname);} catch (Exception e) {e.printstacktrace ();}} Connect and open the database (open the database in read-write mode) public static VOID open () {mdatabase = Mdatebasehelper.getreadabledatabase ();} public static void CreateTable () {if (!mdatabase.isopen () ) {open ();} Mdatabase.execsql ("CREATE TABLE IF not EXISTS priasetable (ID integer primary key autoincrement,clickid varchar)," + "C Licktype varchar (+), Clickcount integer,nativeclick INTEGER); Mdatabase.close (); public static void Createrecordtable () {if (!mdatabase.isopen ()) {open ()}; Mdatabase.execsql ("CREATE TABLE IF not EXISTS playrecordtable (ID integer PRIMARY key autoincrement,playid varchar (100)," + "Playtype varchar", path varchar (+), ADPath varchar (+), APIC varchar (+), Shareurl varchar (+), Title varchar ( (+), time varchar (+)); Mdatabase.close (); public synchronized int Ishasinfors (string clickid, String clicktype) {open (); int id =-1; Cursor cursor = null;try {cursor = mdatabase.query ("priasetable", NULL, "Clickid=? And clicktype=? ", new string[] {clickid,clicktype}, NULL, NULL, NULL), if (Cursor.movetofirst ()) {id = cursor.getint (0);} } catch (Exception e){E.printstacktrace ();} finally {if (null! = Mdatabase) {mdatabase.close ();} if (null! = cursor) {cursor.close ();}} return ID;}


comparison, application and summary of single-case models

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.