package DAO;
import Instrument.GetConnection;
import User.CetStu;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDAO <T> {
private Class<T> TClass = null;
{
Type GSC = this.getClass().getGenericSuperclass();
Type[] TypeArgs = ((ParameterizedType)GSC).getActualTypeArguments();
TClass = (Class<T>) TypeArgs[0];
}
public int UpdateTwo(Connection conn, String sql, Object...args) {
PreparedStatement ps = null;
int ExecuteRS = 0;
try {
ps = conn.prepareStatement(sql);
for (int index = 0; index < args.length; index++) {
ps.setObject(index + 1, args[index]);
}
ExecuteRS = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
GetConnection.Close(null, ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
return ExecuteRS;
}
public List<T> TableSearch( String sql, Object... arg) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
ArrayList<T> TList = null;
try {
conn = GetConnection.getConnection();
ps = conn.prepareStatement(sql);
for (int index = 0; index < arg.length; ++index) {
ps.setObject(index + 1, arg[index]);
}
resultSet = ps.executeQuery();
ResultSetMetaData rsmd = resultSet.getMetaData();
int ColumnNums = rsmd.getColumnCount();
TList = new ArrayList<>();
while (resultSet.next()) {
T temp = TClass.getDeclaredConstructor().newInstance();
for (int index = 0; index < ColumnNums; ++index) {
Object ColumnValue = resultSet.getObject(index + 1);
String ColumnLabel = rsmd.getColumnLabel(index + 1);
Field field = temp.getClass().getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(temp, ColumnValue);
}
TList.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
GetConnection.CloseResource(conn, ps, resultSet);
} catch (Exception e) {
e.printStackTrace();
}
}
return TList;
}
public <E> E GetOneValue(E data, Connection conn, String sql, Object...args){
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = conn.prepareStatement(sql);
for( int index = 0 ; index < args.length; ++index){
ps.setObject(index+1,args[index]);
}
rs = ps.executeQuery();
if(rs.next()){
return (E) rs.getObject(1);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
GetConnection.CloseResource(null,ps,rs);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return null;
}
}
package DAO;
import User.CetStu;
import java.sql.Connection;
import java.util.Date;
import java.util.List;
public interface CET {
void InCET(Connection conn, CetStu cet);
void DeleteById(Connection conn,int FlowId );
void update(Connection conn,CetStu cet);
CetStu QueryStu(Connection conn,int FlowId);
List<CetStu> QueryAll(Connection conn);
long NumOfTable(Connection conn);
Date MaxBirth(Connection conn);
}
package Bean;
import DAO.*;
import User.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
public class CetDAO extends BaseDAO<CetStu> implements CET{
@Override
public void InCET(Connection conn, CetStu cet) {
String sql ;
sql = "Insert into cet ( Species, IDCard, ExamCard, StudentName, Location, Grade , InTime ) " +
"values(?,?,?,?,?,?,?)";
UpdateTwo(conn,sql,cet.getSpecies(),cet.getIDCard(),cet.getExamCard(),cet.getStudentName(),
cet.getLocation(),cet.getGrade(),cet.getInTime());
}
@Override
public void DeleteById(Connection conn, int FlowId) {
String sql = "Delete from cet where FlowId = ?";
UpdateTwo(conn,sql,FlowId);
}
@Override
public void update(Connection conn, CetStu cet) {
String sql = "Update cet set Species = ? , IDCard = ? , ExamCard = ? , " +
"StudentName, = ? Location = ? , Grade = ? , InTime = ? where FlowId = ?";
UpdateTwo(conn,sql,cet.getSpecies(),cet.getIDCard(),cet.getExamCard(),cet.getStudentName(),
cet.getLocation(),cet.getGrade(),cet.getInTime(),cet.getFLowId());
}
@Override
public CetStu QueryStu(Connection conn, int FlowId) {
String sql = "select * from cet where FlowId = ?";
return (CetStu) TableSearch(sql,FlowId);
}
@Override
public List<CetStu> QueryAll(Connection conn) {
String sql = "select * from cet ";
return TableSearch(sql);
}
@Override
public long NumOfTable(Connection conn) {
String sql = "select count(*) from cet";
long i = 0;
return GetOneValue(i,conn,sql);
}
@Override
public Date MaxBirth(Connection conn) {
String sql = "select Max(InTime) from cet";
Date d = null;
return GetOneValue(d,conn,sql);
}
}
private String IDCard;
private String ExamCard;
private String StudentName;
private String Location;
public CetStu(){
super();
}
public CetStu( int species, int grade, Timestamp inTime, String IDCard, String examCard, String studentName, String location) {
Species = species;
Grade = grade;
InTime = inTime;
this.IDCard = IDCard;
ExamCard = examCard;
StudentName = studentName;
Location = location;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CetStu cetStu = (CetStu) o;
return FLowId == cetStu.FLowId &&
Species == cetStu.Species &&
Grade == cetStu.Grade &&
Objects.equals(InTime, cetStu.InTime) &&
Objects.equals(IDCard, cetStu.IDCard) &&
Objects.equals(ExamCard, cetStu.ExamCard) &&
Objects.equals(StudentName, cetStu.StudentName) &&
Objects.equals(Location, cetStu.Location);
}
@Override
public int hashCode() {
return Objects.hash(FLowId, Species, Grade, InTime, IDCard, ExamCard, StudentName, Location);
}
public void setFLowId(int FLowId) {
this.FLowId = FLowId;
}
public void setSpecies(int species) {
Species = species;
}
public void setGrade(int grade) {
Grade = grade;
}
public void setInTime(Timestamp inTime) {
InTime = inTime;
}
public void setIDCard(String IDCard) {
this.IDCard = IDCard;
}
public void setExamCard(String examCard) {
ExamCard = examCard;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public void setLocation(String location) {
Location = location;
}
public int getFLowId() {
return FLowId;
}
public int getSpecies() {
return Species;
}
public int getGrade() {
return Grade;
}
public Timestamp getInTime() {
return InTime;
}
public String getIDCard() {
return IDCard;
}
public String getExamCard() {
return ExamCard;
}
public String getStudentName() {
return StudentName;
}
public String getLocation() {
return Location;
}
@Override
public String toString() {
return "CetStu{ " +
"FLowId=" + FLowId +
", Species=" + Species +
", Grade=" + Grade +
", InTime=" + InTime +
", IDCard='" + IDCard + '\'' +
", ExamCard='" + ExamCard + '\'' +
", StudentName='" + StudentName + '\'' +
", Location='" + Location + '\'' +
'}';
}
}
import java.util.List;
public class CetDAOTest {
private CetDAO CDAO = new CetDAO();
@Test
public void testOne(){
Connection conn = null;
try {
conn = GetConnection.getConnection();
Date date = CDAO.MaxBirth(conn);
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
GetConnection.Close(conn,null);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
@Test
public void testTwo(){
Connection conn = null;
try {
conn = GetConnection.getConnection();
List<CetStu> AllStu= CDAO.QueryAll(conn);
for(CetStu cs : AllStu){
System.out.println(cs);
}
} catch (Exception e) {
e.printStackTrace();
}try {
GetConnection.Close(conn,null);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Test
public void testThree(){
Connection conn = null;
try {
conn = GetConnection.getConnection();
CetStu cs = new CetStu(4,561, new Timestamp(new Date().getTime()),
"4564","119010","时君博","广东省深圳市");
CDAO.InCET(conn,cs);
}catch (Exception e){
e.printStackTrace();
} finally {
try {
GetConnection.Close(conn,null);
}catch (Exception e){
e.printStackTrace();
}
}
}
}