Encapsulation of Java database connection, close, add, delete, modify and query methods (2) implementation of add, delete, modify and query

Keywords: SQL Database Java JDBC

According to the article "JDBC util tool class of Java database connection, close, add, delete and modify query method encapsulation (1)", it realizes the connection, close, add, delete and modify query tool class. Then we will analyze it further.

1. First, edit an entity class and put it in the entry package according to the column name and type of the table in the database. This paper takes Student as an example.

package entry;

public class Student{

 private int id;

 private String name;

 private int gradeId;

 public Student() {

}
 
 public Student (int id, String name, int gradeId) {
this.id = id;
this.name = name;
this.gradeId = gradeId;
}
 public void setId(int id) {
	    this.id = id;
 }

 public int getId() {
		return id;
}

 public void setName(String name) {
	    this.name = name;
 }

 public String getName() {
		return name;
}

 public void setGradeId(int gradeId) {
	    this.gradeId = gradeId;
 }

 public int getGradeId() {
		return gradeId;
}

 public String toString() {
 return "Student [id=" + id + ", name=" + name + ", gradeId=" + gradeId + 
"]";
}
}

1. First, write a BaseDao interface, as shown in the figure. BaseDao uses generics, which can match different entity classes. This paper takes Student as an example

/***
 * The basic class defines the interface methods. Other interfaces can add, delete, modify and query methods through inheritance
 * Defined as generic to accommodate all reference types
 * @author Administrator
 *
 * @param <T>
 */
public interface BaseDao<T> {
	void insert(T args);
	void update(T args);
	void delete(int id);
	T getById(int id);
	List<T> getAll();

}

2. Write class interface, take Student as an example

public interface IStudentDao extends BaseDao<Student>{

}

Creating the implementation class of IStudentDao interface will automatically generate the add, delete, modify and query methods, as shown in the figure

package daoImp;

import java.util.List;

import dao.IStudentDao;
import entry.Student;

public class StudentDaoImp implements IStudentDao {

	@Override
	public void insert(Student args) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Student args) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Student getById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Student> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

}

Now we will supplement, delete, modify and check the method completely

public class StudentDaoImp implements IStudentDao{

	@Override
	public void insert(Student args) {
		// TODO Auto-generated method stub
		String sql="insert into student values(null,?,?)";
		JDBCUtil.executeUpdate(sql, args.getName(),args.getGradeId());
	}

	@Override
	public void update(Student args) {
		// TODO Auto-generated method stub
		String sql="update student set name=?,gradeId=? where id=?";
		JDBCUtil.executeUpdate(sql, args.getName(),args.getGradeId(),args.getId());
		
	}
	

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
	String sql="delete from student where id=?";
	JDBCUtil.executeUpdate(sql, id);
	}

	@Override
	public Student getById(int id) {
		// TODO Auto-generated method stub
		String sql="select * from student where id=?";
		List<Map<String,Object>> list=JDBCUtil.Query(sql, id);
		for(Map<String,Object> map:list){
			String name=(String) map.get("name");
			String gradeId=(String) map.get("gradeId");
		 return new User(id, name, gradeId);
			
			
			
		}
		return null;
		
	}

	@Override
	public List<Student> getAll() {
		// TODO Auto-generated method stub
		String sql="select * from student";
		List<Map<String,Object>> list=JDBCUtil.Query(sql);
		List<Student> li=new ArrayList<>();
		for(Map<String,Object> map:list){
			Integer id=Integer.parseInt(map.get("id").toString());
			String name=(String) map.get("name");
			String gradeId=(String) map.get("gradeId");
			 Student stu=new User(id,name,gradeId);
			 li.add(stu);
		}
		
			
		return li;
      }
}
	

 

Posted by plsanders on Mon, 06 Jan 2020 08:26:20 -0800