Java的DAO设计模式
Posted xhanglog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的DAO设计模式相关的知识,希望对你有一定的参考价值。
用java的DAO模式实现对一个学生实体的增加,查询操作。
1.建立一个学生实体类 Student.java
1 public class Student { 2 private String sid; 3 private String sname; 4 private int age; 5 private int banji; 6 private int score; 7 8 public Student(String sid, String sname, int age, int banji, int score) { 9 this.sid = sid; 10 this.sname = sname; 11 this.age = age; 12 this.banji = banji; 13 this.score = score; 14 } 15 16 public String getSid() { 17 return sid; 18 } 19 20 public void setSid(String sid) { 21 this.sid = sid; 22 } 23 24 public String getSname() { 25 return sname; 26 } 27 28 public void setSname(String sname) { 29 this.sname = sname; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 public void setAge(int age) { 37 this.age = age; 38 } 39 40 public int getBanji() { 41 return banji; 42 } 43 44 public void setBanji(int banji) { 45 this.banji = banji; 46 } 47 48 public int getScore() { 49 return score; 50 } 51 52 public void setScore(int score) { 53 this.score = score; 54 } 55 56 @Override 57 public String toString() { 58 return "student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", banji=" + banji + ", score=" + score 59 + "]"; 60 } 61 62 }
2.数据库连接工具类 ConnectionUtil.java
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 public class ConnectionUtil { 8 ConnectionUtil(){} 9 private static Connection con ; 10 11 static{ 12 try{ 13 Class.forName("com.mysql.jdbc.Driver"); 14 String url = "jdbc:mysql://localhost:3306/shiyan"; 15 String username="root"; 16 String password="1234"; 17 con = DriverManager.getConnection(url, username, password); 18 }catch(Exception ex){ 19 throw new RuntimeException(ex+"数据库连接失败!"); 20 } 21 } 22 23 public static Connection getConnection(){ 24 return con; 25 } 26 27 /*关闭数据库连接操作*/ 28 public static void close(Connection con,Statement stat , ResultSet rs){ 29 if(rs!=null){ 30 try{ 31 rs.close(); 32 }catch(SQLException ex){} 33 } 34 35 if(stat!=null){ 36 try{ 37 stat.close(); 38 }catch(SQLException ex){} 39 } 40 41 if(con!=null){ 42 try{ 43 con.close(); 44 }catch(SQLException ex){} 45 } 46 47 } 48 }
3.DAO接口 UpdateDao.java
1 import java.util.List; 2 3 public interface UpdateDao { 4 5 /*查询所有的学生,返回一个List集合中*/ 6 public List<Student> findAllStudent(); 7 8 /*增加学生*/ 9 public void addStudent(Student stu); 10 11 }
4.DAO接口实现类 UpdateDaoImpl.java
1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 public class UpdateDaoImpl implements UpdateDao { 9 10 @Override 11 public List<Student> findAllStudent() { 12 13 List<Student> list = new ArrayList<Student>(); 14 15 Connection conn = ConnectionUtil.getConnection(); 16 String sql = "select * from student"; 17 PreparedStatement pst; 18 try { 19 pst = conn.prepareStatement(sql); 20 ResultSet rs = pst.executeQuery(); 21 22 while (rs.next()) { 23 Student s = new Student(rs.getString("sid"), rs.getString("sname"), rs.getInt("age"), 24 rs.getInt("banji"), rs.getInt("score")); 25 list.add(s); 26 } 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } 30 return list; 31 } 32 33 @Override 34 public void addStudent(Student stu) { 35 Connection conn = ConnectionUtil.getConnection(); 36 try { 37 String sql = "insert into student values(?,?,?,?,?)"; 38 PreparedStatement pst = conn.prepareStatement(sql); 39 pst.setString(1, stu.getSid()); 40 pst.setString(2, stu.getSname()); 41 pst.setInt(3, stu.getAge()); 42 pst.setInt(4, stu.getBanji()); 43 pst.setInt(5, stu.getScore()); 44 45 pst.executeUpdate(); 46 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 51 } 52 53 }
5.建立一个Junit类进行测试 TestDao.java
1 import java.util.List; 2 3 import org.junit.Test; 4 5 import test3001.Student; 6 import test3001.UpdateDao; 7 import test3001.UpdateDaoImpl; 8 9 public class testDao { 10 11 @Test 12 /*测试增加学生*/ 13 public void testaddStudent() { 14 UpdateDao dao = new UpdateDaoImpl(); 15 Student stu=new Student("8","蓝猫",20,2,98); 16 dao.addStudent(stu); 17 } 18 19 @Test 20 /*测试查询所有学生信息*/ 21 public void testfindAllStudent() { 22 UpdateDao dao = new UpdateDaoImpl(); 23 List<Student> list = null; 24 list = dao.findAllStudent(); 25 for (Student stu : list) { 26 System.out.println(stu); 27 } 28 } 29 }
-----------泛型DAO是什么鬼 -_- ---------------------------
以上是关于Java的DAO设计模式的主要内容,如果未能解决你的问题,请参考以下文章