Java Database Access Layer

Source: Internet
Author: User

The recent project needs to operate MySQL, the knowledge of the database as early as the undergraduate graduation that will be forgotten, the development of the past few years have not touched the operation of the database.

Take this opportunity to re-learn, the database access layer is a package of database operations, shielding the underlying data manipulation details, by using DAO to the database for the deletion and checking operations.

This article will take a small part of the project as an example of how to write a database access layer:

1. Entity class objects

1  Public classCheckinfo {2     PrivateInteger ID;3     PrivateString UserName;4     PrivateTimestamp checktime;5 6      PublicInteger getId () {7         returnID;8     }9 Ten      Public voidsetId (Integer id) { One          This. ID =ID; A     } -  -      PublicString GetUserName () { the         returnUserName; -     } -  -      Public voidsetusername (String userName) { +          This. UserName =UserName; -     } +  A      PublicTimestamp Getchecktime () { at         returnChecktime; -     } -  -      Public voidsetchecktime (Timestamp checktime) { -          This. Checktime =Checktime; -     } in  - @Override to      PublicString toString () { +         return"Checkinfo [id=" + ID + ", username=" +UserName -+ ", checktime=" + Checktime + "]"; the     } *  $}

2. Get the database connection tool class Connectionutil

1  Public classConnectionutil {2     3      PublicConnection getconnection () {4String username = "root";5String password = "123456";6String url= "Jdbc:mysql://localhost:3306/checkin";7String driver= "Com.mysql.jdbc.Driver";8         9         Try {Ten Class.forName (driver); One             returndrivermanager.getconnection (URL, username, password); A}Catch(ClassNotFoundException e) { - e.printstacktrace (); -}Catch(SQLException e) { the e.printstacktrace (); -         } -         return NULL; -     } +}

3. Data Access Layer Interface

1  Public Interface Checkinfodao {2     3      Public Boolean Save (Checkinfo checkinfo); 4      Public List<checkinfo> listcheckinfo (); 5     6 }

4. Data Access Layer Implementation

1  Public classCheckinfodaoimplImplementsCheckinfodao {2 3 @Override4      Public BooleanSave (Checkinfo checkinfo) {5         BooleanFlag =false;6Connectionutil Connutil =Newconnectionutil ();7Connection conn =NULL;8 9conn =connutil.getconnection ();Ten  OnePreparedStatement stmt =NULL; AString sql = "INSERT into Checkinfo values (name, time) VALUES (" -+ checkinfo.getusername () + "," +checkinfo.getchecktime () -+ ")"; the         Try { -stmt =conn.preparestatement (SQL); -Flag =Stmt.execute (); -}Catch(SQLException e) { + e.printstacktrace (); -         } +          A         returnFlag; at     } -  - @Override -      PublicList<checkinfo>Listcheckinfo () { -List<checkinfo> Checkinfos =NewArraylist<checkinfo>(); -          inConnectionutil Connutil =Newconnectionutil (); -Connection conn =NULL; to  +conn =connutil.getconnection (); -  thePreparedStatement stmt =NULL; *String sql = "SELECT * FROM Checkinfo"; $ResultSet ResultSet =NULL;Panax Notoginseng          -         Try { thestmt =conn.preparestatement (SQL); +ResultSet =stmt.executequery (); A              while(Resultset.next ()) { theCheckinfo ci =Newcheckinfo (); +Ci.setid (Resultset.getint (1)); -Ci.setusername (Resultset.getstring (2)); $Ci.setchecktime (Resultset.gettimestamp (3)); $ Checkinfos.add (CI); -             } -}Catch(SQLException e) { the e.printstacktrace (); -         }Wuyi          the         returnCheckinfos; -     } Wu  -}

The datetime type in MySQL corresponds to the Java timestamp type.

5. Test class

1  Public classTestCase {2 3      Public Static voidMain (string[] args) {4         5 testlistcheckinfo ();6     }7 8     Private Static voidTestlistcheckinfo () {9Checkinfodao Checkinfodao =NewCheckinfodaoimpl ();TenList<checkinfo> Checkinfos =checkinfodao.listcheckinfo (); One          A          for(Checkinfo checkinfo:checkinfos) { - System.out.println (checkinfo); -         } the     } -  -}

Database Build Table statement:

1 Create Table if  not existsCheckinfo (2Idint(Ten) not NULL Primary KeyAuto_increment,3Useridvarchar( +),4Timedatetime5);

Java Database Access Layer

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.