增删改查 操作数据库的类(dao)

来源:互联网 发布:windows版rar 编辑:程序博客网 时间:2024/05/23 11:58

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.model.Student1;

public class StudentDAO {
/**
* 对数据库业务操作
*/
Connection conn = null;
PreparedStatement prmt = null;
Statement stamt = null;
ResultSet rs = null;

/**
* // 1.增加
*/

public void addStudent(Student1 stu) {

   conn = ConnectionManager.getConn();
   System.out.println("StudentDAO.java中addStudent得到的连接是:" + conn);
   String sql = "insert into student values(?)";
   try {
    prmt = conn.prepareStatement(sql);
    prmt.setString(1, stu.getName());
    System.out.println("StudentDAO.java中addStudentstu.getName的值: "
      + stu.getName());
    prmt.executeUpdate();

   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);

    }
    if (conn != null) {
     ConnectionManager.close(conn);

    }

   }
}

/**
* // 2.修改
*/

public void updateStudent(Student1 stu) {
   conn = ConnectionManager.getConn();
   String sql = "update student set name=? where id=?";
   try {
    prmt = conn.prepareStatement(sql);
    prmt.setString(1, stu.getName());
    prmt.setInt(2, stu.getId());

    System.out.println("StudentDAO列表中的updateStudent的id值: "
      + stu.getId());
    System.out.println("StudentDAO列表中的updateStudent的name值: "
      + stu.getName());

    prmt.executeUpdate();

    System.out.println("StudentDAO列表中prmt.executeUpdate()后的值: "
      + stu.getId());
    System.out.println("StudentDAO列表中prmt.executeUpdate()后的值: "
      + stu.getName());

   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);

    }
    if (conn != null) {
     ConnectionManager.close(conn);

    }
   }
}

/**
* // 3.通过编号删除 或 // 4.删除所有
*/
public void del_Student(String id) {
   conn = ConnectionManager.getConn();
   String sql = "delete from student where id = ?";

   System.out.println("StudentDAO列表中del_Student中的sql try前的值:" + sql);

   try {
    prmt = conn.prepareStatement(sql);

    System.out.println("StudentDAO列表中del_Student中的sql try后的值:" + sql);
   
    prmt.setString(1, id);

    System.out
      .println("StudentDAO列表中del_Student中的prmt.executeUpdate()前id的值:"
        + id);
    System.out.println("prmt.executeUpdate()前sql的值:" + sql);
   
    prmt.executeUpdate();

    System.out
      .println("StudentDAO列表中del_Student中的prmt.executeUpdate()后id的值:"
        + id);
    System.out.println("prmt.executeUpdate()前sql的值:" + sql);
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);

    }
    if (conn != null) {
     ConnectionManager.close(conn);
    }

   }

}

/**
* // 5.模糊查询
*/

public List selectStudent(String name) {
   conn = ConnectionManager.getConn();
   List list = new ArrayList();
   String sql = "select * from student where name like '%" + name + "%'";
   System.out.println("sql" + sql);
   try {
    prmt = conn.prepareStatement(sql);
    rs = (ResultSet) prmt.executeQuery();
    while (rs.next()) {
     Student1 student = new Student1();
     student.setId(rs.getInt(1));
     student.setName(rs.getString(2));
     list.add(student);
     System.out.println("list.add(student)" + list.add(student));
    }
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);

    }
    if (conn != null) {
     ConnectionManager.close(conn);
    }
   }
   return list;
}

/**
* 6.根椐学生的id查出所在的学生进得单独的修改
*/

public Student1 get_Student(String id) {
   conn = ConnectionManager.getConn();
   Student1 student = new Student1();
   String sql = "select * from student where id = ?";

   try {
    prmt = conn.prepareStatement(sql);
    prmt.setString(1, id);
    rs = (ResultSet) prmt.executeQuery();
    while (rs.next()) {
     student.setId(rs.getInt(1));
     student.setName(rs.getString(2));
    }
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);
    }
    if (conn != null) {
     ConnectionManager.close(conn);
    }
   }

   return student;

}

/**
* 7.用在student_list判断是否查询所有学生(如果list==null)即是查询所有的学生
*/

public List selectAllStudent() {
   conn = ConnectionManager.getConn();
   List list = new ArrayList();
   String sql = "select * from student";

   try {
    prmt = conn.prepareStatement(sql);
    rs = (ResultSet) prmt.executeQuery();
    while (rs.next()) {
     Student1 student = new Student1();
     student.setId(rs.getInt(1));
     student.setName(rs.getString(2));

     System.out.println("StudentDAO列表中selectAllStudent的id值:"
       + rs.getInt(1));
     System.out.println("StudentDAO列表中selectAllStudent的id值:"
       + rs.getString(2));

     list.add(student);

     System.out.println("StudentDAO列表中selectAllStudent的list值:"
       + list);
    }

   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);

    }
    if (conn != null) {
     ConnectionManager.close(conn);
    }

   }
   return list;

}

/**
* 8、删除学生
*/

public void deleteStudent(Student1 stu) {
   conn = ConnectionManager.getConn();
   String sql = "delete from student where id = ?";
   System.out.println("要删除学生的编号是:" + stu.getId());

   try {
    prmt = conn.prepareStatement(sql);
    prmt.setInt(1, stu.getId());
    prmt.executeUpdate();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    if (prmt != null) {
     ConnectionManager.close(prmt);
    }
    if (conn != null) {
     ConnectionManager.close(conn);
    }
   }
}

}