从零开始,制作简易MVC(二)构建dao层
目的:
dao层是data access object,它的作用是用来连接数据库并从对数据库进行操作。在这个MVC的制作当中,是将dao层和控制层分开,dao层只与model层相关,将数据库数据内容转换为对象,被控制层调用。
所以dao层中不含SQL,SQL是写在控制层当中的,这样将其分开是为了之后便于扩展,便于维护。
代码:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 import java.util.ArrayList; 8 9 /* 10 * author:lihao 11 * 12 * date:20161101 13 * 14 * idea:构建dao层 15 * 16 * */ 17 public class Test3Dao { 18 19 String DRIVER = "com.mysql.jdbc.Driver"; 20 //先初始化 21 Connection conn = null ; 22 PreparedStatement ps = null; 23 ResultSet rs = null; 24 //连接数据库 25 public Test3Dao(){ 26 try { 27 //启动jdbc驱动 28 Class.forName(DRIVER); 29 } catch (ClassNotFoundException e) { 30 //若jdbc驱动类没有找到,则抛出异常 31 e.printStackTrace(); 32 // TODO: handle exception 33 } 34 //启动jdbc驱动和连接数据库都采用try catch模式 方便 35 //之后扩展异常处理机制 36 try { 37 conn = DriverManager.getConnection("jdbc:mysql://**.*.**.**:3306/test", "roots", "roots"); 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 // TODO: handle exception 41 } 42 } 43 44 //增加数据 45 //使用count作为特征,用来做之后在控制层的判断 46 public int getInsert(String sql,Test3Model m){ 47 int count = 0; 48 try { 49 ps = conn.prepareStatement(sql); 50 ps.setString(1, m.getName()); 51 ps.setString(2, m.getPerno()); 52 ps.setString(3, m.getNation()); 53 ps.setString(4, m.getPhone()); 54 count = ps.executeUpdate(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 // TODO: handle exception 58 } 59 //在这里必须要将查询和连接结束掉,以达成单次使用 60 //单次连接,这是处于安全性考虑。 61 finally{ 62 try { 63 ps.close(); 64 conn.close(); 65 } catch (SQLException e) { 66 e.printStackTrace(); 67 // TODO: handle exception 68 } 69 } 70 return count; 71 } 72 73 //删除 74 75 public int getDel(String sql,int id){ 76 int count = 0; 77 try { 78 ps = conn.prepareStatement(sql); 79 ps.setInt(1, id); 80 count = ps.executeUpdate(); 81 } catch (SQLException e) { 82 e.printStackTrace(); 83 // TODO: handle exception 84 } 85 finally{ 86 try { 87 ps.close(); 88 conn.close(); 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 // TODO: handle exception 92 } 93 } 94 return count; 95 } 96 97 //根据ID查询 98 99 public Test3Model getSearchById(String sql,int id){ 100 Test3Model m = new Test3Model(); 101 try { 102 103 ps = conn.prepareStatement(sql); 104 ps.setInt(1, id); 105 rs = ps.executeQuery(); 106 //将其他数据关联 107 while (rs.next()) { 108 m.setName(rs.getString("name")); 109 m.setPerno(rs.getString("perno")); 110 m.setNation(rs.getString("nation")); 111 m.setPhone(rs.getString("phone")); 112 } 113 114 } catch (SQLException e) { 115 e.printStackTrace(); 116 // TODO: handle exception 117 } 118 return m; 119 } 120 121 //修改 122 123 public int getUpdata(String sql,Test3Model m){ 124 125 int count = 0; 126 127 try { 128 ps = conn.prepareStatement(sql); 129 ps.setInt(5, m.getId()); 130 ps.setString(1, m.getName()); 131 ps.setString(2, m.getPerno()); 132 ps.setString(3, m.getNation()); 133 ps.setString(4, m.getPhone()); 134 count = ps.executeUpdate(); 135 } catch (SQLException e) { 136 e.printStackTrace(); 137 // TODO: handle exception 138 } 139 finally{ 140 try { 141 ps.close(); 142 conn.close(); 143 } catch (SQLException e) { 144 e.printStackTrace(); 145 // TODO: handle exception 146 } 147 } 148 return count; 149 } 150 151 //显示所有记录 采用数据列表的方式达成这一目的 152 153 public ArrayList<Test3Model> getSearch(String sql){ 154 ArrayList<Test3Model> list = new ArrayList<Test3Model>(); 155 156 try { 157 ps = conn.prepareStatement(sql); 158 rs = ps.executeQuery(); 159 160 while (rs.next()) { 161 Test3Model m = new Test3Model(); 162 m.setId(rs.getInt("id")); 163 m.setName(rs.getString("name")); 164 m.setPerno(rs.getString("perno")); 165 m.setNation(rs.getString("nation")); 166 m.setPhone(rs.getString("phone")); 167 list.add(m); 168 } 169 } catch (SQLException e) { 170 e.printStackTrace(); 171 // TODO: handle exception 172 } 173 finally { 174 try { 175 ps.close(); 176 conn.close(); 177 } catch (SQLException e) { 178 e.printStackTrace(); 179 // TODO: handle exception 180 } 181 } 182 return list; 183 } 184 }
dao层是data access object,它的作用是用来连接数据库并从对数据库进行操作。在这个MVC的制作当中,是将dao层和控制层分开,dao层只与model层相关,将数据库数据内容转换为对象,被控制层调用。
所以dao层中不含SQL,SQL是写在控制层当中的,这样将其分开是为了之后便于扩展,便于维护。
浙公网安备 33010602011771号