编写构造方法 初始化连接与数据dao
alt+ins
选择Constructor构造方法
我们将连接对象以及人类到定义为全局变量
package service.impl; import dao.impl.PersonDaoImpl; import service.PersonService; import tools.JDBCUtiles; import java.sql.Connection; public class PersonServiceImpl implements PersonService { //连接对象 private Connection connection; //人类dao private PersonDaoImpl personDao; //构造方法 public PersonServiceImpl() { //获得链接对象 connection = JDBCUtiles.getConnection(); //获得dao对象 personDao = new PersonDaoImpl(connection); } }
编写close方法
public void close(){ //关dao的资源 this.personDao.close(); //关连接对象 JDBCUtiles.close(this.connection); }
编写完毕