MVC三层架构:
Model 模型层,数据处理和业务逻辑
View 视图层,为客户展示内容
Control 控制层,协调控制,更新模型
案例如下:
1、获得数据库连接
package com.db; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class JDBCUtil { private static final String URL="jdbc:mysql://127.0.0.1:3306/student";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn = null; static{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} public static Connection getConn(){
return conn;
} }
2、创建模型
package com.model; public class Student { private int id;
private String name;
private char sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
3、创建数据库访问对象--DAO
package com.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import com.zhidi.db.JDBCUtil;
import com.zhidi.model.Student; public class StuUtile { private static final Connection conn = JDBCUtil.getConn(); //添加
public void addStu(Student stu) throws SQLException{
String sql = "insert into stu"+
"(id,name,sex,age) values (?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, stu.getId());
ps.setString(2, stu.getName());
ps.setString(3,String.valueOf(stu.getSex()));
ps.setInt(4, stu.getAge()); ps.execute(); } //删除
public void delStu(int id) throws SQLException{
String sql = "delete from stu where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id); ps.execute();
} //修改
public void updateStu(Student stu) throws SQLException{
String sql = "update stu set name=?,sex=?,age=? where "+
"id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, stu.getName());
ps.setString(2,String.valueOf(stu.getSex()));
ps.setInt(3, stu.getAge());
ps.setInt(4, stu.getId()); ps.execute();
} //查询所有学生
public List<Student> query() {
List<Student> list = new ArrayList<Student>();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from stu"); Student stu = null;
while(rs.next()){
stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
String sex = rs.getString("sex");
char[] ch = sex.toCharArray();
for (char c : ch) {
stu.setSex(c);
}
stu.setAge(rs.getInt("age"));
list.add(stu);
} } catch (SQLException e) {
e.printStackTrace();
} return list;
} //查询单个学生
public Student getStu(int id) throws SQLException{
String sql = "select * from stu where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id); ResultSet rs = ps.executeQuery();
Student stu = null;
while(rs.next()){
stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
String sex = rs.getString("sex");
char[] ch = sex.toCharArray();
for (char c : ch) {
stu.setSex(c);
}
stu.setAge(rs.getInt("age"));
}
return stu;
} }
4、控制层逻辑代码
package com.control; import java.sql.SQLException;
import java.util.List; import com.zhidi.dao.StuUtile;
import com.zhidi.model.Student; public class StuControl { public static void main(String[] args) throws SQLException {
StuUtile su = new StuUtile();
Student stu = new Student();
stu.setId(10);
stu.setName("张芳");
stu.setSex('女');
stu.setAge(20);
// su.addStu(stu); // su.updateStu(stu); // su.delStu(10);
Student s = su.getStu(1);
System.out.println(s.getId()+" "+s.getName()+" "+s.getSex()+" "+s.getAge()); // List<Student> list = su.query();
// for (Student s : list) {
// System.out.println(s.getId()+" "+s.getName()+" "+s.getSex()+" "+s.getAge());
// } } }