dao层
dao全称 data access object,即数据连接层,也叫持久层,是数据库和web服务器的连接层。
包含两个基本java⽂件,⼀个⽂件是专门写接⼝,另⼀个是写接⼝的实现,专门写接⼝程序为了⽅便contraller调⽤。
接⼝程序:
1package dao;
2import java.util.List;
3import entity.Student;
4
5public interface StuDao {
6public List <Student> selAllStu();
7public void insertStu(Student stu);
8public void delStu(String num);
9 }
接⼝实现:
1package dao;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.sql.Statement;
8import java.util.ArrayList;
9import java.util.List;
10
11import entity.Student;
12
13public class StuDaoImp implements StuDao { //zhidao.baidu/question/355976494.html
14public List <Student> selAllStu(){
15 List <Student> list = new <String> ArrayList();
16 Connection con = null;
17 Statement st = null;
18 ResultSet rs = null;
19try {
20 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
21 con = Connection("jdbc:oracle:thin:@//172.16." , "" , "" );//建⽴连接
22 st = ateStatement(); //获取Statement对象
23 rs = st.executeQuery("select * from tb_students");
()){
25 Student stu = new Student();
26 stu.String("num"));
27 stu.String("name"));
28 stu.String("sex"));
29 stu.Int("age"));
30 list.add(stu);
31 }
32
33 } catch (Exception e) {
34// TODO Auto-generated catch block
35 e.printStackTrace();
36 }
37finally{
38try {
39if(rs != null)rs.close();
40if(st != null)st.close();
41if(con != null)con.close();
42 } catch (SQLException e) {
43// TODO Auto-generated catch block
44 e.printStackTrace();
45 }
46 }
47return list;
48 }
49public 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('"+Num()+"','"+Name()+"','"+Sex()+"',"+Age()+")"; 54try {
55 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
56 con = Connection("jdbc:oracle:thin:@//172.16." , "" , "" );//建⽴连接
57 st = ateStatement(); //获取Statement对象
58 st.execute(sql);
59 } catch (Exception e) {
60// TODO Auto-generated catch block
61 e.printStackTrace();
62 }
63finally{
64try {
65if(rs != null)rs.close();
66if(st != null)st.close();
67if(con != null)con.close();
68 } catch (SQLException e) {
69// TODO Auto-generated catch block
70 e.printStackTrace();
71 }
72 }
73
74 }
75public 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+"'";
80try {
81 Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
82 con = Connection("jdbc:oracle:thin:@//172.16." , "" , "" );//建⽴连接
83 st = ateStatement(); //获取Statement对象
84 st.execute(sql);
85 } catch (Exception e) {
86// TODO Auto-generated catch block
87 e.printStackTrace();jdbc连接oracle
88 }
89finally{
90try {
91if(rs != null)rs.close();
92if(st != null)st.close();
93if(con != null)con.close();
94 } catch (SQLException e) {
95// TODO Auto-generated catch block
96 e.printStackTrace();
97 }
98 }
99 }
100 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论