当前位置:Gxlcms > 数据库问题 > jdbc访问数据库

jdbc访问数据库

时间:2021-07-01 10:21:17 帮助过:4人阅读

7.在src/main/java下创建接口StudentDao,包名(com.mycompany.dao),目录结构如图所示

技术分享


8.接口StudentDao的内容如下

  1. package com.mycompany.dao;
  2. import java.util.List;
  3. import com.mycompany.domain.Student;
  4. /**
  5.  * 学生DAO接口
  6.  * @author Administrator
  7.  *
  8.  */
  9. public interface StudentDao {
  10. /**
  11.  * 查询所有学生
  12.  * @return 学生列表
  13.  */
  14. public List<Student> query();
  15. /**
  16.  * 添加学生
  17.  * @param student
  18.  */
  19. public void save(Student student);
  20. }


9.在src/main/java下创建接口StudentDao的实现类StudentDaoImpl,包名(com.mycompany.dao.impl),目录如图所示

技术分享


10.StudentDaoImpl实现类的内容如下

  1. package com.mycompany.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.mycompany.dao.StudentDao;
  8. import com.mycompany.domain.Student;
  9. import com.mycompany.util.JDBCUtil;
  10. public class StudentDaoImpl implements StudentDao {
  11. public List<Student> query() {
  12. Connection connection = null;
  13. PreparedStatement preparedStatement = null;
  14. ResultSet resultSet = null;
  15. List<Student> students = new ArrayList<Student>();
  16. String sql = "select * from student";
  17. try {
  18. connection = JDBCUtil.getConnection();
  19. preparedStatement = connection.prepareStatement(sql);
  20. resultSet = preparedStatement.executeQuery();
  21. while(resultSet.next()){
  22. int sid = resultSet.getInt("sid");
  23. String name = resultSet.getString("name");
  24. Student student = new Student(sid,name);
  25. students.add(student);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally{
  30. JDBCUtil.release(resultSet, preparedStatement, connection);
  31. }
  32. return students;
  33. }
  34. public void save(Student student) {
  35. Connection connection = null;
  36. PreparedStatement preparedStatement = null;
  37. ResultSet resultSet = null;
  38. List<Student> students = new ArrayList<Student>();
  39. String sql = "insert into student(name) values(?)";
  40. try {
  41. connection = JDBCUtil.getConnection();
  42. preparedStatement = connection.prepareStatement(sql);
  43. preparedStatement.setString(1, student.getName());
  44. preparedStatement.executeUpdate();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. } finally{
  48. JDBCUtil.release(resultSet, preparedStatement, connection);
  49. }
  50. }
  51. }


11.在src/main/resources下创建db.properties属性文件,存储数据库连接信息

技术分享


12.db.properties属性文件的内容如下

  1. jdbc.url=jdbc:mysql:///test
  2. jdbc.user=root
  3. jdbc.password=
  4. jdbc.driverClass=com.mysql.jdbc.Driver


13.在src/test/java下创建测试类StudentDaoImplTest,包名(com.mycompany.dao.impl),目录结构如图所示

技术分享


14.测试类StudentDaoImplTest的内容如下

  1. package com.mycompany.dao.impl;
  2. import java.util.List;
  3. import org.junit.Test;
  4. import com.mycompany.dao.StudentDao;
  5. import com.mycompany.domain.Student;
  6. public class StudentDaoImplTest {
  7. @Test
  8. public void testQuery(){
  9. StudentDao studentDao = new StudentDaoImpl();
  10. List<Student> students = studentDao.query();
  11. for (Student student : students) {
  12. System.out.println(student);
  13. }
  14. }
  15. @Test
  16. public void testSave(){
  17. Student student = new Student("test save");
  18. StudentDao studentDao = new StudentDaoImpl();
  19. studentDao.save(student);
  20. }
  21. }

技术分享

本文出自 “素颜” 博客,谢绝转载!

jdbc访问数据库

标签:jdbc访问数据库

人气教程排行