Myschool试题

题目:

1.查询所有学生记录,包含年级名称
2.查询S1年级下的学生记录

 

 一、com.myschool.dao

  1 BaseDao

复制代码
package com.myschool.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    /**
     * 数据库连接字符串
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/myschool?useUniCode=true&characterEncoding=utf-8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    private Connection conn;
    private PreparedStatement statement;
    private ResultSet rs;
    /**
     * 获取连接的方法
     */
    public Connection getConnection() {
        try {
            Class.forName(DRIVER);
            //判断连接对象是否为空或者当前连接对象是否是isClosed()已经关闭的
            if(conn==null||conn.isClosed()){
                conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;

    }

    /**
     * 增删改
     * 
     * @throws Exception
     */
    public int executeUpdate(String sql, Object... obj) throws Exception {
        // 获取连接
        getConnection();
        // 获取PreparedStatement对象
        
        statement = conn.prepareStatement(sql);
        // 循环加载参数
        for (int i = 1; i <= obj.length; i++) {
            statement.setObject(i, obj[i-1]);
        }
        // 执行SQL
        int count = statement.executeUpdate();

        return count;
    }

    /**
     * 查询
     * @throws SQLException 
     */
    public ResultSet executeQuery(String sql, Object... obj) throws Exception {
        // 获取连接
        getConnection();
        // 获取PreparedStatement对象
        statement = conn.prepareStatement(sql);
        // 循环加载参数
        for (int i = 1; i <= obj.length; i++) {
            
            statement.setObject(i, obj[i-1]);
        }
        rs = statement.executeQuery();
        return rs;
    }

    /**
     * 关闭连接
     * @throws Exception 
     */
    public void closeResource() throws Exception {
        if(rs!=null){
            rs.close();
        }
        if(statement!=null){
            statement.close();
        }
        if(conn!=null){
            //关闭连接
            conn.close();
        }
    }
}
复制代码

  2 IGradeDao

复制代码
package com.myschool.dao;

import com.myschool.entity.Grade;

public interface IGradeDao {
    /**
     * 查询年级下的所有学生信息
     * 一个年级有多名学生
     */
    public Grade getStudentByGrade(String gradeName) throws Exception;
}
复制代码

  3 IStudentDao

复制代码
package com.myschool.dao;

import java.util.List;

import com.myschool.entity.Student;

public interface IStudentDao {
    /**
     * 查询所有学生信息,包含年级名称
     * 一个学生对应一个年级
     * 
     */
    public List<Student> getAllStudent() throws Exception;
    
    
}
复制代码

  二、com.myschool.dao.impl

  3.1 IGradeDaoImpl

复制代码
package com.myschool.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.myschool.dao.BaseDao;
import com.myschool.dao.IGradeDao;
import com.myschool.entity.Grade;
import com.myschool.entity.Student;

public class IGradeDaoImpl extends BaseDao implements IGradeDao{

    @Override
    public Grade getStudentByGrade(String gradeName) throws Exception {
        Grade grade=new Grade();
        String sql="SELECT * FROM Student,Grade WHERE Student.GradeId=Grade.GradeId AND GradeName=?";
        ResultSet rs = executeQuery(sql, gradeName);
        if(rs!=null){
            
            while (rs.next()) {
                
                //获取年级信息
                grade.setGradeName(rs.getString("gradeName"));
                //获取学生信息
                Student student=new Student();
                student.setGradeId(rs.getInt("gradeId"));
                student.setStudentName(rs.getString("StudentName"));
                student.setStudentNo(rs.getInt("studentNo"));
                //将查询出来的学生信息添加到集合当中
                grade.getStulist().add(student);
            }
            
        }
        return grade;
    }

}
复制代码

 

  3.2 IStudentDaoImpl

复制代码
package com.myschool.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.myschool.dao.BaseDao;
import com.myschool.dao.IStudentDao;
import com.myschool.entity.Grade;
import com.myschool.entity.Student;

public class IStudentDaoImpl extends BaseDao implements IStudentDao{

    @Override
    public List<Student> getAllStudent() throws Exception {
        List<Student> list=new ArrayList<Student>();
        String sql="SELECT * FROM Student,Grade WHERE Student.GradeId=Grade.GradeId";
        ResultSet rs = executeQuery(sql);
        if(rs!=null){
            while(rs.next()){
                //获取学生信息
                Student student=new Student();
                student.setGradeId(rs.getInt("gradeId"));
                student.setStudentName(rs.getString("StudentName"));
                student.setStudentNo(rs.getInt("studentNo"));
                //获取的就是当前学生的年级信息
                Grade grade=new Grade();
                grade.setGradeName(rs.getString("gradeName"));
                student.setGrade(grade);
                
                
                list.add(student);
            }
        }
        closeResource();
        return list;
    }
}
复制代码

  三、com.myschool.entity

  4.1 Grade

复制代码
package com.myschool.entity;

import java.util.ArrayList;
import java.util.List;


/**
 * Grade实体类
 * @author FLC
 *
 */
public class Grade {
    private int gradeId;
    private String gradeName;
    private List<Student> stulist=new ArrayList<Student>();
    public List<Student> getStulist() {
        return stulist;
    }
    public void setStulist(List<Student> stulist) {
        this.stulist = stulist;
    }
    public int getGradeId() {
        return gradeId;
    }
    public void setGradeId(int gradeId) {
        this.gradeId = gradeId;
    }
    public String getGradeName() {
        return gradeName;
    }
    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }
    public Grade(int gradeId, String gradeName) {
        this.gradeId = gradeId;
        this.gradeName = gradeName;
    }
    public Grade() {
        
    }
}
复制代码

 

  4.2 Student

