JAVA Study Notes ()-classic three-tier architecture example, java Study Notes
UserDAO Interface
/** UserDAO interface */public interface UserDAO {// insert the User public void insert (user User); // delete the user public void delete (int id ); // update the User public void update (user User); // query all users public List <user> getAllUsers (); // query the public boolean checkUser (User user User) by User name or password; // query the public user getUserById (int id) by User number );}
UserDAOImpl implementation class
Import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. 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 (int id) {// TODO Auto-generated method stub} @ Override public void update (User user) {// TODO Auto-generated method stub} @ Override public List <User> getAllUsers () {// TODO Auto-generated method stub return null;} @ Override public boolean checkUser (User user) {boolean flag = 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 (); rs1_pstmt.exe cuteQuery (); if (rs. next () {flag = true ;}} catch (SQLException e) {e. printStackTrace ();} finally {DBUtil. closeAll (rs, pstmt, conn);} return flag;} @ Override public User getUserById (int id) {// TODO Auto-generated method stub return null ;}}
UserService Interface
/** UserService interface */public interface UserService {// The User registers public void register (user User); // the user logs on to public boolean login (User user );}
UserServiceImpl implementation class, call DAO to complete corresponding functions
/** 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 User) {ud. insert (user) ;}@ Override public boolean login (User user) {return ud. checkUser (user );}}
Database tools
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement;/** database tool class */public Class DBUtil {// obtain the database Connection public static Connection getConnection () {Connection conn = null; try {class. forName ("com. mysql. jdbc. driver "); conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", "123456");} catch (ClassNotFo UndException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace () ;}return conn ;}// 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 instance UserService us = new UserServiceImpl (); // login form component private JFrame frmLogin = new JFrame ("login"); private JPanel [] pnls = new JPanel [4]; // four rows of panel private JLabel lblWelcome = new JLabel ("welcome to the small supermarket management system! "); Private JLabel lblUsername = new JLabel (" username: "); private JTextField txtUsername = new JTextField (20); private JLabel lblPassword = new JLabel (" Password :"); private JPasswordField txtPassword = new JPasswordField (20); private JButton btnLogin = new JButton ("login"); private JButton btnExit = new JButton ("exit "); // initialize the public Login () {// initialize the four-row panel and add it to the form for (int I = 0; I <pnls. length; I ++) {pnls [I] = New JPanel (); pnls [I]. setOpaque (false); // set the transparent frmLogin on the panel. add (pnls [I]); // add the panel to the component} // initialize the JLabel and text box, and add lblWelcome to the mounted panel. setFont (new Font ("", Font. BOLD, 20); lblWelcome. setForeground (Color. red); pnls [0]. add (lblWelcome); pnls [1]. setLayout (new FlowLayout (FlowLayout. CENTER, 20, 0); pnls [1]. add (lblUsername); pnls [1]. add (txtUsername); pnls [2]. setLayout (new FlowLayout (FlowLayout. CENTER, 20, 0); p Nls [2]. add (lblPassword); pnls [2]. add (txtPassword); pnls [3]. setLayout (new FlowLayout (FlowLayout. CENTER, 20, 0); pnls [3]. add (btnLogin); pnls [3]. add (btnExit); // add listener btnLogin. addActionListener (this); btnExit. addActionListener (this); // set the background image of the form ImageIcon iconBg = new ImageIcon ("images/bg.jpg"); JLabel lblBg = new JLabel (iconBg); lblBg. setSize (iconBg. getIconWidth (), iconBg. getIconHeight (); JLayeredPane LayeredPane = frmLogin. getLayeredPane (); layeredPane. add (lblBg, new Integer (Integer. MIN_VALUE); JPanel contentPane = (JPanel) frmLogin. getContentPane (); contentPane. setOpaque (false); // initialize the form frmLogin. setLayout (new GridLayout (4, 1); frmLogin. setSize (400,300); frmLogin. setResizable (false); frmLogin. setLocationRelativeTo (null); frmLogin. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frmLogin. setVisibl E (true) ;}@ Override public void actionreceivmed (ActionEvent e) {String cmd = e. getActionCommand (); // obtain the text if (cmd. equals ("login") {String username = txtUsername. getText (); String password = new String (txtPassword. getPassword (); User user = new User (username, password); if (us. login (user) {frmLogin. dispose (); // hide the login form new JFrame (). setVisible (true);} else {JOptionPane. showMessageDialog (frmLogin, "User Name Or the password is incorrect! ") ;}} Else {int choice = JOptionPane. showConfirmDialog (frmLogin," Are you sure you want to exit? "," Prompt ", JOptionPane. YES_NO_OPTION, JOptionPane. QUESTION_MESSAGE); if (choice = 0) {// if yes, exit the program System. exit (0) ;}} public static void main (String [] args) {new Login ();}}
Database Design
create table user( id int primary key auto_increment, username varchar(20) not null, password varchar(20) not null);insert into user values (null,'admin','123');insert into user values (null,'tom','456');