dao層

dao全稱 data access object,即數據鏈接層,也叫持久層,是數據庫和web服務器的鏈接層。html

包含兩個基本java文件,一個文件是專門寫接口,另外一個是寫接口的實現,專門寫接口程序爲了方便contraller調用。java

接口程序:web

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 }

接口實現:sql

  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 }
相關文章
相關標籤/搜索