发表于: 2017-04-05 22:35:36

4 1605


今天完成的事情:

17.编写DAO,注意写清楚Interface和Impl,注意遵守命名规范。

自己的理解:用实现类实现接口,完成对数据库的增删改查。需要有一个User类定义变量和方法,一个UserDao接口,一个UserDaoImpl实现,一个对JDBC进行简单封装的开源工具类库。一个测试类。

Test.java

package com.ptteng.test;

import com.ptteng.dao.StudentDao;
import com.ptteng.dao.impl.StudentDaoImpl;
import com.ptteng.entity.Student;

import java.sql.SQLException
public class Test {

    public static void main(String args[]) throws SQLException  {
        Student stu = new Student();
        StudentDao dao = new StudentDaoImpl();
        /**
         * 调用add方法,插入数据
        stu.setStu_name("张三");
        stu.setStu_school("天天玩");
        dao.addStudent(stu);
        System.out.println("插入成功!");
        */
        stu.setStu_name("张四");
        stu.setId(123);
        dao.updateStudent(stu);
        System.out.println("修改成功!");
        
   //     dao.selectStudent();
        
    }
}

DBUtil.java

package com.ptteng.connection
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
    private static Connection conn = null;
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/itxzy";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    static {
        try {
            Class.forName(DRIVER);
            conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }

    /**
     * 数据库关闭操作
     */
    public void close() throws Exception{
        if (this.conn != null){
            try {
                this.conn.close();
            }catch (Exception e){
                throw e;
            }
        }
    }
}

Student.java

package com.ptteng.entity;
public class Student {
    private int id;
    private String stu_name;
    private String stu_school;

    public int getId() {
        return id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public String getStu_school() {
        return stu_school;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public void setStu_school(String stu_school) {
        this.stu_school = stu_school;
    }
}

StudentDao.java

package com.ptteng.dao
import com.ptteng.entity.Student
import java.sql.SQLException;
import java.util.List;
public interface StudentDao {

    public void addStudent(Student stu) throws SQLException;

    public void updateStudent(Student stu) throws SQLException;

    public int deleteStudent(int id) throws SQLException;

    public List<Student> selectStudent() throws SQLException;

    public Student findStudentById();
}

StudentDaoImpl.java

package com.ptteng.dao.impl
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List
import com.ptteng.connection.DBUtil;
import com.ptteng.dao.StudentDao;
import com.ptteng.entity.Student;
public class StudentDaoImpl implements StudentDao {
    @Override
    public void addStudent(Student stu) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "INSERT into student (stu_name,stu_school) values(?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,stu.getStu_name());
            pst.setString(2,stu.getStu_school());
            pst.execute();
    }

    @Override
    public void updateStudent(Student stu) throws SQLException{
        Connection conn = DBUtil.getConnection();
        String sql = "update student set stu_name = ? , stu_school = ? where id= ?";
        System.out.println(sql);
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,stu.getStu_name());
            pst.setString(2,stu.getStu_school());
            pst.setInt(3,stu.getId());
            pst.executeUpdate();
    }

    @Override
    public int deleteStudent(int id) throws SQLException{
        Connection conn = DBUtil.getConnection();
        String sql = "delete from student where id= ? ";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,id);
            pst.execute();
            return id;
    }

    @Override
    public List<Student> selectStudent() throws SQLException{
        Connection conn = DBUtil.getConnection();
        String sql = "select * from student";
        PreparedStatement pst = conn.prepareStatement(sql);
        Student stu = new Student();
        ResultSet rs = pst.executeQuery();
        List<Student> list = new ArrayList<Student>();
        
        while (rs.next()){
            stu.setStu_name(rs.getString(1));
            stu.setId(rs.getInt(2));
            stu.setStu_school(rs.getString(3));
            list.add(stu);
            
        }
        return list;
    }

    @Override
    public Student findStudentById() {
        return null;
    }
}

遇到的问题:在写Dao时,开始的sql数据时写死的。最后弄了很长时间改传递的值和sql,实现了动态传值增删改查。有时候遇到问题,百度里的解答也不都是对的,浪费了很多时间,昨天定的计划大大延迟了,看来要加快步伐才行。

明天的计划:学习Spring、Junit.

收获:用JDBC实现了动态增删改查


返回列表 返回列表
评论

    分享到