首页 > 代码库 > 采用MVC模式JDBC演示案例

采用MVC模式JDBC演示案例

MVC三层架构:

Model 模型层,数据处理和业务逻辑

View 视图层,为客户展示内容

Control 控制层,协调控制,更新模型

案例如下:

1、获得数据库连接

 1 package com.db;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 public class JDBCUtil {
 8     
 9     private static final String URL="jdbc:mysql://127.0.0.1:3306/student";
10     private static final String USER="root";
11     private static final String PASSWORD="root";
12     private static Connection conn = null;
13     
14     static{
15         try {
16             Class.forName("com.mysql.jdbc.Driver");
17             conn = DriverManager.getConnection(URL, USER, PASSWORD);
18         } catch (ClassNotFoundException e) {
19             e.printStackTrace();
20         } catch (SQLException e) {
21             e.printStackTrace();
22         }
23     }
24     
25     public static Connection getConn(){
26         return conn;
27     }
28 
29 }

2、创建模型

 1 package com.model;
 2 
 3 public class Student {
 4 
 5     private int id;
 6     private String name;
 7     private char sex;
 8     private int age;
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public char getSex() {
22         return sex;
23     }
24     public void setSex(char sex) {
25         this.sex = sex;
26     }
27     public int getAge() {
28         return age;
29     }
30     public void setAge(int age) {
31         this.age = age;
32     }
33     
34     
35     
36 }

3、创建数据库访问对象--DAO

  1 package com.dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  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 com.zhidi.db.JDBCUtil;
 12 import com.zhidi.model.Student;
 13 
 14 public class StuUtile {
 15     
 16     private static final Connection conn = JDBCUtil.getConn();
 17     
 18     //添加
 19     public void addStu(Student stu) throws SQLException{
 20         String sql = "insert into stu"+
 21                     "(id,name,sex,age) values (?,?,?,?)";
 22         PreparedStatement ps = conn.prepareStatement(sql);
 23         ps.setInt(1, stu.getId());
 24         ps.setString(2, stu.getName());
 25         ps.setString(3,String.valueOf(stu.getSex()));
 26         ps.setInt(4, stu.getAge());
 27         
 28         ps.execute();
 29         
 30     }
 31     
 32     //删除
 33     public void delStu(int id) throws SQLException{
 34         String sql = "delete from stu where id=?";
 35     PreparedStatement ps = conn.prepareStatement(sql);
 36     ps.setInt(1,id);
 37     
 38     ps.execute();
 39     }
 40     
 41     //修改
 42     public void updateStu(Student stu) throws SQLException{
 43         String sql = "update stu set name=?,sex=?,age=? where "+
 44                 "id=?";
 45     PreparedStatement ps = conn.prepareStatement(sql);
 46     ps.setString(1, stu.getName());
 47     ps.setString(2,String.valueOf(stu.getSex()));
 48     ps.setInt(3, stu.getAge());
 49     ps.setInt(4, stu.getId());
 50     
 51     ps.execute();
 52     }
 53     
 54     //查询所有学生
 55     public List<Student> query() {
 56         List<Student> list = new ArrayList<Student>();
 57         try {
 58             Statement stmt = conn.createStatement();
 59             ResultSet rs = stmt.executeQuery("select * from stu");
 60             
 61             Student stu = null;
 62             while(rs.next()){
 63                 stu = new Student();
 64                 stu.setId(rs.getInt("id"));
 65                 stu.setName(rs.getString("name"));
 66                 String sex = rs.getString("sex");
 67                 char[] ch = sex.toCharArray();
 68                 for (char c : ch) {
 69                     stu.setSex(c);
 70                 }
 71                 stu.setAge(rs.getInt("age"));
 72                 list.add(stu);
 73             }
 74             
 75         } catch (SQLException e) {
 76             e.printStackTrace();
 77         }
 78         
 79         return list;
 80     }
 81     
 82     //查询单个学生
 83     public Student getStu(int id) throws SQLException{
 84         String sql = "select * from stu where id=?";
 85     PreparedStatement ps = conn.prepareStatement(sql);
 86     ps.setInt(1, id);
 87     
 88     ResultSet rs = ps.executeQuery();
 89     Student stu = null;
 90     while(rs.next()){
 91         stu = new Student();
 92         stu.setId(rs.getInt("id"));
 93         stu.setName(rs.getString("name"));
 94         String sex = rs.getString("sex");
 95         char[] ch = sex.toCharArray();
 96         for (char c : ch) {
 97             stu.setSex(c);
 98         }
 99         stu.setAge(rs.getInt("age"));
100     }
101         return stu;
102     }
103 
104 }

4、控制层逻辑代码

 1 package com.control;
 2 
 3 import java.sql.SQLException;
 4 import java.util.List;
 5 
 6 import com.zhidi.dao.StuUtile;
 7 import com.zhidi.model.Student;
 8 
 9 public class StuControl {
10 
11     public static void main(String[] args) throws SQLException {
12         StuUtile su = new StuUtile();
13         Student stu = new Student();
14         stu.setId(10);
15         stu.setName("张芳");
16         stu.setSex(‘女‘);
17         stu.setAge(20);
18 //        su.addStu(stu);
19         
20 //        su.updateStu(stu);
21         
22 //        su.delStu(10);
23         Student s = su.getStu(1);
24         System.out.println(s.getId()+" "+s.getName()+" "+s.getSex()+" "+s.getAge());
25         
26 //        List<Student> list = su.query();
27 //        for (Student s : list) {
28 //            System.out.println(s.getId()+" "+s.getName()+" "+s.getSex()+" "+s.getAge());
29 //        }
30 
31     }
32 
33 }

 

采用MVC模式JDBC演示案例