五,dao和tools代码
JobDao.java
package com.hust.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.Bean.JobBean;
import com.hust.tools.DBConnection;
public class JobDao {
private DBConnection DB=new DBConnection();
//返回title列表
public static List<String> GetTitleList(){
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
List<String> titlelist=new ArrayList<String>();
if(conn!=null){
try {
String sql="select Title from job ORDER BY Date DESC limit 0,4";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
titlelist.add(rs.getString("Title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return titlelist;
}
//返回job对象
public List<JobBean> GetJobList(){
Connection conn=DB.getCon();
List<JobBean> joblist=new ArrayList<JobBean>();
if(conn!=null){
try {
String sql ="select * from job order by Date desc";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
JobBean job=new JobBean();
job.setId(rs.getInt("id"));
job.setTitle(rs.getString("Title"));
job.setCompany(rs.getString("Company"));
job.setContent(rs.getString("Content"));
job.setTime(rs.getString("Time"));
job.setAddress(rs.getString("Address"));
job.setPay(rs.getString("Pay"));
job.setRemark(rs.getString("Remark"));
job.setLinkman(rs.getString("Linkman"));
job.setPhone(rs.getString("Phone"));
job.setDate(rs.getString("Date"));
joblist.add(job);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*for(int i=0;i<joblist.size();i++){
System.out.println(joblist.get(i).getCompany());
}*/
return joblist;
}
//插入工作
public boolean InsertJob(String[] s){
Connection conn=DB.getCon();
if(conn!=null){
try {
String sql ="insert into job values(?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setInt(1, Integer.valueOf(s[0]));
pstm.setString(2, s[1]);
pstm.setString(3, s[2]);
pstm.setString(4, s[3]);
pstm.setString(5, s[4]);
pstm.setString(6, s[5]);
pstm.setString(7, s[6]);
pstm.setString(8, s[7]);
pstm.setString(9, s[8]);
pstm.setString(10, s[9]);
pstm.setString(11, s[10]);
pstm.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}
LectureDao.java
package com.hust.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.Bean.LectureBean;
import com.hust.tools.DBConnection;
public class LectureDao {
private DBConnection DB;
//返回title列表
public static List<String> GetTitleList(){
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
List<String> titlelist=new ArrayList<String>();
if(conn!=null){
try {
String sql="select Title from lecture ORDER BY Date DESC limit 0,4";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
titlelist.add(rs.getString("Title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return titlelist;
}
//查询华科大讲座或者武汉讲座
public List<LectureBean> GetLectureList(String sql){
DB=new DBConnection();
Connection conn=DB.getCon();
List<LectureBean> lecturelist=new ArrayList<LectureBean>();
if(conn!=null){
try {
//String sql ="select * from lecture where Sign=1 ";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
LectureBean lecture=new LectureBean();
lecture.setId(rs.getInt("Id"));
lecture.setTitle(rs.getString("Title"));
lecture.setSpeaker(rs.getString("Speaker"));
lecture.setIntroduce(rs.getString("Introduce"));
lecture.setTime(rs.getString("Time"));
lecture.setAddress(rs.getString("Address"));
lecture.setSign(rs.getString("Sign"));
lecture.setDate(rs.getString("Date"));
lecturelist.add(lecture);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*for(int i=0;i<joblist.size();i++){
System.out.println(joblist.get(i).getCompany());
}*/
return lecturelist;
}
//查询单个lecture信息
public LectureBean getLecture(int id){
DB=new DBConnection();
Connection conn=DB.getCon();
LectureBean lecture=new LectureBean();
if(conn!=null){
try {
String sql ="select * from lecture where Id='"+id+"'";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
lecture.setId(rs.getInt("Id"));
lecture.setTitle(rs.getString("Title"));
lecture.setSpeaker(rs.getString("Speaker"));
lecture.setIntroduce(rs.getString("Introduce"));
lecture.setTime(rs.getString("Time"));
lecture.setAddress(rs.getString("Address"));
lecture.setSign(rs.getString("Sign"));
lecture.setDate(rs.getString("Date"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return lecture;
}
//插入lecture信息
public boolean InsertLecture(String params[]){
DB=new DBConnection();
Connection conn=DB.getCon();
LectureBean lecture=new LectureBean();
if(conn!=null){
try {
String sql ="insert into lecture values(?,?,?,?,?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setInt(1,Integer.valueOf(params[0]));
pstm.setString(2, params[1]);
pstm.setString(3, params[2]);
pstm.setString(4, params[3]);
pstm.setString(5, params[4]);
pstm.setString(6, params[5]);
pstm.setString(7, params[6]);
pstm.setString(8, params[7]);
pstm.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}
RecruitDao.java
package com.hust.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.tools.DBConnection;
public class RecruitDao {
//返回title列表
public static List<String> GetTitleList(){
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
List<String> titlelist=new ArrayList<String>();
if(conn!=null){
try {
String sql="select Title from recruit ORDER BY Date DESC limit 0,4";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
titlelist.add(rs.getString("Title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return titlelist;
}
}
RentDao.java
package com.hust.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.tools.DBConnection;
public class RentDao {
//返回title列表
public static List<String> GetTitleList(){
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
List<String> titlelist=new ArrayList<String>();
if(conn!=null){
try {
String sql="select Title from rent ORDER BY Date DESC limit 0,4";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
titlelist.add(rs.getString("Title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return titlelist;
}
}
TutorDao.java
package com.hust.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.tools.DBConnection;
public class TutorDao {
//返回title列表
public static List<String> GetTitleList(){
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
List<String> titlelist=new ArrayList<String>();
if(conn!=null){
try {
String sql="select Title from tutor ORDER BY Date DESC limit 0,4 ";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
titlelist.add(rs.getString("Title"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return titlelist;
}
}
TypeDao.java
package com.hust.Dao;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import com.hust.tools.DBConnection;
public class TypeDao {
private DBConnection DB=new DBConnection();
//放回信息类别列表
public Map<Integer,String> GetTypeList(){
Connection conn=DB.getCon();
Map<Integer,String> typemap=new HashMap<Integer,String>();
if(conn!=null){
try {
String sql="select * from infotype order by Sign";
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
Integer sign=Integer.valueOf(rs.getInt("Sign"));
String name=rs.getString("Name");
typemap.put(sign, name);
}
rs.close();
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return typemap;
}
}
DateToString.java
package com.hust.tools;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
public static String dateTimeChange(Date date){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
return time;
}
}
MySuperAction.java是Action继承的类
package com.hust.tools;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class MySuperAction extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Map session;
public void setSession(Map session) {
this.session=session;
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
}
PageQueryDao.java
package com.hust.tools;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hust.Bean.JobBean;
import com.hust.Bean.LectureBean;
import com.hust.tools.DBConnection;
public class PageQueryDao {
//分页查询所有兼职信息
public List<JobBean> getJobPageList(int page){
List<JobBean> list=new ArrayList<JobBean>();
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
String sql="select * from job order by Id desc limit ?,?";
try {
PreparedStatement pstm=conn.prepareStatement(sql);
pstm.setInt(1, (page-1)*JobBean.PAGE_SIZE); //设置查询记录的开始位置
pstm.setInt(2, JobBean.PAGE_SIZE); //设置查询数据所返回的记录数
ResultSet rs=pstm.executeQuery();
while(rs.next()){
JobBean job=new JobBean();
job.setId(rs.getInt("id"));
job.setTitle(rs.getString("Title"));
job.setCompany(rs.getString("Company"));
job.setContent(rs.getString("Content"));
job.setTime(rs.getString("Time"));
job.setAddress(rs.getString("Address"));
job.setPay(rs.getString("Pay"));
job.setRemark(rs.getString("Remark"));
job.setLinkman(rs.getString("Linkman"));
job.setPhone(rs.getString("Phone"));
job.setDate(rs.getString("Date"));
list.add(job);
}
rs.close();
pstm.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//查询总记录数
public int FindCounts(String sql){
int count=0;
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
//String sql="select count(*) from job";
try {
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
if(rs.next()){//rs里就一个值
count=rs.getInt(1);
}
rs.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
//查询讲座分页列表
public List<LectureBean> getLecturePageList(int page,String sql){
List<LectureBean> lecturelist=new ArrayList<LectureBean>();
DBConnection DB=new DBConnection();
Connection conn=DB.getCon();
//String sql="select * from lecture order by Id desc limit ?,?";
try {
PreparedStatement pstm=conn.prepareStatement(sql);
pstm.setInt(1, (page-1)*LectureBean.PAGE_SIZE); //设置查询记录的开始位置
pstm.setInt(2, LectureBean.PAGE_SIZE); //设置查询数据所返回的记录数
ResultSet rs=pstm.executeQuery();
while(rs.next()){
LectureBean lecture=new LectureBean();
lecture.setId(rs.getInt("id"));
lecture.setTitle(rs.getString("Title"));
lecture.setSpeaker(rs.getString("Speaker"));
lecture.setIntroduce(rs.getString("Introduce"));
lecture.setTime(rs.getString("Time"));
lecture.setAddress(rs.getString("Address"));
lecture.setSign(rs.getString("sign"));
lecture.setDate(rs.getString("Date"));
lecturelist.add(lecture);
}
rs.close();
pstm.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lecturelist;
}
}