欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【作业Day02】JDBC-MySQL的增删改查功能实现

程序员文章站 2022-05-08 08:44:02
...

JDBC-MySQL的增删改查功能实现

通过navicat连接上mysql以后
先建立一个新数据库

格式如下
【作业Day02】JDBC-MySQL的增删改查功能实现
【作业Day02】JDBC-MySQL的增删改查功能实现

1.建立properties文件

便于后期快速修改,降低耦合度

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_db?rewriteBatchedStatements=true
username=root
password=123456

2.建立domain

package com.wzbc.jdbc.domain;

public class Student {
	Integer id;
	String name;
	Integer age;
	public Integer 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 Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	
}

3.建立DAO接口

package com.wzbc.jdbc.dao;

import java.util.List;

import com.wzbc.jdbc.domain.Student;

public interface IStudentDao {
	/**
	 * 保存一个学生
	 */
	public void save(Student stu);
	/**
	 * 删除学生
	 */
	public void delete(int id);
	/**
	 * 更新一个学生信息
	 */
	public void update(int id,Student stu);
	/**
	 * 获取指定学生
	 */
	public Student get(int id);


4.建立JDBCUtils.java文件(工具类)

package com.wzbc.jdbc.util;

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

import javax.sql.DataSource;


import com.alibaba.druid.pool.DruidDataSourceFactory;

public class JdbcUtil {

	public static DataSource ds = null;
	static {
		try {
			//1.加载配置文件
			Properties p = new Properties();
			FileInputStream in = new FileInputStream("resource/db.properties");
			p.load(in);
			ds = DruidDataSourceFactory.createDataSource(p);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static DataSource getDataSource() {
		return ds;
	}
	
	
	public static Connection getConn() {
		try {
			// 2.连接数据
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 关闭资源 
	 */
	public static void close(Connection conn,Statement st,ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}

5.接口实现类

package com.wzbc.jdbc.dao.impl;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.wzbc.jdbc.dao.IStudentDao;
import com.wzbc.jdbc.domain.Student;
import com.wzbc.jdbc.util.JdbcUtil;

public class StudentDaoImpl implements IStudentDao {
	@Override
	public void save(Student stu) {
		String sql = "insert into student(name,age) values(?,?)";
		QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
		try {
			qr.update(sql, stu.getName(),stu.getAge());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void delete(int id) {
		// 3.创建语句
		String sql = "delete from student where id = ?";
	}
	@Override
	public void update(int id, Student stu) {
		String sql = "update student set name=?, age=? where id =? ";
	}
	@Override
	public Student get(int id) {
		String sql = "select * from student where id = ?";
		QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
		try {
			return   qr.query(sql, new BeanHandler<Student>(Student.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}

6.测试类

package com.wzbc.jdbc.test;

import java.util.List;

import org.junit.Test;

import com.wzbc.jdbc.dao.IStudentDao;
import com.wzbc.jdbc.dao.impl.StudentDaoImpl;
import com.wzbc.jdbc.domain.Student;

public class StudentDaoTest {
	@Test
	public void save() {
		// 创建一个学生
		Student stu = new Student();
		stu.setName("李白100");
		stu.setAge(21);
		// 把学生保存到数据库当中
		IStudentDao dao = new StudentDaoImpl();
		dao.save(stu);
	}

	@Test
	public void delete() {
		IStudentDao dao = new StudentDaoImpl();
		dao.delete(10);
	}

	@Test
	public void update() {
		IStudentDao dao = new StudentDaoImpl();
		// 创建一个学生
		Student stu = new Student();
		stu.setName("鲁班7");
		stu.setAge(7);	
		dao.update(9, stu);
		
	}
	@Test
	public void get(){
		IStudentDao dao = new StudentDaoImpl();
		 Student stu = dao.get(2);
		 System.out.println(stu);
	}
}

相关标签: 作业