使用JDBC的CRUD操作

JDBC访问数据库(面向对象的方式)

  • 1创建工具类(工厂类),连接数据

public class StudentInfoUtil {
	private static String forNameDriver="com.mysql.jdbc.Driver";
	private static String jdbcUrl="jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8";
	private static String jdbcUserName="root";
	private static String jdbcPassword="123456";
	/**
	 * @方法名: getConn
	 * @方法说明: 创建链接
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月7日下午11:24:30
	 * @return
	 * @return: Connection
	 */
	public static Connection getConn(){
		Connection conn = null;
		try {
			Class.forName(forNameDriver);
			//创建连接
			conn = DriverManager.getConnection(jdbcUrl, jdbcUserName, jdbcPassword);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * @方法名: closed
	 * @方法说明: 释放资源
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月7日下午11:37:05
	 * @param ps
	 * @param conn
	 * @return: void
	 */
	public static void closed(PreparedStatement ps,Connection conn){
		try{
			if(ps!=null){
				ps.close();
				if(conn!=null){
					conn.close();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
  • 2创建学生的实体类(和表中字段一一对应)

public class StudentInfo {
	private Integer stuId;
	private String stuCode;
	private String stuName;
	private int stuAge;
	private String stuSex;
    
	public Integer getStuId() {
		return stuId;
	}
	public void setStuId(Integer stuId) {
		this.stuId = stuId;
	}
	public String getStuCode() {
		return stuCode;
	}
	public void setStuCode(String stuCode) {
		this.stuCode = stuCode;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	public String getStuSex() {
		return stuSex;
	}
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}
	@Override
	public String toString() {
		return "StudentInfo [stuId=" + stuId + ", stuCode=" + stuCode + ", stuName=" + stuName + ", stuAge=" + stuAge
				+ ", stuSex=" + stuSex + "]";
	}
	
}
  • 3抽取出对数据库进行操作的内容(这里我没有使用接口实现,直接使用类实现)

public class StudentInfoDao {
	/**
	 * @方法名: findAll
	 * @方法说明: 查询学生信息
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日上午11:30:38
	 * @return
	 * @return: List<StudentInfo>
	 */
	public List<StudentInfo> findAll(){
		Connection conn = StudentInfoUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from t_studentInfo";
		//创建集合
		List<StudentInfo> list = new ArrayList<StudentInfo>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				StudentInfo studentInfo = new StudentInfo();
				//获取结果集中的数据
				studentInfo.setStuCode(rs.getString("stu_code"));
				studentInfo.setStuName(rs.getString("stu_name"));
				studentInfo.setStuAge(rs.getInt("stu_age"));
				studentInfo.setStuSex(rs.getString("stu_sex"));
				/*	//创建集合
			List<StudentInfo> list = new ArrayList<StudentInfo>();*/
				//将数据存入集合
				list.add(studentInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			StudentInfoUtil.closed(ps, conn);
		}
		return list;
	}
	
	/**
	 * @方法名: addStudentInfo
	 * @方法说明: 新增学生信息
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日上午11:48:57
	 * @param studentInfo
	 * @return
	 * @return: int
	 */
	public int addStudentInfo(StudentInfo studentInfo){
		Connection conn = StudentInfoUtil.getConn();
		PreparedStatement ps = null;
		String sql = "insert into t_studentInfo(stu_code,stu_name,stu_age,stu_sex) values(?,?,?,?)";
		int result = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, studentInfo.getStuCode());
			ps.setString(2, studentInfo.getStuName());
			ps.setInt(3, studentInfo.getStuAge());
			ps.setString(4, studentInfo.getStuSex());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			StudentInfoUtil.closed(ps, conn);
		}
		return result;
	}
	
	/**
	 * @方法名: deleteStudentInfo
	 * @方法说明: 根据ID删除学生信息
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日上午11:58:45
	 * @param stuId
	 * @return
	 * @return: int
	 */
	public int deleteStudentInfo(int stuId){
		Connection conn = StudentInfoUtil.getConn();
		PreparedStatement ps = null;
		String sql = "delete from t_studentInfo where stu_id = ?";
		int result = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, stuId);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			StudentInfoUtil.closed(ps, conn);
		}
		return result;
	}
	
	/**
	 * @方法名: updateStudentInfo
	 * @方法说明: 修改学生信息
	 * @作者: LiYuHui
	 * @邮箱:1327711913@qq.com
	 * @日期: 2020年6月8日下午12:08:01
	 * @param studentInfo
	 * @return
	 * @return: int
	 */
	public int updateStudentInfo(StudentInfo studentInfo){
		Connection conn = StudentInfoUtil.getConn();
		PreparedStatement ps = null;
		String sql = "update t_studentInfo set stu_name=?,stu_sex=? where stu_id=?";
		int result = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, studentInfo.getStuName());
			ps.setString(2, studentInfo.getStuSex());
			ps.setInt(3, studentInfo.getStuId());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			StudentInfoUtil.closed(ps, conn);
		}
		return result;
	}
}
  • 4测试(测试相应的内容,把多行注释删掉即可)

public class Test {
	public static void main(String[] args) {
		//查询
		StudentInfoDao studentInfoDao = new StudentInfoDao();
		List<StudentInfo> list = studentInfoDao.findAll();
		Iterator<StudentInfo> it = list.iterator();
		while(it.hasNext()){
			StudentInfo stu = it.next();
			System.out.println(stu);
		}
		
		/*//新增
		StudentInfoDao studentInfoDao = new StudentInfoDao();
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStuCode("10010");
		studentInfo.setStuName("小丽");
		studentInfo.setStuAge(18);
		studentInfo.setStuSex("女");
		studentInfoDao.addStudentInfo(studentInfo);*/
		
		/*//删除
		StudentInfoDao studentInfoDao = new StudentInfoDao();
		studentInfoDao.deleteStudentInfo(11);*/
		
		/*//修改
		StudentInfoDao studentInfoDao = new StudentInfoDao();
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStuName("春丽");
		studentInfo.setStuSex("女");
		studentInfo.setStuId(8);
		studentInfoDao.updateStudentInfo(studentInfo);*/
	}
}

当然,实现的前提是数据库和数据库驱动都要弄好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值