package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.academyBean;
import bean.classBean;
import bean.professionBean;
import bean.sexBean;
import bean.timeBean;
import bean.typeBean;
import db.db;
public class chartsDAO {
private Connection conn;
public chartsDAO(){
this.conn=db.getConnection();
}
public ArrayList<academyBean> academyChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = "select u_academy,count(*) from user , borrow where borrow.U_ID = user.U_ID group by u_academy";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<academyBean> array = new ArrayList<academyBean>();
while(rst.next())
{
academyBean ab = new academyBean();
ab.setU_academy(rst.getString("u_academy"));
ab.setNum(rst.getInt("count(*)"));
array.add(ab);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<academyBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
public ArrayList<classBean> classChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = "select u_class,count(*) from user , borrow where borrow.U_ID = user.U_ID group by u_class";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<classBean> array = new ArrayList<classBean>();
while(rst.next())
{
classBean cb = new classBean();
cb.setU_class(rst.getString("u_class"));
cb.setNum(rst.getInt("count(*)"));
array.add(cb);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<classBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
public ArrayList<professionBean> professionChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = " select u_profession,count(*) from user , borrow where borrow.U_ID = user.U_ID group by u_profession";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<professionBean> array = new ArrayList<professionBean>();
while(rst.next())
{
professionBean cb = new professionBean();
cb.setU_profession(rst.getString("u_profession"));
cb.setNum(rst.getInt("count(*)"));
array.add(cb);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<professionBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
public ArrayList<sexBean> sexChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = "select u_sex,count(*) from user , borrow where borrow.U_ID = user.U_ID group by u_sex";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<sexBean> array = new ArrayList<sexBean>();
while(rst.next())
{
sexBean sb = new sexBean();
sb.setU_sex(rst.getString("u_sex"));
sb.setNum(rst.getInt("count(*)"));
array.add(sb);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<sexBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
public ArrayList<typeBean> typeChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = "select u_type,count(*) from user , borrow where borrow.U_ID = user.U_ID group by u_type";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<typeBean> array = new ArrayList<typeBean>();
while(rst.next())
{
typeBean tb = new typeBean();
tb.setU_type(rst.getString("u_type"));
tb.setNum(rst.getInt("count(*)"));
array.add(tb);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<typeBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
public ArrayList<timeBean> timeChart()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
String sql = "select MONTH(borrow_time) as month,count(*) from borrow group by MONTH(borrow_time)";
try{
pstmt = conn.prepareStatement(sql);
rst = pstmt.executeQuery();
ArrayList<timeBean> array = new ArrayList<timeBean>();
while(rst.next())
{
timeBean timebean = new timeBean();
timebean.setU_time(rst.getInt("month"));
timebean.setNum(rst.getInt("count(*)"));
array.add(timebean);
}
pstmt.close();
rst.close();
return array;
}catch(SQLException e){
return new ArrayList<timeBean>();
}finally{
try{
conn.close();
}catch(SQLException e){
System.out.println("Error occured at closing connection");
}
}
}
}