The learning process of Java (II.)

Source: Internet
Author: User

Then the evolutionary history of the previous login module takes you back to the Java learning Process (a) continue to speak down

Before we go to implement the login function, are thinking to complete this function, directly in the actual business of the class to start writing specific code step-by-step implementation, that is, process-oriented programming.

So now we stand in the design of the angle to achieve how to do it? In fact, when the project development, requirements Planning and database design, we began to write code, often only to write some interfaces out, the interface only some of the various services corresponding to the empty method, no specific implementation, when the architect interface design, It's already been figured out. After the function is implemented and called by the interface is definitely achievable, and so on the interface design of each function is good, the rest is the programmer to implement and invoke these interfaces, as in the blanks. In the interface design only with the form of care method return value type, you can write the corresponding interface document, describe the function of each interface, the meaning of the parameters of each method in the interface and the meaning of the return value, so as to better facilitate team development. In fact, this is like making a house, making a house is the first to do the design, and then build the framework, the rest is the code brick.

When we use the function (method), we need to understand a concept. One-on-one (it can be called a request and response), especially when making HTTP requests. In a function of a non-void return type, it is necessary to retrun a return value, while calling the function to pass the argument to the parameter, this process can be regarded as "go", in the function body class do a lot of logic and business processing, after processing, return a result back, this is "back".

For example, to do a web landing function, the first is from the HTML input box to the user name and password information (actual parameters) to the server, the service side there is a checklogin function to receive the front end passed the argument, and then to query the database, Returns a result after processing the result tells the front end whether to log on successfully, the front end can be based on this return value UI interface prompt and interface jump and so on. In the division of business involved in the time, the previous user name and password may have to pass a few times between functions, but all are back again. Like the web design in the Ajax and Android network request Retorfit are embodied in the particularly obvious, we fully understand the principle of a go back, it can be more convenient for us to design the function module.

Well, here's what we're going to show through the code.

We create a new Java project, and then at this point we are not anxious to write code, the first to do a subcontracting (package) work, the equivalent of an organizational structure, so that the code structure clearer, and easier to read and maintain.

DAO This package is used to store our interface, Daoimpl below is used to store our interface implementation class, main as the main entrance of the project, to store the implementation of the specific business, Pojo used to store the data model class (and so on specific explanation pojo role), Utils is used to store tool classes (put the dbhelper we have written in the previous chapter under this package).

POJO (Plain old Java Objects) A simple Java object, which is actually a common JavaBeans, some of the classes of properties and their getter setter methods, can sometimes be used as value object or DTO (Data Transform object) to use. Of course, if you have a simple arithmetic attribute is also possible, but do not allow business methods, can be seen as a table in the database is mapped Java objects.

We'll create a new user class under the Pojo package.

/** * */package com.xdw.pojo;/** * @author Xiadewang * January 14, 2018 */public class User {private int id;    Private String username;    private String password; /** * @param ID * @param username * @param password */public User (int ID, string username, String Pass        Word) {super ();        This.id = ID;        This.username = Username;    This.password = password;    }/** * @return the ID */public int getId () {return id;    }/** * @param id The ID to set */public void setId (int id) {this.id = ID;    }/** * @return the username * */public String GetUserName () {return username; }/** * @param username the username to set */public void Setusername (String username) {This.userna    me = Username;    }/** * @return the password * */public String GetPassword () {return password; }/** * @param password the password to set */public VOID SetPassword (String password) {this.password = password; }  }



Below we are programming for interfaces, for example, our business now has the following:

1. Login

2. Registration

3, according to the ID query user information

4, according to username query user information

5. Modify User Password

At this time, if it is a process-oriented design, we will first go to complete the login function, and then complete the registration function, and then continue the following 345 of the business.

For the interface design, we first create a new interface under the DAO Userdao, the specific code is as follows

