题目:

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());
}
}
}

Myschool试题的更多相关文章

  1. accp7.0优化MySchool数据库设计内测笔试题总结

    1) 在SQL Server 中,为数据库表建立索引能够(C ). 索引:是SQL SERVER编排数据的内部方法,是检索表中数据的直接通道 建立索引的作用:大大提高了数据库的检索速度,改善数据库性能 ...

  2. 优化MySchool数据库设计之【巅峰对决】

    优化MySchool数据库设计 之独孤九剑 船舶停靠在港湾是很安全的,但这不是造船的目的 By:北大青鸟五道口原玉明老师 1.学习方法: 01.找一本好书 初始阶段不适合,可以放到第二个阶段,看到知识 ...

  3. .NET面试题系列[8] - 泛型

    “可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...

  4. 关于面试题 Array.indexof() 方法的实现及思考

    这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...

  5. 对Thoughtworks的有趣笔试题实践

    记得2014年在网上看到Thoughtworks的一道笔试题,当时觉得挺有意思,但是没动手去写.这几天又在网上看到了,于是我抽了一点时间写了下,我把程序运行的结果跟网上的答案对了一下,应该是对的,但是 ...

  6. 从阿里巴巴笔试题看Java加载顺序

    一.阿里巴巴笔试题: public class T implements Cloneable { public static int k = 0; public static T t1 = new T ...

  7. JAVA面试题

    在这里我将收录我面试过程中遇到的一些好玩的面试题目 第一个面试题:ABC问题,有三个线程,工作的内容分别是打印出"A""B""C",需要做的 ...

  8. C++常考面试题汇总

    c++面试题 一 用简洁的语言描述 c++ 在 c 语言的基础上开发的一种面向对象编程的语言: 应用广泛: 支持多种编程范式,面向对象编程,泛型编程,和过程化编程:广泛应用于系统开发,引擎开发:支持类 ...

  9. .NET面试题系列[4] - C# 基础知识(2)

    2 类型转换 面试出现频率:主要考察装箱和拆箱.对于有笔试题的场合也可能会考一些基本的类型转换是否合法. 重要程度:10/10 CLR最重要的特性之一就是类型安全性.在运行时,CLR总是知道一个对象是 ...

随机推荐

  1. python Mock 示例

    在Python3.x中,mock已经被集成到了unittest单元测试框架中,所以,可以直接使用. 可能你和我初次接触这个概念的时候会有这样的疑问:把要测的东西都模拟掉了还测试什么呢? 但在,实际生产 ...

  2. 【ElasticSearch】查询优化

    一.背景 每周统计接口耗时,发现耗时较长的前几个接口tp5个9都超过了1000ms. 经过分析慢查询的原因是ES查询耗时太长导致的 二.设计方案 1.问题定位 查询功能使用不当导致慢查询 索引设计存在 ...

  3. 1+X证书学习日志——javascript基础

    js javascript js的组成: ECMAscript DOM BOM js放置的位置 <script></script> <script src="路 ...

  4. ffmpeg 把视频转换为图片

    ffmpeg -i "Tail of Hope.mp4" -r 1 -q:v 2 -f image2 pic-%03d.jpeg

  5. Linux--重要文件

    目录 /etc/resolv.conf /etc/host /etc/sysconfig/network /etc/fstab /etc/rc.local /etc/profile /etc/bash ...

  6. C#中多集合组合实现

    C#中多集合组合使用方法,主要用到 Enumerable.Join方法 var skuInfoList = new Dictionary<int, List<long>>(); ...

  7. Eclipse上Maven环境配置使用

    1. 安装配置Maven: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven. Maven下载地址: http://maven. ...

  8. nodejs express+nodemon 实现自动刷新 (热更新)

    1.全局安装 nodemon npm i nodemon -g -f 2. 在项目中安装nodemon npm i nodemon -f 3.使用nodemon 在express项目中 运行nodem ...

  9. 04-JavaScript的操作

    本篇主要介绍获取元素的方法.操作元素.数组和字符串的操作方法.定时器和封闭函数.以及贪吃蛇案例: 一.获取元素的方法 1.document.getElementById:可以使用内置对象documen ...

  10. nginx的stream模块和upstream模块

    nginx7层调度方式 使用upstream模块定义集群名称和节点地址 定义在server字段之外httpd字段之内 upstream staticweb { server 172.17.0.2; # ...