数据库使用mysql,如下:
vo包的Sales类:
package com.vo;
public class Sales {
public String salestime;
public float salesnum;
public String getSalestime() {
return salestime;
}
public void setSalestime(String salestime) {
this.salestime = salestime;
}
public float getSalesnum() {
return salesnum;
}
public void setSalesnum(float salesnum) {
this.salesnum = salesnum;
}
}
dao包中的DBManager类:
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbManager {
private static String URL = "jdbc:mysql://localhost:3306/sales";
private static String USER = "root";
private static String PWD = "root";
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(Connection conn,Statement ste,ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ste !=null){
try {
ste.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
dao包中的SalesDao类:
package com.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.vo.Sales;
public class SalesDao {
public List find(String month) {
Connection con = null;
Statement state = null;
ResultSet resultSet = null;
List list = null;
String sql = "select * from sales where salestime like '" + month + "-%'" ;
con = DbManager.getConn();
try {
state = con.createStatement();
resultSet = state.executeQuery(sql);
while(resultSet.next()){
if(null == list){
list = new ArrayList();
}
Sales sales = new Sales();
sales.setSalestime(resultSet.getString("salestime"));
sales.setSalesnum(resultSet.getFloat("salesnum"));
list.add(sales);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbManager.closeAll(con, state, resultSet);
}
return list;
}
}
index.jsp页面:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page请选择查询的月份:
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
show.jsp页面:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'show.jsp' starting page销售日期 | 销售额 |
String month = request.getParameter("month");
SalesDao dao = new SalesDao();
List list = dao.find(month);
if(list != null){
for(Sales sales : list){
%>
}
}else{
%>
暂时没有数据}
%>
结果展示:
如图,选择1月,点击查询按钮,出现如下情况: