DAO design mode and dao design

Source: Internet
Author: User

DAO design mode and dao design

DAO design mode

DAO design mode Introduction: DAO design mode can reduce the amount of code, enhance program portability, and improve code readability.

The DAO (database operation object) design mode is the operation on the JavaEE data layer. It consists of five parts:

1. Database Connection class: connect to the database and obtain the connection object.

2. VO object class: contains the class that corresponds to the fields in the table.

3. DAO interface: provides all the user's operation methods (for example, the teacher provides some learning methods for students ).

4. DAO implementation class: implement all the methods in DAO (just as the method provided by the teacher depends on how you complete it ).

5. DAO factory class: provides methods for programs. To replace DAO implementation class, you only need to modify the method code in the Dao factory class,

You don't have to ask to modify all the operation database Code (for example, the agent provides talents for the companies you need, also called the service layer ).

 

The following uses a user logon as an example:

1. Create a database table:

2. Database Connection class com. util (remember to import the c3p0 Framework Package)

Package com. util; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. statement; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource; public class C3P0Util {// C3P0 data source private static ComboPooledDataSource dataSource = new ComboPooledDataSource (); public static DataSource getDataSource () {return dataSource;} public static Connection getConn () {Connection co Nne = null; try {conne = dataSource. getConnection ();} catch (Exception e) {e. printStackTrace () ;}return conne;} // close the public static void closeAll (ResultSet rs, Statement st, Connection conne) {try {if (null! = Rs) {rs. close () ;}} catch (Exception e) {e. printStackTrace () ;}try {if (null! = St) {st. close () ;}} catch (Exception e) {e. printStackTrace () ;}try {if (conne! = Null) {conne. close () ;}} catch (Exception e) {e. printStackTrace ();}}}

 

3. VO entity class com. entity

package com.entity;import java.io.Serializable;public class Person implements Serializable {    private static final long serialVersionUID = 1L;    private int id;    private String name;    private String password;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

  

4. DAO Interface Class com. dao

Package com. dao; import com. entity. Person; public interface PersonDao {// query public Person findMaster (Person person Person) based on the object );}

  

5. DAO implementation class com. daoImpl

Package com. daoImpl; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import com. dao. personDao; import com. entity. person; import com. util. c3P0Util; public class PersonDaoImpl implements PersonDao {@ Override public Person findMaster (Person person) {Connection conne = null; PreparedStatement pstmt = null; ResultSet rs = null; Person person2 = null; string SQL = "SE LECT * FROM person WHERE name =? AND password =? "; Try {conne = C3P0Util. getConn (); // After establishing a connection to a specific database, you can use this connection to send an SQL statement. Pstmt = conne. prepareStatement (SQL); pstmt. setString (1, person. getName (); pstmt. setString (2, person. getPassword (); rs = pstmt.exe cuteQuery (); if (rs. next () {person2 = new Person (); person2.setId (rs. getInt ("id"); person2.setName (rs. getString ("name"); person2.setPassword (rs. getString ("password") ;}} catch (Exception e) {e. printStackTrace ();} finally {C3P0Util. closeAll (rs, pstmt, conne) ;}return person2 ;}}

  

6. DAO factory interface com. service

package com.service;import com.entity.Person;public interface PersonService {    public boolean login (Person person);}

  

7. DAO factory implementation class com. serviceImpl

package com.serviceImpl;import com.dao.PersonDao;import com.daoImpl.PersonDaoImpl;import com.entity.Person;import com.service.PersonService;public class PersonServiceImpl implements PersonService {    @Override    public boolean login(Person person) {        boolean islogin = false;        PersonDao pd = new PersonDaoImpl();        Person ps2 = pd.findMaster(person);        if (null != ps2) {            islogin = true;        }        return islogin;    }}

  

8. Test

Package com. test; import java. util. logging; import org. junit. test; import com. entity. person; import com. service. personService; import com. serviceImpl. personServiceImpl; public class LoginTest {@ Test public void loginTest () {login SC = new Login (System. in, "UTF-8"); System. out. println ("Enter the user name"); String loginName = SC. next (); System. out. println ("Enter Password"); String loginPassword = SC. next (); Person person = new Person (); person. setName (loginName); person. setPassword (loginPassword); PersonService ps = new PersonServiceImpl (); boolean isLoginid = ps. login (person); if (isLoginid) {System. out. println ("Logon successful");} else {System. out. println ("incorrect user name or password ");}}}

 

The running result is as follows:

 

 

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.