DAO interface and Implementation class

Source: Internet
Author: User

All user actions are defined in the DAO interface, such as adding records, deleting records, and querying records.

1  PackageChapter13;2 ImportJava.util.*;3  Public InterfaceUserdao {4      Public voidInsert (user user)throwsException;5      Public voidUpdate (user user)throwsException;6      Public voidDeleteintUserIDthrowsException;7      PublicUser Querybyid (intUserIDthrowsException;8      PublicList<user> Queryall ()throwsException;9}

The DAO implementation class implements the DAO interface and implements all the methods defined in the interface.

1  PackageChapter13;2 3 ImportJava.util.*;4 ImportJava.sql.*;5 Importjava.sql.Date;6 7  Public classUserdaoimplImplementsUserdao {8 9 @OverrideTen      Public voidInsert (user user)throwsException { One         //TODO auto-generated Method Stub AString sql= "INSERT into User (Username,password) VALUES (?,?)"; -PreparedStatement preparedstatement=NULL; -DatabaseConnection dbc=NULL; the         Try{ -Dbc=Newdatabaseconnection (); -Preparedstatement=dbc.getconnection (). preparestatement (SQL); -Preparedstatement.setstring (1, User.getusername ()); +Preparedstatement.setstring (2, User.getpassword ()); - preparedstatement.executeupdate (); + preparedstatement.close (); A         } at         Catch(Exception e) { -             Throw NewException ("Operation Exception"); -}finally { - dbc.close (); -         } -  in     } -  to @Override +      Public voidUpdate (user user)throwsException { -         //TODO auto-generated Method Stub theString sql= "Update user set username=?,password=?" where userid=? "; *PreparedStatement preparedstatement=NULL; $DatabaseConnection dbc=NULL;Panax Notoginseng         Try{ -Dbc=Newdatabaseconnection (); thePreparedstatement=dbc.getconnection (). preparestatement (SQL); +Preparedstatement.setstring (1, User.getusername ()); APreparedstatement.setstring (2, User.getpassword ()); thePreparedstatement.setint (3, User.getuserid ()); + preparedstatement.executeupdate (); - preparedstatement.close (); $         } $         Catch(Exception e) { -             Throw NewException ("Operation Exception"); -}finally { the dbc.close (); -         }Wuyi     } the  - @Override Wu      Public voidDeleteintUserIDthrowsException { -         //TODO auto-generated Method Stub AboutString sql= "Delete from user where userid=?"; $PreparedStatement preparedstatement=NULL; -DatabaseConnection dbc=NULL; -         Try{ -Dbc=Newdatabaseconnection (); APreparedstatement=dbc.getconnection (). preparestatement (SQL); +Preparedstatement.setint (1, userid); the preparedstatement.executeupdate (); - preparedstatement.close (); $         } the         Catch(Exception e) { the             Throw NewException ("Operation Exception"); the}finally { the dbc.close (); -         } in     } the  the @Override About      PublicUser Querybyid (intUserIDthrowsException { the         //TODO auto-generated Method Stub theUser user=NULL; theString sql= "SELECT * from user where userid=?"; +PreparedStatement preparedstatement=NULL; -DatabaseConnection databaseconnection=NULL; the         Try{Bayidatabaseconnection=Newdatabaseconnection (); thePreparedstatement=databaseconnection.getconnection (). preparestatement (SQL); theResultSet rset=preparedstatement.executequery (); -             if(Rset.next ()) { -User=NewUser (); theUser.setuserid (Rset.getint (1)); theUser.setusername (Rset.getstring (2)); theUser.setpassword (Rset.getstring (3)); the             } - rset.close (); the preparedstatement.close (); the         } the         Catch(Exception e) {94             Throw NewException ("Operation Exception"); the}finally { the databaseconnection.close (); the         }98         returnuser; About     } - 101 @Override102      PublicList<user> Queryall ()throwsException {103         //TODO auto-generated Method Stub104List<user> all=NewArraylist<user>(); theString sql= "SELECT * from User";106PreparedStatement preparedstatement=NULL;107DatabaseConnection databaseconnection=NULL;108         Try{109databaseconnection=Newdatabaseconnection (); thePreparedstatement=databaseconnection.getconnection (). preparestatement (SQL);111ResultSet rset=preparedstatement.executequery (); the             if(Rset.next ()) {113User user=NewUser (); theUser=NewUser (); theUser.setuserid (Rset.getint (1)); theUser.setusername (Rset.getstring (2));117User.setpassword (Rset.getstring (3));118 all.add (user);119             } - rset.close ();121 preparedstatement.close ();122         }123         Catch(Exception e) {124             Throw NewException ("Operation Exception"); the}finally {126 databaseconnection.close ();127         } -         returnAll ;129     } the 131}

DAO interface and Implementation class

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.