/** *  */package com.xdw.dao;import com.xdw.pojo.user;/** * @author Xiadewang * January 14, 2018 */public interface Userdao {    /**     * Pass user name and password to verify login     * @param username     * @param password     * @return    */Public Boolean Checklogin (String username,string password);        /**     * Pass the User object to verify the login     * @param user     * @return *     /public    user checklogin (user user);        /**     * Pass the user object in, return value 1 for registration successful, 0 for registration failure     * @param user     * @return */public    int register (user user );        /**     * Pass the ID parameter of type int, the return value is User Object     * @param ID     * @return *     /public    User Getuserbyid (int id);        /**     * Passes the user name of the string type, the return value is User Object     * @param username     * @return *     /public    user Getuserbyusername ( String username);        /**     * Pass the user object to modify the password, and the password to be modified, the return value 1 means that the modification succeeded, 0 for the modification failed     * @param user     * @param password     * @return     * /Public    int Modifypassword (User user,string password);}

Because the function here is very simple, directly to the interface description document written in the comments, this time the design work is over, the following to change a programmer to complete the program's full functionality.

Write an interface implementation class Userdaoimpl below the Daoimpl package, as follows

/** * */package com.xdw.daoimpl;import java.sql.resultset;import java.sql.sqlexception;import Com.xdw.dao.UserDao; Import Com.xdw.pojo.user;import com.xdw.utils.dbhelper;/** * @author Xiadewang * January 14, 2018 */public class Userdaoimpl     Implements Userdao {/* (non-javadoc) * @see com.xdw.dao.userdao#checklogin (java.lang.String, java.lang.String) */@Override public boolean Checklogin (string username, string password) {//TODO auto-generated method Stu    b return false; }/* (non-javadoc) * @see com.xdw.dao.userdao#checklogin (com.xdw.pojo.User) */@Override public User Chec    Klogin (user user) {//TODO auto-generated method stub return null; }/* (non-javadoc) * @see com.xdw.dao.userdao#register (com.xdw.pojo.User) */@Override public int Registe    R (user user) {//TODO auto-generated method stub return 0; }/* (non-javadoc) * @see Com.xdw.dao.userdao#getuserbyid (int) */@OvErride public User Getuserbyid (int id) {//TODO auto-generated method stub return null; }/* (non-javadoc) * @see com.xdw.dao.userdao#getuserbyusername (java.lang.String) */@Override public use    R Getuserbyusername (String username) {//TODO auto-generated method stub return null; }/* (non-javadoc) * @see Com.xdw.dao.userdao#modifypassword (com.xdw.pojo.User, java.lang.String) */@Overri    De public int Modifypassword (user user, String password) {//TODO auto-generated method stub return 0; }}

These are automatically generated by the compiler code, this time we do not even have to complete the contents of each method, you can jump directly to the implementation of the business of the main class to implement our business, because these methods will have a default return value, does not affect the program's compilation and operation.

At this point we create a class mainclass that implements the business in the main package, as follows

/** * */package com.xdw.main;import com.xdw.dao.userdao;import com.xdw.daoimpl.userdaoimpl;import com.xdw.pojo.User ;/** * @author Xiadewang * January 14, 2018 */public class MainClass {/** * @param args */public static void main (string[] args)        {//TODO auto-generated method stub Userdao userdao=new Userdaoimpl ();        if (Userdao.checklogin ("Xdw", "123456")) {System.out.println ("User XDW login succeeded");        }else {System.out.println ("User XDW Login Failed");        } if (Userdao.checklogin ("xxx", "1234")) {System.out.println ("User xxx login succeeded");        }else {System.out.println ("User xxx Login Failed");        } User User1=new User (1, "XDW", "123456");        User User2=new User (2, "xxx", "TTT");        if (Userdao.checklogin (user1)!=null) {System.out.println ("User XDW login succeeded");        }else {System.out.println ("User XDW Login Failed"); } if (Userdao.checklogin (user2)!=null) {System.out.println ("User xxx login succeeded");        }else {System.out.println ("User xxx Login Failed"); }    }}

Here, I only use 2 overloaded Checklogin method to do the business show, everyone can now run under, run the result as follows

Carried out, in fact, the entire business design and implementation is complete, the rest of the work we are going to specific to improve the content of the interface implementation class, let them more in line with the actual business processing, such as to connect the database to do real data processing, here only with the first checklogin to do the following example, The perfect Userdaoimpl are as follows:

/** * */package com.xdw.daoimpl;import java.sql.resultset;import java.sql.sqlexception;import Com.xdw.dao.UserDao; Import Com.xdw.pojo.user;import com.xdw.utils.dbhelper;/** * @author Xiadewang * January 14, 2018 */public class Userdaoimpl    Implements Userdao {private String sql = null;    Private DBHelper DB1 = null;    Private RESULTSET ret = null; /* (non-javadoc) * @see com.xdw.dao.userdao#checklogin (java.lang.String, java.lang.String) */@Override Publ            IC boolean Checklogin (string username, string password) {//TODO auto-generated method stub try { sql = "SELECT * from user where username=?" and password=? ";            /SQL statement DB1 = new dbhelper (SQL);//Create DBHelper Object Db1.pst.setString (1, username);            Db1.pst.setString (2, password);            ret = Db1.pst.executeQuery ();//execute statement, get result set if (Ret.next ()) {return true;            } ret.close ();          Db1.close ();//close connection          } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();    } return false; }/* (non-javadoc) * @see com.xdw.dao.userdao#checklogin (com.xdw.pojo.User) */@Override public User Chec    Klogin (user user) {//TODO auto-generated method stub return null; }/* (non-javadoc) * @see com.xdw.dao.userdao#register (com.xdw.pojo.User) */@Override public int Registe    R (user user) {//TODO auto-generated method stub return 0; }/* (non-javadoc) * @see Com.xdw.dao.userdao#getuserbyid (int) */@Override public User getuserbyid (int i    d) {//TODO auto-generated method stub return null; }/* (non-javadoc) * @see com.xdw.dao.userdao#getuserbyusername (java.lang.String) */@Override public use    R Getuserbyusername (String username) {//TODO auto-generated method stub return null; }/* (Non-javadoc) * @see Com.xdw.dao.userdao#modifypassword (com.xdw.pojo.User, java.lang.String) */@Override public int mod    Ifypassword (user user, String password) {//TODO auto-generated method stub return 0; }}

You don't need me here. The important role of the interface, Java's core personal feel is the class, object and interface, especially for interface programming.

Beginners tend to feel that the interface is not a bird, it can be a few lines of code implementation of the function, but to add an interface, but also to add an implementation class, not only to increase the code is also around.

Through the demonstration of the interface programming here, we have no idea that we can make the project more clear, the code can read and the more extensible. We need to have a concept, programming is not the less code, the better, but to design and thinking more clearly the better.

This is the end of the next article will be introduced into the servlet+jsp, then there will be UI interface, the front is the console console output, may feel not fun, or feel that the code is not used, can not be applied to the actual.

The learning process of Java (II.)

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.