JDBC连接MySQL数据库(四)——DAO模式的使用

DAO模式的使用

DAO模式的介绍

DAO(Data Access Object) 数据存取对象,位于业务逻辑和持久化的数据之间,实现对数据的访问。 此模式主要是封装了数据库的各种操作,主要作用就是隔离了业务逻辑数据库访问之间的代码,从而简化负责处理业务的类的复杂性和耦合性。

DAO模式的组成部分

  • DAO接口:具体定义了要实现的操作
  • DAO实现类:实现接口部分的操作
  • 实体类:规定具体怎么使用

DAO 的使用

在这里简单的介绍一下DAO模式的使用 看如下结构目录图:
JDBC连接MySQL数据库(四)——DAO模式的使用_第1张图片
我们以这个数据库为例
在这里插入图片描述
接下来依次的将每个文件展示出来

创建一个接口:

public interface UserDao {
	 void Query();
	 void insert(int id, String name, int score);
	 void delete(int id);
	 void update(int id,int score);
}

创建一个实现类实现接口中的方法:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Dao.UserDao;
import Utils.JDBCutils;

public class UserDaoImpl  implements UserDao{

	@Override
	public void Query() {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn = JDBCutils.getconn();
			st = conn.createStatement();
			String sql = "select * from  stugrade";
			rs = st.executeQuery(sql);
			while(rs.next()) {
				String name = rs.getString("name");
				int score = rs.getInt("score");
				System.out.println("name: "+name+" score:"+score);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
	@Override
	public void insert(int id, String name, int score) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCutils.getconn();
			String sql ="insert into stugrade values(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.setString(2, name);
			ps.setInt(3, score);
			int rs = ps.executeUpdate();
			if(rs>0) {
				System.out.println("insert ok");
			}else {
				System.out.println("error");
			}	
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			JDBCutils.Release(conn,ps);
		}
	}
	@Override
	public void update(int id, int score) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCutils.getconn();
			String sql = "update stugrade set score= ? where  id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, score);
			ps.setInt(2, id);
			int rs = ps.executeUpdate();
			if(rs > 0) {
				System.out.println("update ok");
			}else {
				System.out.println("error");
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			JDBCutils.Release(conn, ps);
		}
	}
	@Override
	public void delete(int id) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCutils.getconn();
			String sql = "delete from stugrade  where id = ?";
			ps =conn.prepareStatement(sql);
			ps.setInt(1, id);
			int rs = ps.executeUpdate();
			if(rs>0) {
				System.out.println("ok");
			}else {
				System.out.println("error");
			}	
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

添加测试类


import org.junit.Test;
import Dao.UserDao;
import Dao.imp.UserDaoImpl;

public class UserDaoImpTest {
	@Test
	public void testQuery() {
		UserDao dao =new UserDaoImpl();
		dao.Query();
	}
	@Test
	public void insert() {
		UserDao dao = new UserDaoImpl();
		dao.insert(5, "CXK", 99);
	}

	@Test
	public void update() {
		UserDao dao = new UserDaoImpl();
		dao.update(5,60);
	}
	@Test
	public void delete() {
		UserDao dao = new UserDaoImpl();
		dao.delete(2);
	}
}

建立数据库的连接(之前写过):



import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCutils {
	static String url = null;
	static String password = null;
	static String user = null;
	static String DriverClass = null;
	static {
			Properties p = new Properties();
			InputStream is = JDBCutils.class.getClassLoader().getResourceAsStream("jdbc.pro");
			try {
				p.load(is);
				url = p.getProperty("url");
				password = p.getProperty("password");
				user = p.getProperty("user");
				DriverClass = p.getProperty("driverclass");
			} catch (Exception e) {
				
			}
		}
	public static Connection  getconn() {
		Connection conn = null;
		try {
			Class.forName(DriverClass);
			conn = DriverManager.getConnection(url, user, password);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void ReleaseAll(Connection conn, ResultSet rs, Statement st) {
		CloseConn(conn);
		CloseRs(rs);
		CloseSt(st);
	}
	public static void Release(Connection conn, Statement st) {
		CloseConn(conn);
		CloseSt(st);
	}
	
	private static void CloseConn(Connection conn) {
		try {
			if(conn != null) {
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			conn = null;
		}
	}
	private static void CloseRs(ResultSet rs) {
		try {
			if(rs  != null) {
				rs.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			rs  = null;
		}
	}
	private static void CloseSt(Statement st) {
		try {
			if(st  != null) {
				st.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally {
			st  = null;
		}
	}
}

配置文件信息:

url = jdbc:mysql://localhost/studentinformation?serverTimezone=GMT%2B8
user = root
password = 81604152
driverclass = com.mysql.cj.jdbc.Driver

你可能感兴趣的:(JDBC)