当前位置:编程学习 > JAVA >>

用JSP+JavaBean开发模式实现一个销售额的查询

\

 
 
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<Sales> find(String month) {
  Connection con = null;
  Statement state = null;
  ResultSet resultSet = null;
  List<Sales> 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 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页面:
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  
  <body>
    <form action="show.jsp" method="post">
     请选择查询的月份:
     <select name="month">
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,