JDBC笔记(二)

抽取JDBCUtil和设计DAO包

设计DAO包

在这里插入图片描述

1.DAO接口

public interface IStudentDao {
	//添加信息
	public boolean save(Student stu);
	//根据ID删除信息
	public boolean delete(Integer id);
	//根据ID查找信息
	public Student get(Integer id);
	//根据ID修改信息
	public boolean update(int id,Student stu);
	//得到所有信息
	public List<Student> getAll();
}

2.实现DAO接口

public class IStudentDaoImpl implements IStudentDao {

	@Override
	public boolean save(Student stu) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			//建立数据库连接
			conn = JDBCUtil.getConn();
			//3.编写sql语句
			String sql="insert into stu value(?,?,?)";
			//4.执行sql语句
			ps = conn.prepareStatement(sql);//预编译
			ps.setInt(1, stu.getId());
			ps.setString(2, stu.getName());
			ps.setInt(3, stu.getAge());
			int row = ps.executeUpdate();
			System.out.println(row);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally{
			try {
				JDBCUtil.close(conn, ps, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean delete(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			//2.得到连接对象
			conn=JDBCUtil.getConn();
			//3.编写sql语句
			String sql="delete from stu where id = ?";
			//4.执行sql语句
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			int row = ps.executeUpdate();
			if(row>0){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				JDBCUtil.close(conn, ps, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	@Override
	public Student get(Integer id) {
		PreparedStatement ps = null;
		ResultSet res = null;
		Connection conn = null;
		Student stu = null;
		try {
			//2.获得数据库连接
			conn = JDBCUtil.getConn();
			//3.编写sql语句
			String sql="select * from stu where id=?";
			//4.执行sql语句
			ps = conn.prepareCall(sql);
			ps.setInt(1, id);
			res = ps.executeQuery();
			if(res.next()){
				stu =new Student();
				stu.setId(res.getInt("id"));
				stu.setName(res.getString("name"));
				stu.setAge(res.getInt("age"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				JDBCUtil.close(conn, ps, res);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return stu;
	}

	@Override
	public boolean update(int id, Student stu) {
		Connection conn = null;
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			//2.得到连接对象
			conn=JDBCUtil.getConn();
			//3.编写sql语句
			String sql="update stu set name = ? ,age = ? where id = ?";
			ps= conn.prepareStatement(sql);
			ps.setString(1, stu.getName());
			ps.setInt(2, stu.getAge());
			ps.setInt(3, id);
			//4.执行sql语句
			int row = ps.executeUpdate();
			if(row>0){//影响行数大于0时,则返回true
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				JDBCUtil.close(conn, ps, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	@Override
	public List<Student> getAll() {
		List<Student> stuList=null;
		PreparedStatement ps = null;
		Connection conn = null;
		ResultSet res = null;
		try {
			stuList=new ArrayList<Student>();
			//2.获取连接对象
			conn = JDBCUtil.getConn();
			//3.编写sql语句
			String sql="select * from stu";
			//4.执行sql语句
			ps = conn.prepareStatement(sql);
			res = ps.executeQuery();
			while(res.next()){
				Student stu=new Student();
				stu.setId(res.getInt("id"));
				stu.setName(res.getString("name"));
				stu.setAge(res.getInt("age"));
				stuList.add(stu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				JDBCUtil.close(conn, ps, res);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return stuList;
	}

domain类

public class Student {
	private int id;
	private String name;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

抽取JDBCUtil

public class JDBCUtil {
	public final static String URL="jdbc:mysql://localhost:3306/jdbc_db";
	public final static String USER="root";
	public final static String PASSWOED="123456";
	public final static String CONN_NAME="com.mysql.jdbc.Driver";
	
	//加载驱动
	static{
		try {
			Class.forName(JDBCUtil.CONN_NAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//获取连接对象
	public static Connection getConn() throws SQLException{
		Connection conn = DriverManager.getConnection(JDBCUtil.URL, JDBCUtil.USER, JDBCUtil.PASSWOED);
		return conn;
	}
	
	//关闭资源
	public static void close(Connection conn,Statement st,ResultSet res) throws SQLException{
		if(conn!=null){
			conn.close();			
		}
		if(st!=null){
			st.close();			
		}
		if(res!=null){
			res.close();			
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值