sdent类:
package com.myschool.entity;
public class student{
private int studentno;
private String loginpwd;
private int studentno;
private String loginpwd;
private String studentname;
private int sex;
private int gradeid;
private String phone;
private String address;
private String borndate;
private int gradeid;
private String phone;
private String address;
private String borndate;
private String email;
private String IdentityCard;
grade grade=new 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 getLoginpwd() {
return loginpwd;
}
public void setLoginpwd(String loginpwd) {
this.loginpwd = loginpwd;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getGradeid() {
return gradeid;
}
public void setGradeid(int gradeid) {
this.gradeid = gradeid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBorndate() {
return borndate;
}
public void setBorndate(String borndate) {
this.borndate = borndate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIdentityCard() {
return IdentityCard;
}
public void setIdentityCard(String identityCard) {
IdentityCard = identityCard;
}
//带参构造
private String IdentityCard;
grade grade=new 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 getLoginpwd() {
return loginpwd;
}
public void setLoginpwd(String loginpwd) {
this.loginpwd = loginpwd;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getGradeid() {
return gradeid;
}
public void setGradeid(int gradeid) {
this.gradeid = gradeid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBorndate() {
return borndate;
}
public void setBorndate(String borndate) {
this.borndate = borndate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIdentityCard() {
return IdentityCard;
}
public void setIdentityCard(String identityCard) {
IdentityCard = identityCard;
}
//带参构造
public student(int studentno, String loginpwd, String studentname, int sex,
int gradeid, String phone, String address, String borndate,
String email, String identityCard) {
super();
this.studentno = studentno;
this.loginpwd = loginpwd;
this.studentname = studentname;
this.sex = sex;
this.gradeid = gradeid;
this.phone = phone;
this.address = address;
this.borndate = borndate;
this.email = email;
IdentityCard = identityCard;
}
//无参构造
public student(){
}
int gradeid, String phone, String address, String borndate,
String email, String identityCard) {
super();
this.studentno = studentno;
this.loginpwd = loginpwd;
this.studentname = studentname;
this.sex = sex;
this.gradeid = gradeid;
this.phone = phone;
this.address = address;
this.borndate = borndate;
this.email = email;
IdentityCard = identityCard;
}
//无参构造
public student(){
}
}
grade类:
package com.myschool.entity;
import java.util.List;
public class grade {
private int gradeid;
private String gradename;
public List<student> list;
public List<student> getList() {
return list;
}
public void setList(List<student> list) {
this.list = list;
}
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) {
super();
this.gradeid = gradeid;
this.gradename = gradename;
}
private int gradeid;
private String gradename;
public List<student> list;
public List<student> getList() {
return list;
}
public void setList(List<student> list) {
this.list = list;
}
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) {
super();
this.gradeid = gradeid;
this.gradename = gradename;
}
//无参构造
public grade(){
}
public grade(){
}
}
BaseDao接口:
package com.myschool.daos;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static final String STRING="com.mysql.jdbc.Driver";
private static final String URL="jdbc:mysql:///myschool";
private static final String U_STRING="root";
private static final String PASSWORD="";
Connection con=null;
PreparedStatement Statement;
ResultSet executeQuery ;
public Connection getdConnection() throws Exception
{
Class.forName(STRING);
//con.isClosed:判断是否是关闭的
if (con==null||con.isClosed()) {
con = DriverManager.getConnection(URL, U_STRING,PASSWORD);
}
return con;
}
//Object ...objects这种参数定义是在不确定方法参数的情况下的一种多态表现形式。
//即这个方法可以传递多个参数,这个参数的个数是不确定的。这样你在方法体中需要相应的做些处理。
//因为Object是基类,所以使用Object ...objects这样的参数形式,允许一切继承自Object的对象作为参数。
//这种方法在实际中应该还是比较少用的。
public int getDao(String string,Object...objects) throws Exception
{
getdConnection();
Statement = con.prepareStatement(string);
for (int i = 1; i <=objects.length; i++) {
Statement.setObject(i,objects[i-1]);
}
int execute = Statement.executeUpdate();
return execute;
}
public ResultSet getDaos(String string,Object...objects) throws Exception
{
getdConnection();
Statement = con.prepareStatement(string);
for (int i = 1; i <=objects.length; i++) {
Statement.setObject(i,objects[i-1]);
}
executeQuery = Statement.executeQuery();
return executeQuery;
}
public void close() throws Exception
{
if (executeQuery!=null) {
executeQuery.close();
}
if (Statement!=null) {
Statement.close();
}
if (con!=null) {
con.close();
}
}
private static final String URL="jdbc:mysql:///myschool";
private static final String U_STRING="root";
private static final String PASSWORD="";
Connection con=null;
PreparedStatement Statement;
ResultSet executeQuery ;
public Connection getdConnection() throws Exception
{
Class.forName(STRING);
//con.isClosed:判断是否是关闭的
if (con==null||con.isClosed()) {
con = DriverManager.getConnection(URL, U_STRING,PASSWORD);
}
return con;
}
//Object ...objects这种参数定义是在不确定方法参数的情况下的一种多态表现形式。
//即这个方法可以传递多个参数,这个参数的个数是不确定的。这样你在方法体中需要相应的做些处理。
//因为Object是基类,所以使用Object ...objects这样的参数形式,允许一切继承自Object的对象作为参数。
//这种方法在实际中应该还是比较少用的。
public int getDao(String string,Object...objects) throws Exception
{
getdConnection();
Statement = con.prepareStatement(string);
for (int i = 1; i <=objects.length; i++) {
Statement.setObject(i,objects[i-1]);
}
int execute = Statement.executeUpdate();
return execute;
}
public ResultSet getDaos(String string,Object...objects) throws Exception
{
getdConnection();
Statement = con.prepareStatement(string);
for (int i = 1; i <=objects.length; i++) {
Statement.setObject(i,objects[i-1]);
}
executeQuery = Statement.executeQuery();
return executeQuery;
}
public void close() throws Exception
{
if (executeQuery!=null) {
executeQuery.close();
}
if (Statement!=null) {
Statement.close();
}
if (con!=null) {
con.close();
}
}
}
接口:
package com.myschool.daos;
import java.util.List;
import com.myschool.entity.grade;
import com.myschool.entity.student;
import com.myschool.entity.student;
public interface studentdao {
public List<student> getstudent() throws Exception;
public grade getgrade(String gradeid) throws Exception;
}
public grade getgrade(String gradeid) throws Exception;
}
实现类:
package com.myschool.dao.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import com.myschool.daos.BaseDao;
import com.myschool.daos.studentdao;
import com.myschool.entity.grade;
import com.myschool.entity.student;
import com.myschool.daos.studentdao;
import com.myschool.entity.grade;
import com.myschool.entity.student;
public class studnetdaoimpl extends BaseDao implements studentdao{
private ResultSet rs=null;
private ResultSet rs=null;
@Override
public grade getgrade(String gradename) throws Exception {
List<student> list1=new ArrayList<student>();
String sql="select s.*,g.GradeName from student as s,grade as g where s.GradeId=g.GradeId and g.gradename=?";
Object[] obj={gradename};
rs=executeQuery(sql, obj);
grade grade=null;
if (rs!=null) {
while(rs.next()){
grade=new grade();
student student=new student();
student.setStudentno(rs.getInt("studentno"));
student.setStudentname(rs.getString("studentname"));
list1.add(student);
grade.setGradename(rs.getString("gradename"));
}
grade.setList(list1);
}
return grade;
}
public grade getgrade(String gradename) throws Exception {
List<student> list1=new ArrayList<student>();
String sql="select s.*,g.GradeName from student as s,grade as g where s.GradeId=g.GradeId and g.gradename=?";
Object[] obj={gradename};
rs=executeQuery(sql, obj);
grade grade=null;
if (rs!=null) {
while(rs.next()){
grade=new grade();
student student=new student();
student.setStudentno(rs.getInt("studentno"));
student.setStudentname(rs.getString("studentname"));
list1.add(student);
grade.setGradename(rs.getString("gradename"));
}
grade.setList(list1);
}
return grade;
}
@Override
public List<student> getstudent() throws Exception {
List<student> list=new ArrayList<student>();
String sql="select * from student as s,grade as g where s.GradeId=g.GradeId";
rs = executeQuery(sql);
if (rs!=null) {
while(rs.next()){
student stu=new student();
grade grade=new grade();
stu.setStudentno(rs.getInt("studentno"));
stu.setStudentname(rs.getString("studentname"));
grade.setGradename(rs.getString("gradename"));
stu.setGrade(grade);
list.add(stu);
}
}
return list;
}
}
main方法:
package ui;
import java.util.List;
import com.myschool.dao.impl.studnetdaoimpl;
import com.myschool.daos.studentdao;
import com.myschool.entity.grade;
import com.myschool.entity.student;
import com.myschool.daos.studentdao;
import com.myschool.entity.grade;
import com.myschool.entity.student;
public class ui {
static studentdao dao=new studnetdaoimpl();
public static void getstudent() throws Exception{
List<student> list=dao.getstudent();
for (student student : list) {
System.out.println(student.getStudentno()+" "+student.getStudentname()+" "+student.getGrade().getGradename());
}
}
public static void main(String[] args) throws Exception {
getstudent();
grade grade=dao.getgrade("S1");
for (student stu : grade.getList()) {
System.out.println(stu.getStudentno()+" "+stu.getStudentname()+" "+grade.getGradename());
}
public static void getstudent() throws Exception{
List<student> list=dao.getstudent();
for (student student : list) {
System.out.println(student.getStudentno()+" "+student.getStudentname()+" "+student.getGrade().getGradename());
}
}
public static void main(String[] args) throws Exception {
getstudent();
grade grade=dao.getgrade("S1");
for (student stu : grade.getList()) {
System.out.println(stu.getStudentno()+" "+stu.getStudentname()+" "+grade.getGradename());
}
}
}