dao层
dao全称 data access object,即数据连接层,也叫持久层,是数据库和web服务器的连接层。
包含两个基本java文件,一个文件是专门写接口,另一个是写接口的实现,专门写接口程序为了方便contraller调用。
接口程序:
1 package dao; 2 import java.util.List; 3 import entity.Student; 4 5 public interface StuDao { 6 public List <Student> selAllStu(); 7 public void insertStu(Student stu); 8 public void delStu(String num); 9 }
接口实现:
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import entity.Student; 12 13 public class StuDaoImp implements StuDao { // https://zhidao.baidu.com/question/355976494.html 14 public List <Student> selAllStu(){ 15 List <Student> list = new <String> ArrayList(); 16 Connection con = null; 17 Statement st = null; 18 ResultSet rs = null; 19 try { 20 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动 21 con = DriverManager.getConnection("jdbc:oracle:thin:@//172.16." , "" , "" );//建立连接 22 st = con.createStatement(); //获取Statement对象 23 rs = st.executeQuery("select * from tb_students"); 24 while(rs.next()){ 25 Student stu = new Student(); 26 stu.setNum(rs.getString("num")); 27 stu.setName(rs.getString("name")); 28 stu.setSex(rs.getString("sex")); 29 stu.setAge(rs.getInt("age")); 30 list.add(stu); 31 } 32 33 } catch (Exception e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 finally{ 38 try { 39 if(rs != null)rs.close(); 40 if(st != null)st.close(); 41 if(con != null)con.close(); 42 } catch (SQLException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 } 47 return list; 48 } 49 public void insertStu(Student stu){ 50 Connection con = null; 51 Statement st = null; 52 ResultSet rs = null; 53 String sql = "insert into tb_students (num,name,sex,age) values('"+stu.getNum()+"','"+stu.getName()+"','"+stu.getSex()+"',"+stu.getAge()+")"; 54 try { 55 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动 56 con = DriverManager.getConnection("jdbc:oracle:thin:@//172.16." , "" , "" );//建立连接 57 st = con.createStatement(); //获取Statement对象 58 st.execute(sql); 59 } catch (Exception e) { 60 // TODO Auto-generated catch block 61 e.printStackTrace(); 62 } 63 finally{ 64 try { 65 if(rs != null)rs.close(); 66 if(st != null)st.close(); 67 if(con != null)con.close(); 68 } catch (SQLException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 } 73 74 } 75 public void delStu(String num){ 76 Connection con = null; 77 Statement st = null; 78 ResultSet rs = null; 79 String sql = "delete from tb_students where num='"+num+"'"; 80 try { 81 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动 82 con = DriverManager.getConnection("jdbc:oracle:thin:@//172.16." , "" , "" );//建立连接 83 st = con.createStatement(); //获取Statement对象 84 st.execute(sql); 85 } catch (Exception e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } 89 finally{ 90 try { 91 if(rs != null)rs.close(); 92 if(st != null)st.close(); 93 if(con != null)con.close(); 94 } catch (SQLException e) { 95 // TODO Auto-generated catch block 96 e.printStackTrace(); 97 } 98 } 99 } 100 }