這是項目的架構 mysql
dao層下面有一個平級的包 impl //dao層 訪問數據庫。sql
GradeDAOImpl 他繼承了BaseDAO 實現了IGradeDAO接口數據庫
public class GradeDAOImpl extends BaseDAO implements IGradeDAO { @Override public List<Grade> findAllGrades() throws Exception { List<Grade> list = new ArrayList<Grade>(); String sql = "select * from grade"; ResultSet rs = executeQuery(sql); if (rs != null) { while (rs.next()) { Grade grade = new Grade(); grade.setGid(rs.getInt("gid")); grade.setGname(rs.getString("gname")); list.add(grade); } } return list; } @Override public boolean addGrade(Grade grade) throws Exception { boolean flag = false;// 添加失敗 String sql = "insert into grade(gname) values(?)"; int count = executeUpdate(sql, grade.getGname()); if (count > 0) { flag = true; } return flag; } }
BaseDAO類 //訪問數據庫的底層類架構
/** * 數據庫訪問的工具類 * * @author Happy * */ public class BaseDAO { // 定義四個靜態常量,保存數據鏈接信息 private static final String driver = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql:///s2230myschool"; // localhost // // // 3306 private static final String username = "root"; private static final String password = ""; // 三個接口 private Connection con; private PreparedStatement ps; private ResultSet rs; // 01.寫一個 返回鏈接的方法 public Connection getConnection() throws Exception { Class.forName(driver); if (con == null || con.isClosed()) { con = DriverManager.getConnection(url, username, password); } return con; } // 02.寫一個執行增 刪 改操做的方法 public int executeUpdate(String sql, Object... objs) throws Exception { getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i + 1, objs[i]); } int count = ps.executeUpdate(); return count; } // 03.寫一個執行查詢操做的方法 public ResultSet executeQuery(String sql, Object... objs) throws Exception { getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i + 1, objs[i]); } rs = ps.executeQuery(); return rs; } // 04.寫一個回收資源的方法 public void closeResource() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { con.close(); } } catch (Exception e) { e.printStackTrace(); } } }
IGradeDAO接口app
public interface IGradeDAO { // 01.書寫一個用於查詢全部年級名稱和編號的方法 public List<Grade> findAllGrades() throws Exception; // 02.寫一個用戶添加年級的方法 public boolean addGrade(Grade grade) throws Exception; }
entity層 //實體類ide
public class Grade { private int gid; private String gname; public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } }
service層 有一個平級的 impl層 // GradeServiceImpl 實現了 IGadeService 接口 工具
GradeServiceImpl :測試
public class GradeServiceImpi implements IGradeService { // 須要向下依賴DAO的實例 以及他提供的方法 IGradeDAO dao = new GradeDAOImpl(); @Override public List<Grade> findAllGrades() throws Exception { return dao.findAllGrades(); } @Override public boolean addGrade(Grade grade) throws Exception { return dao.addGrade(grade); } }
IGadeService 接口 :this
public interface IGradeService { // 01.書寫一個用於查詢全部年級名稱和編號的方法 public List<Grade> findAllGrades() throws Exception; // 02.寫一個用戶添加年級的方法 public boolean addGrade(Grade grade) throws Exception; }
test層 //UI層 url
//測試類:
public class JDBCTest { public static void main(String[] args) throws Exception { /*GradeDAOImpl gradeDAO = new GradeDAOImpl(); Grade grade = new Grade(); grade.setGname("精英班"); boolean flag = gradeDAO.addGrade(grade); System.out.println(flag);*/ //UI層 依賴於service IGradeService service =new GradeServiceImpi(); List<Grade> list =service.findAllGrades(); for(Grade grade: list){ System.out.println(grade.getGname()); } } }