- 编写一个JdbcUtil的工具类,完成以下功能:
- 将装载数据驱动程序的代码封装在静态初始化代码块中。
- 将获得数据库连接的代码封装在一个静态方法getConnection()中,返回Connection实例。
- 编写一个方法static void close(Connection conn, Statement stmt, ResultSet rs){ }可以关闭各种数据资源,本方法要作null检查和异常处理。
close(conn,null,null);
JdbcUtil.class
import java.sql.*;
public class JdbcUtil {
private final static String URL = "jdbc:mysql://localhost:3306/qxq";//你的url
private final static String USER = "root"; //数据库账户
private final static String PASSWORD = "******";//你的数据库密码
private final static String CLASSNAME = "com.mysql.cj.jdbc.Driver";
static {
try {
Class.forName(CLASSNAME);
} catch (ClassNotFoundException e) {
System.out.println("注册数据库失败 " + e);
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("连接数据库失败");
}
return null;
}
public static void close(Connection con, Statement statement, ResultSet resultSet) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("连接关闭失败");
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
System.out.println("创建语句关闭失败");
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println("执行语句资源关闭失败");
}
}
}
public static void main(String[] args) {
Connection connection = JdbcUtil.getConnection();
System.out.println(connection);
JdbcUtil.close(connection,null,null);
}
}
- 编写一个EmployeeDao类(DAO是Data Access Object的意思),完成以下增删改修功能,都是实例方法(非静态):
- 编写一个Employee JavaBean类,用于封装scott.emp表的数据,Employee类的属性与scott.emp的列相对应,类型要相符。注意JavaBean类要有一个默认的构造器,属性有getter/setter方法。
编写一个:List<Employee> findAll( )方法查询所有的员工信息,并将信息保存在列表中。
编写一个:Employee findByPK(Integer empno)方法将指定员工号的员工信息查询出来,封装成一个Employee对象返回。若找不到员工返回null。
…. Where empno=8888
While(rs.next()){
Return new Employee(…..)
}
编写一个:void save(Employee emp)方法将一个员工对象保存到scott.emp表中。若添加失败本方法抛出一个DataAccessExcption自定义异常。
编写一个:void update(Employee emp)方法更新参数指定的员工,若更新失败本方法抛出一个DataAccessExcption自定义异常。
编写一个:void delete(Integer empno)方法删除指定员工号的员工记录,若删除失败本方法抛出一个DataAccessExcption自定义异常。
Delete employee where empno=8888
Stmt.executeUpdate(sql)
所有的CRUD方法通必须用JUnit4做单元测试;在模块中新建一个test测试源码目录在里面相应的包中创建测试类, 如:com.lanqiao.dao.EmployeeDaoTest 进行测试。
StudentDao.class
public class StudentDao {
public List<Student> findAll() {
String sql="select * from student";
ArrayList<Student> students = new ArrayList<>();
Connection connection = JdbcUtil.getConnection();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String studentName = resultSet.getString("studentName");
int age = resultSet.getInt("age");
int studentnumber = resultSet.getInt("studentnumber");
Student student = new Student(studentName, age, studentnumber);
students.add(student);
}
} catch (SQLException e) {
System.out.println("创建语句失败");
}
return students;
}
public Student findById(String name) {
String sql="select * from student where studentName='"+name+"'";
System.out.println(sql);
Connection connection = JdbcUtil.getConnection();
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String studentName = resultSet.getString("studentName");
int age = resultSet.getInt("age");
int studentnumber = resultSet.getInt("studentnumber");
return new Student(studentName, age, studentnumber);
}
} catch (SQLException e) {
System.out.println("创建语句失败");
}
return null;
}
public int save(Student student) {
String sql = "insert into student values(?,?,?)";
try (Connection connection = JdbcUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)
) {
int i=1;
preparedStatement.setString(i++, student.getName());
if (String.valueOf(student.getAge()) == null) {
preparedStatement.setInt(i++, Types.INTEGER);
} else {
preparedStatement.setInt(i++, student.getAge());
}
if (String.valueOf(student.getNumber()) == null) {
preparedStatement.setInt(i++, Types.INTEGER);
} else {
preparedStatement.setInt(i++, student.getNumber());
}
final int ii= preparedStatement.executeUpdate();
return ii;
} catch (SQLException e) {
System.out.println("添加学生失败");
}
return 0;
}
public void update(Student student) {
StringBuffer sql = new StringBuffer("update student set ");
if (String.valueOf(student.getAge()) != null) {
sql.append("age="+student.getAge()+",");
}
if (String.valueOf(student.getNumber()) != null) {
sql.append("studentnumber="+student.getNumber()+",");
}
sql.deleteCharAt(sql.length() - 1);
sql.append(" where studentName=" + student.getName());
try (Connection connection = JdbcUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(String.valueOf(sql));
) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("更新信息失败");
}
}
}
Test.class
import static org.junit.Assert.*;
public class StudentDaoTest {
StudentDao studentDao;
@Before
public void setUp() throws Exception {
this.studentDao= new StudentDao();
}
@After
public void tearDown() throws Exception {
this.studentDao=null;
}
@Test
public void findAll() {
this.studentDao.findAll().forEach(System.out::println);
}
@Test
public void findById() {
System.out.println(this.studentDao.findById("张三"));
}
@Test
public void save() {
Student student1 = new Student();
student1.setName("鸭鸭");
System.out.println(this.studentDao.save(student1));
}
@Test
public void update() {
this.studentDao.update( new Student("鸭鸭", 40, 222));
}
}