1.在Navicat数据库管理工具里创建3个表,t_user用户表,t_score学生成绩表,t_course表。
2.源代码
1.实体类比如学生类,用户类以此类推。
package com.pzhu.pojo;
public class Student {
private int uid;
private String uname;
private int highmath;
private int lisansx;
private int english;
private int caozuo;
private double zfavg;
private int cjdbh;
private double xfavg;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getHighmath() {
return highmath;
}
public void setHighmath(int highmath) {
this.highmath = highmath;
}
public int getLisansx() {
return lisansx;
}
public void setLisansx(int lisansx) {
this.lisansx = lisansx;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getCaozuo() {
return caozuo;
}
public void setCaozuo(int caozuo) {
this.caozuo = caozuo;
}
public double getZfavg() {
return zfavg;
}
public void setZfavg(double zfavg) {
this.zfavg = zfavg;
}
public int getCjdbh() {
return cjdbh;
}
public void setCjdbh(int cjdbh) {
this.cjdbh = cjdbh;
}
public double getXfavg() {
return xfavg;
}
public void setXfavg(double xfavg) {
this.xfavg = xfavg;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + caozuo;
result = prime * result + cjdbh;
result = prime * result + english;
result = prime * result + highmath;
result = prime * result + lisansx;
result = prime * result + uid;
result = prime * result + ((uname == null) ? 0 : uname.hashCode());
long temp;
temp = Double.doubleToLongBits(xfavg);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(zfavg);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (caozuo != other.caozuo)
return false;
if (cjdbh != other.cjdbh)
return false;
if (english != other.english)
return false;
if (highmath != other.highmath)
return false;
if (lisansx != other.lisansx)
return false;
if (uid != other.uid)
return false;
if (uname == null) {
if (other.uname != null)
return false;
} else if (!uname.equals(other.uname))
return false;
if (Double.doubleToLongBits(xfavg) != Double
.doubleToLongBits(other.xfavg))
return false;
if (Double.doubleToLongBits(zfavg) != Double
.doubleToLongBits(other.zfavg))
return false;
return true;
}
@Override
public String toString() {
return "Student [uid=" + uid + ", uname=" + uname + ", highmath="
+ highmath + ", lisansx=" + lisansx + ", english=" + english
+ ", caozuo=" + caozuo + ", zfavg=" + zfavg + ", cjdbh="
+ cjdbh + ", xfavg=" + xfavg + "]";
}
public Student(int uid, String uname, int highmath, int lisansx,
int english, int caozuo, double zfavg, int cjdbh, double xfavg) {
super();
this.uid = uid;
this.uname = uname;
this.highmath = highmath;
this.lisansx = lisansx;
this.english = english;
this.caozuo = caozuo;
this.zfavg = zfavg;
this.cjdbh = cjdbh;
this.xfavg = xfavg;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
2.编写dao层函数。
package com.pzhu.dao.impl;
//JDBC必须导入的包
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.pzhu.dao.StudentDao;
import com.pzhu.pojo.Student;
import com.pzhu.pojo.User;
public class StudentDaoImpl implements StudentDao{
@Override
public User checkLoginDao(String uname, String pwd) {
//定义JDBC对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//定义用户对象并初始化,存储查询结果
User u=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/niit?useUnicode=true&characterEncoding=utf8","root","1234");
//创建SQL语句
String sql="select * from t_user where uname=? and pwd=?";
//预处理
ps=conn.prepareStatement(sql);
//添加占位符
ps.setString(1, uname);
ps.setString(2, pwd);
//执行SQL语句
rs=ps.executeQuery();
//遍历
while(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
}
} catch (Exception e) {
// TODO: handle exception