|
@@ -0,0 +1,235 @@
|
|
|
|
|
+package com.sf._03_jdbc_dao.dao.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.sf._03_jdbc_dao.dao.StudentDAO;
|
|
|
|
|
+import com.sf._03_jdbc_dao.domain.Student;
|
|
|
|
|
+
|
|
|
|
|
+import java.sql.*;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * JDBC 对于DAO 层实现类
|
|
|
|
|
+ */
|
|
|
|
|
+public class StudentDAOImpl implements StudentDAO {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void save(Student student) {
|
|
|
|
|
+ //1 加载驱动
|
|
|
|
|
+ Connection connection = null;
|
|
|
|
|
+ PreparedStatement statement = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
+ //2 获取连接对象
|
|
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
|
|
|
|
|
+ //3 创建语句对象
|
|
|
|
|
+// statement = connection.createStatement();
|
|
|
|
|
+ statement = connection.prepareStatement("insert into t_student (name,age,email) values(?,?,?)");
|
|
|
|
|
+ statement.setString(1,student.getName());
|
|
|
|
|
+ statement.setInt(2,student.getAge());
|
|
|
|
|
+ statement.setString(3,student.getEmail());
|
|
|
|
|
+ //4 执行sql 语句
|
|
|
|
|
+ // statement 拼接字符串 可能会产生sql 注入问题
|
|
|
|
|
+ // li -si
|
|
|
|
|
+ statement.executeUpdate();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ // 判断连接对象不为空再关闭资源
|
|
|
|
|
+ if(connection!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(statement!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ statement.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void update(Student student) {
|
|
|
|
|
+ //1 加载驱动
|
|
|
|
|
+ Connection connection = null;
|
|
|
|
|
+ PreparedStatement statement = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
+ //2 获取连接对象
|
|
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
|
|
|
|
|
+ //3 创建语句对象
|
|
|
|
|
+// statement = connection.createStatement();
|
|
|
|
|
+ statement = connection.prepareStatement("update t_student set name=?,age=?,email=? where id=?");
|
|
|
|
|
+ statement.setString(1,student.getName());
|
|
|
|
|
+ statement.setInt(2,student.getAge());
|
|
|
|
|
+ statement.setString(3,student.getEmail());
|
|
|
|
|
+ statement.setLong(4,student.getId());
|
|
|
|
|
+ //4 执行sql 语句
|
|
|
|
|
+ // statement 拼接字符串 可能会产生sql 注入问题
|
|
|
|
|
+ // li -si
|
|
|
|
|
+ statement.executeUpdate();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ // 判断连接对象不为空再关闭资源
|
|
|
|
|
+ if(connection!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(statement!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ statement.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void delete(Long id) {
|
|
|
|
|
+ //1 加载驱动
|
|
|
|
|
+ Connection connection = null;
|
|
|
|
|
+ PreparedStatement statement = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
+ //2 获取连接对象
|
|
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
|
|
|
|
|
+ //3 创建语句对象
|
|
|
|
|
+// statement = connection.createStatement();
|
|
|
|
|
+ statement = connection.prepareStatement("delete from t_student where id=?");
|
|
|
|
|
+ statement.setLong(1,id);
|
|
|
|
|
+ //4 执行sql 语句
|
|
|
|
|
+ // statement 拼接字符串 可能会产生sql 注入问题
|
|
|
|
|
+ // li -si
|
|
|
|
|
+ statement.executeUpdate();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ // 判断连接对象不为空再关闭资源
|
|
|
|
|
+ if(connection!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(statement!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ statement.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Student get(Long id) {
|
|
|
|
|
+ Connection connection = null;
|
|
|
|
|
+ PreparedStatement statement = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
+ //2 获取连接对象
|
|
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
|
|
|
|
|
+ //3 创建语句对象
|
|
|
|
|
+// statement = connection.createStatement();
|
|
|
|
|
+ statement = connection.prepareStatement("select * from t_student where id=?");
|
|
|
|
|
+ statement.setLong(1,id);
|
|
|
|
|
+ //4 执行sql 语句
|
|
|
|
|
+ // statement 拼接字符串 可能会产生sql 注入问题
|
|
|
|
|
+ // li -si
|
|
|
|
|
+ ResultSet resultSet = statement.executeQuery();
|
|
|
|
|
+ // resultSet 结果集, 从数据库当中查询数据都在resultSet 中
|
|
|
|
|
+ // 利用.next 判断是否查询到了数据.如果next为true 表示有数据 false 表示没有数据
|
|
|
|
|
+ if(resultSet.next()){
|
|
|
|
|
+ // 有数据,创建出来一个Student 对象 , 把数据库当中数据设置到student 对象当中
|
|
|
|
|
+ Student student = new Student();
|
|
|
|
|
+ student.setId(resultSet.getLong("id"));
|
|
|
|
|
+ student.setName(resultSet.getString("name"));
|
|
|
|
|
+ student.setEmail(resultSet.getString("email"));
|
|
|
|
|
+ student.setAge(resultSet.getInt("id"));
|
|
|
|
|
+ return student;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ // 判断连接对象不为空再关闭资源
|
|
|
|
|
+ if(connection!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(statement!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ statement.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Student> list() {
|
|
|
|
|
+ Connection connection = null;
|
|
|
|
|
+ PreparedStatement statement = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
|
+ //2 获取连接对象
|
|
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
|
|
|
|
|
+ //3 创建语句对象
|
|
|
|
|
+// statement = connection.createStatement();
|
|
|
|
|
+ statement = connection.prepareStatement("select * from t_student ");
|
|
|
|
|
+ //4 执行sql 语句
|
|
|
|
|
+ // statement 拼接字符串 可能会产生sql 注入问题
|
|
|
|
|
+ // li -si
|
|
|
|
|
+ ResultSet resultSet = statement.executeQuery();
|
|
|
|
|
+ List<Student> list = new ArrayList<>();
|
|
|
|
|
+ // resultSet 结果集, 从数据库当中查询数据都在resultSet 中
|
|
|
|
|
+ // 利用.next 判断是否查询到了数据.如果next为true 表示有数据 false 表示没有数据
|
|
|
|
|
+ while (resultSet.next()){
|
|
|
|
|
+ // 有数据,创建出来一个Student 对象 , 把数据库当中数据设置到student 对象当中
|
|
|
|
|
+ Student student = new Student();
|
|
|
|
|
+ student.setId(resultSet.getLong("id"));
|
|
|
|
|
+ student.setName(resultSet.getString("name"));
|
|
|
|
|
+ student.setEmail(resultSet.getString("email"));
|
|
|
|
|
+ student.setAge(resultSet.getInt("id"));
|
|
|
|
|
+ // 把学生信息添加到集合当中
|
|
|
|
|
+ list.add(student);
|
|
|
|
|
+ }
|
|
|
|
|
+ return list;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ // 判断连接对象不为空再关闭资源
|
|
|
|
|
+ if(connection!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(statement!=null){
|
|
|
|
|
+ try {
|
|
|
|
|
+ statement.close();
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|