复制代码
package com.myschool.entity;
/**
 * Student实体类
 * @author FLC
 *
 */
public class Student {
    private int studentNo;
    private String studentName;
    private int gradeId;

    
    //只要拿到学生就能拿到对应学生的年级信息
    private Grade grade;
    
    public Grade getGrade() {
        return grade;
    }
    public void setGrade(Grade grade) {
        this.grade = grade;
    }
    public int getStudentNo() {
        return studentNo;
    }
    public void setStudentNo(int studentNo) {
        this.studentNo = studentNo;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getGradeId() {
        return gradeId;
    }
    public void setGradeId(int gradeId) {
        this.gradeId = gradeId;
    }
    public Student(int studentNo, String studentName, int gradeId) {
        this.studentNo = studentNo;
        this.studentName = studentName;
        this.gradeId = gradeId;
    }
    public Student() {
        
    }
    
}
复制代码

  四、com.myschool.service

  5.1 IGradeService

复制代码
package com.myschool.service;

import com.myschool.entity.Grade;

public interface IGradeService {
    /**
     * 查询年级下的所有学生信息
     * 一个年级有多名学生
     */
    public Grade getStudentByGrade(String gradeName) throws Exception;
}
复制代码

  5.2 IStudentService

复制代码
package com.myschool.service;

import java.util.List;

import com.myschool.entity.Student;

public interface IStudentService {
    /**
     * 查询所有学生信息,包含年级名称
     * 
     * 
     */
    public List<Student> getAllStudent() throws Exception;
}
复制代码

  六、com.myschool.service.impl

  6.1 IGradeServiceImpl

复制代码
package com.myschool.service.impl;

import com.myschool.dao.IGradeDao;
import com.myschool.dao.impl.IGradeDaoImpl;
import com.myschool.entity.Grade;
import com.myschool.service.IGradeService;

public class IGradeServiceImpl implements IGradeService{
    
    IGradeDao gradeDao=new IGradeDaoImpl();

    @Override
    public Grade getStudentByGrade(String gradeName) throws Exception {
        // TODO Auto-generated method stub
        return gradeDao.getStudentByGrade(gradeName);
    }

}
复制代码

  6.2 IStudentServiceImpl

复制代码
package com.myschool.service.impl;

import java.util.List;

import com.myschool.dao.IStudentDao;
import com.myschool.dao.impl.IStudentDaoImpl;
import com.myschool.entity.Student;
import com.myschool.service.IStudentService;

public class IStudentServiceImpl implements IStudentService{
    //创建Dao层对象
    IStudentDao studentDao=new IStudentDaoImpl();
    
    
    @Override
    public List<Student> getAllStudent() throws Exception {
        return studentDao.getAllStudent();
    }

}
复制代码

  六、com.myschool.ui

  7.1 MyMian

复制代码
package com.myschool.ui;

import java.util.List;

import com.myschool.entity.Grade;
import com.myschool.entity.Student;
import com.myschool.service.IGradeService;
import com.myschool.service.IStudentService;
import com.myschool.service.impl.IGradeServiceImpl;
import com.myschool.service.impl.IStudentServiceImpl;

public class MyMian {
    //创建Service对象
    static IStudentService studentService=new IStudentServiceImpl();
    static IGradeService gradeService=new IGradeServiceImpl();
    public static void main(String[] args) throws Exception {
        /*System.out.println("============查询所有学生信息============");
        List<Student> allStudent = studentService.getAllStudent();
        for (Student student : allStudent) {
            System.out.println("学生姓名:"+student.getStudentName()+"\t学生年级:"+student.getGradeId()+"\t年级名称:"+student.getGrade().getGradeName());
            
        }*/
        Grade grade = gradeService.getStudentByGrade("S1");
        for (Student stu : grade.getStulist()) {
            System.out.println(stu.getStudentName());
        }
    }
}

转载于:https://www.cnblogs.com/mayuan01/p/10945014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值