首页>代码>使用jdbc实现各种类型查询数据>/JDBCSelect/src/com/bdqn/dao/StudentDao.java
package com.bdqn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.print.attribute.HashAttributeSet;

import com.bdqn.entity.ClassS;
import com.bdqn.entity.Student;
import com.bdqn.util.DbUtil;

public class StudentDao {
	
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	public List<Student> SelecttAll(){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student";
		Object[] obj = {};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Student> SelectAllId(int id){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where id=?";
		Object[] obj = {id};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Student> SelectAllName(String name){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where name=?";
		Object[] obj = {name};
		ps = DbUtil.executeSql(sql, obj);
		System.out.println(ps);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Student> SelectAllIdName(int id,String name){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where id=? and name=?";
		Object[] obj = {id,name};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Student> SelectAllIdNameClassId(int id,String name,int classId){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where id=? and name=? and classid=?";
		Object[] obj = {id,name,classId};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Student> SelectAllStudent(Student stu){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where id=? and name=? and classid=?";
		Object[] obj = {stu.getId(),stu.getName(),stu.getClassid()};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public Map<Student,ClassS> SelectAllMapCname(){
		Map<Student,ClassS> map = new HashMap<Student,ClassS>();
		String sql = "select s.id,s.name,c.cname from student s,class c where s.classid = c.id";
		Object[] obj = {};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				ClassS cs = new ClassS();
				cs.setCname(rs.getString("cname"));
				map.put(stu, cs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	public Hashtable<Student, ClassS> SelectAllHashtableCname(){
		Hashtable<Student,ClassS> hash = new Hashtable<Student,ClassS>();
		String sql = "select s.id,s.name,c.cname from student s,class c where s.classid = c.id";
		Object[] obj = {};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				ClassS cs = new ClassS();
				cs.setCname(rs.getString("cname"));
				hash.put(stu, cs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hash;
	}
	
	public List<Student> SelectLikeName(String name){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where name like ?";
		String name1 = "%" + name + "%";
		Object [] obj = {name1};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setClassid(rs.getInt("classid"));
				list.add(stu);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	public List<Student> SelectLikeName(Student stu){
		List<Student> list = new ArrayList<Student>();
		String sql = "select * from student where name like ?";
		String name1 = "%" + stu.getName() + "%";
		Object [] obj = {name1};
		ps = DbUtil.executeSql(sql, obj);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu1 = new Student();
				stu1.setId(rs.getInt("id"));
				stu1.setName(rs.getString("name"));
				stu1.setClassid(rs.getInt("classid"));
				list.add(stu1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	public Map<Student,ClassS> SelectLikeMapSname(String name){
		Map<Student,ClassS> map = new HashMap<Student,ClassS>();
		String sql = "select s.id,s.name,c.cname from student s,class c where s.classid = c.id and s.name like ?";
		String name1 = "%" + name + "%";
		Object[] obj = {name1};
		ps = DbUtil.executeSql(sql, obj);
		System.out.println(ps);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				ClassS cs = new ClassS();
				cs.setCname(rs.getString("cname"));
				map.put(stu, cs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	public Map<Student,ClassS> SelectLikeMapSname(Student stu){
		Map<Student,ClassS> map = new HashMap<Student,ClassS>();
		String sql = "select s.id,s.name,c.cname from student s,class c where s.classid = c.id and s.name like ?";
		String name = "%" + stu.getName() + "%";
		Object[] obj = {name};
		ps = DbUtil.executeSql(sql, obj);
		System.out.println(ps);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu1 = new Student();
				stu1.setId(rs.getInt("id"));
				stu1.setName(rs.getString("name"));
				ClassS cs = new ClassS();
				cs.setCname(rs.getString("cname"));
				map.put(stu, cs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	public Map<Student,ClassS> SelectLikeMapCname(String name){
		Map<Student,ClassS> map = new HashMap<Student,ClassS>();
		String sql = "select s.id,s.name,c.cname from student s,class c where s.classid = c.id and c.cname like ?";
		String name1 = "%" + name + "%";
		Object[] obj = {name1};
		ps = DbUtil.executeSql(sql, obj);
		System.out.println(ps);
		try {
			rs = ps.executeQuery();
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				ClassS cs = new ClassS();
				cs.setCname(rs.getString("cname"));
				map.put(stu, cs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
}
最近下载更多
贺和hhhhh  LV1 2021年12月5日
又没抢到饭  LV1 2021年8月13日
ommmly  LV5 2021年6月9日
zmy001  LV11 2020年10月25日
skystory  LV11 2020年9月25日
xzyxzyxzy  LV7 2020年4月15日
768412219  LV1 2019年12月30日
jaonsang  LV25 2019年11月3日
wcxuhqw  LV5 2019年10月29日
冷曦1369  LV9 2019年9月23日
最近浏览更多
Mhdpig  LV3 2023年10月22日
zt3631877  LV9 2023年10月20日
cc900118  LV17 2022年12月5日
1358849392  LV21 2022年10月25日
yjh120470  LV3 2022年9月24日
是你爸爸啊100  LV5 2022年8月8日
林间听风  LV10 2022年6月7日
侗哥阿辉  LV1 2022年3月21日
2469095052  LV8 2021年12月30日
贺和hhhhh  LV1 2021年12月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友