Java Learning Note (53)-Classic three-tier architecture example

Source: Internet
Author: User

Userdao interface
/ * * Userdao interface * / Public InterfaceUserdao {//Insert User     Public void Insert(user user);//delete user     Public void Delete(intID);//Update users     Public void Update(user user);//Query All users     PublicList<user>getAllUsers();//Query users based on user name or password     PublicBooleanCheckUser(user user);//Query users by number     PublicUserGetuserbyid(intID);}
Userdaoimpl Implementation Class
ImportJava.sql.Connection;ImportJava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.SQLException;ImportJava.util.List;/ * * USERDAOIMPL Implementation class */ Public  class Userdaoimpl implements Userdao{Connection conn=NULL; PreparedStatement pstmt=NULL; ResultSet rs=NULL;@Override     Public void Insert(User user) {//TODO auto-generated method stub}@Override     Public void Delete(intID) {//TODO auto-generated method stub}@Override     Public void Update(User user) {//TODO auto-generated method stub}@Override     PublicList<user>getAllUsers() {//TODO auto-generated method stub        return NULL; }@Override     Public Boolean CheckUser(User user) {Booleanflag=false; String sql="SELECT * from user where username=?" and password=? ";Try{conn=dbutil.getconnection ();            Pstmt=conn.preparestatement (SQL); Pstmt.setstring (1, User.getusername ()); Pstmt.setstring (2, User.getpassword ()); Rs=pstmt.executequery ();if(Rs.next ()) {flag=true; }        }Catch(SQLException e)        {E.printstacktrace (); }finally{Dbutil.closeall (RS, PSTMT, conn); }returnFlag }@Override     PublicUserGetuserbyid(intID) {//TODO auto-generated method stub        return NULL; }}
UserService interface
/* * UserService接口 */publicinterface UserService {    //用户注册    publicvoidregister(User user);    //用户登陆    publiclogin(User user);}
Userserviceimpl implementation class, invoking DAO to complete the corresponding function
/* * Userserviceimpl implementation class, Call DAO to complete the corresponding function */ public   class  userserviceimpl  implements  userservice  { //Create Userdao instance  Userdao ud=new     Userdaoimpl ();     @Override  public  void  register  (User user)    {Ud.insert (user); }  @Override  public  boolean  login  (User user) {return<    /span> ud.checkuser (user); }}
Database Tools Classes
ImportJava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;ImportJava.sql.SQLException;ImportJava.sql.Statement;/ * Database Tools class */ Public  class dbutil {    //Get database connection     Public StaticConnectiongetconnection() {Connection conn=NULL;Try{Class.forName ("Com.mysql.jdbc.Driver"); Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test","Root","123456"); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Catch(SQLException e)        {E.printstacktrace (); }returnConn }//Close all resources     Public Static void CloseAll(ResultSet rs,statement stmt,connection conn) {Try{if(rs!=NULL) Rs.close ();if(stmt!=NULL) Stmt.close ();if(conn!=NULL) Conn.close (); }Catch(SQLException e)        {E.printstacktrace (); }    }}
Login Form
/ * * Login Form */ Public  class Login implements ActionListener {    //Define a UserService instanceUserService us=NewUserserviceimpl ();//Login Form Components    PrivateJFrame Frmlogin =NewJFrame ("Login");Privatejpanel[] Pnls =Newjpanel[4];//4 row panel    PrivateJLabel Lblwelcome =NewJLabel ("Welcome to the small supermarket management system!" ");PrivateJLabel Lblusername =NewJLabel ("User name:");PrivateJTextField txtUserName =NewJTextField ( -);PrivateJLabel Lblpassword =NewJLabel ("Password:");PrivateJPasswordField Txtpassword =NewJPasswordField ( -);PrivateJButton Btnlogin =NewJButton ("Login");PrivateJButton Btnexit =NewJButton ("Exit");//Initialize Components     Public Login() {//Initialize 4 rows panel and add to form         for(inti =0; i < pnls.length; i++) {Pnls[i] =NewJPanel (); Pnls[i].setopaque (false);//Set panel transparentFrmlogin.add (Pnls[i]);//Add a panel to the Assembly}//Initialize the JLabel and text boxes, and add the load panelLblwelcome.setfont (NewFont ("Blackbody", Font.Bold, -));        Lblwelcome.setforeground (color.red); pnls[0].add (Lblwelcome); pnls[1].setlayout (NewFlowLayout (Flowlayout.center, -,0)); pnls[1].add (Lblusername); pnls[1].add (txtUserName); pnls[2].setlayout (NewFlowLayout (Flowlayout.center, -,0)); pnls[2].add (Lblpassword); pnls[2].add (Txtpassword); pnls[3].setlayout (NewFlowLayout (Flowlayout.center, -,0)); pnls[3].add (Btnlogin); pnls[3].add (Btnexit);//Add monitoringBtnlogin.addactionlistener ( This); Btnexit.addactionlistener ( This);//Set the form background pictureImageIcon iconbg=NewImageIcon ("Images/bg.jpg"); JLabel lblbg=NewJLabel (ICONBG);        Lblbg.setsize (Iconbg.geticonwidth (), Iconbg.geticonheight ());        JLayeredPane Layeredpane=frmlogin.getlayeredpane (); Layeredpane.add (LBLBG,NewInteger (Integer.min_value));        JPanel contentpane= (JPanel) Frmlogin.getcontentpane (); Contentpane.setopaque (false);//Initialize formFrmlogin.setlayout (NewGridLayout (4,1)); Frmlogin.setsize ( -, -); Frmlogin.setresizable (false); Frmlogin.setlocationrelativeto (NULL);        Frmlogin.setdefaultcloseoperation (Jframe.exit_on_close); Frmlogin.setvisible (true); }@Override     Public void actionperformed(ActionEvent e) {String cmd = E.getactioncommand ();//Get text on the button        if(Cmd.equals ("Login") {String username=txtusername.gettext (); String password=NewString (Txtpassword.getpassword ()); User user=NewUser (Username,password);if(Us.login (user)) {Frmlogin.dispose ();//Hide Login Form                NewJFrame (). setvisible (true); }Else{Joptionpane.showmessagedialog (Frmlogin,"The user name or password is incorrect!" "); }        }Else{intChoice=joptionpane.showconfirmdialog (Frmlogin,"Are you sure you want to quit?" ","Hint", Joptionpane.yes_no_option, Joptionpane.question_message);if(choice==0){//If you select Yes, exit the programSystem.exit (0); }        }    } Public Static void Main(string[] args) {NewLogin (); }}
Database design
 Create table user(id int primary key auto_increment, user Name varchar() not  null, password varchar   null); Insert  into user values (null,' admin ',' 123 '); insert into  user values (null,' Tom ',' 456 ' );

Java Learning Note (53)-Classic three-tier architecture example

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.