题目:

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. np.minimum()与tf.minimum()的用法

    总结:二者用法一致.a=np.array([[[[10,8,3,9],[5,6,7,8]]],[[[1,2,3,4],[5,6,7,8]]],[[[1,2,3,4],[5,6,7,8]]]] )pri ...

  2. head 与 tail

    head head [-n] 数字『文件』 显示前面n行 例如 head -n 3 test 显示 test 文件的前 3 行,也可以写作 head -3 test 比较有趣的是 -n 后面的数字,可 ...

  3. 【开发工具】- 推荐一款好用的文本编辑器[Sublime Text]

    作为一个程序员除了IDE外,文本编辑器也是必不可少的一个开发工具.之前一直在用的是NotePad++.EditPlus,这两款编辑器,但是总感觉差点什么,昨天在知乎上看到有人推荐Sublime Tex ...

  4. ES6数组方法总结

    关于数组中forEach() .map().filter().reduce().some().every()的总结 1. forEach() let array = [1,2,3,4]; array. ...

  5. 使用vue-router在页面之间传值及接收值

    第一页 点击去第二页的时候进行传值直接贴代码看: <template> <div id="app"> <div><router-link ...

  6. procfs、debugfs

    procfs 是进程文件系统(file system) 的缩写,包含一个伪文件系统,用于通过内核访问进程信息 这个文件系统通常被挂载到 /proc 目录.由于 /proc 不是一个真正的文件系统,它也 ...

  7. 应用在App Store上被拒重新提交审核流程指南

    1. 打开地址: https://itunesconnect.apple.com 2. 输入APPID和密码后,再输入绑定手机后的验证码. 3. 查看“”我的APP“”,如果显示拒绝,可能需打开Mac ...

  8. C# 里面将字符作为代码计算,主要是运算符号的计算

    DataTable dt = new DataTable(); string str="1+2*(5+3)+3-1"; dt.Compute(str, "false&qu ...

  9. Linux命令——whiptail交互式shell脚本对话框

    转自:交互式shell脚本对话框----whiptail指令 当你在linux环境下setup软件的时候就会有相应的对话框让你输入.虽然我们已经习惯了这种交互的方法,但是如果有一种直观的界面来输入是不 ...

  10. curl请求https资源的时候出现400

    在nginx上配置了一个新的域名, 习惯性地用curl请求看看有没有配置错误 因为是https的, 所以 $curl 'https://test.test.com/' -x 127.0.0.1:443 ...