MVC模式
MVC是Model(模型) View(视图) Controller(控制器)的缩写,是实现业务模型与用户界面分离的一种设计模式,而控制器则是保证业务模型与用户界面的同步
案例_使用GUI界面操作数据库的数据
1.界面展示
2.实现思路
- 核心方法
1.1 获取全部员工的一般信息
1.2 根据员工编号,获取员工的所有信息(一般信息、部门信息、薪资等级信息)
1.3 根据员工编号,修改员工的一般信息
1.4 根据员工编号,删除员工的所有信息
1.5 添加员工信息 - MVC框架
view中 的类调用controller中的接口
controller中接口的实现类调用service中的接口
service中的接口实现类调用dao中的接口
dao中的接口实现类实现jdbc
3. 实现
//查询所有员工信息
public List<Emp> getAllEmp(){
Connection conn=null;//声明一个连接对象
PreparedStatement pstat=null;//声明一个语句对象
ResultSet set=null;//声明一个结果集
List<Emp> list=null;//声明一个结果集的对象容器
try{
Class.forName("com.mysql.jdbc.Driver");//加载驱动
conn=DriverManage.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false","root","root");//创建连接对象
String sql="select * from emp";//sql语句
pstat=conn.prepareStatement(sql);//传递sql语句
set=pstat.executeQuery();//返回sql执行结果
list=new LinkedList();//创建一个容器
while(set.next()){
Emp emp=new Emp();//创建要给emp对象
emp.setEmpno(set.getInt("empno");//设置emp对象的empno属性值为set中empno的值
emp.setDeptno(set.getInt("deptno");//设置emp对象的deptno属性值为set中的deptno的值
emp.setEname(set.getString("ename");
emp.setJob(set.getString("job");
emp.setHiredate(set.getDate("hiredate");
emp.setComm(set.getDouble("comm");
emp.setMgr(set.getInt("mgr");
list.add(emp);//将获取到的一个emp对象封装到list容器中
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
//关闭资源
set.close();
pstat.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
//返回查询结果的容器
return list;
}
4.优化
4.1 使用连接池等工具jdbc工具优化
- 动态代理连接池
public MyConnectionPool{
prviate static final String DRIVER="com.mysql.jdbc.Driver";//指定驱动全限定名
private static final String URL="jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false";//指定URL
private static final String USER="root";//指定用户名
private static final String PWD="root";//指定登录密码
private static LinkedList<Connection> pool;//声明连接池
private static final int INITSIZE=5;//连接池中连接的初始数量
private static final int MAXSIZE=10;//连接池中连接的最大数量
//静态代码块实现,在第一次加载类时就创建连接池
static{
try{
Class clazz=Class.forName(DRIVER);//创建驱动的字节码对象---初始化驱动
pool=new LinkedList<Connection>();//创建连接池
for(int i=0;i<INITSIZE;i++){
//向池中添加INITSIZE个连接
pool.add(initConnection());//通过调用initConnection方法一次添加一个连接
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
//添加一个连接或关闭连接
private static Conneciton initConnection(){
try{
Connection conn=DriverManage.getConncetion(URL,USER,PWD);//创建一个连接对象
//动态代理一个连接对象
Connection proxyConn=(Connection)Proxy.newProxyInstance(
conn.getClass().getClassLoader(),//获取被代理对象的类的类加载器
conn.getClass().getInterfaces(),//获取被代理对象的类的实现的所有接口
new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
Object res=null;
//如果被执行的方法是close方法,就开始回收连接
if(method.getName().equals("close")){
if(pool.size()<MAXSIZE){
//如果池中未满,就直接回收入池
pool.addLast((Conneciton)proxy);
}else{
//如果池中已满,就直接关闭
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
//如果不是close方法,就继续执行被代理对象的方法
}else{
res=method.invoke(conn, args);
}
return res;
}
}
);
return proxyConn;
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
//向外界提供一个连接
public static Connection getConnection(){
if(pool.size>0){
//如果池中还有连接,就直接取出一个连接
return pool.removeFirst();
}else{
//如果池中没有连接,就直接创建一个新连接
retuen initConnection();
}
}
}
- DBUtil类
public class DBUtil{
//获取一个连接的方法
public static Connection getConnection(){
return MyConnectionPool.getConnection();
}
//关闭资源
public static void relase(ResultSet set, PreparedStatement pstat, Connection conn){
if(set != null){
set.close();
}
if(pstat != null){
pstat.close();
}
if(conn != null){
conn.close();
}
}
}