商品信息数据库GoodsForm:
create table GoodsForm(
id int identity(1,1) primary key,
name varchar(45) not null,
price float not null,
unit varchar(10) not null,
manufacturer varchar(45) not null
)
javaBean类:GoodsForm
package com.model;
public class GoodsForm {
private int id=0;
private String name="";
private double price;
private String unit="";
private String manufacturer="";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
connDB.properties
DB_CLASS_NAME=com.microsoft.sqlserver.jdbc.SQLServerDriver
DB_URL=jdbc:sqlserver://localhost:1433;databaseName=user_login
DB_USERNAME=du
DB_PASSWORD=123
数据库操作类:
package com.tool;
import java.sql.*;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
public class ConnDB {
public Connection conn=null;
public Statement stmt=null;
public ResultSet rs=null;
private String propFileName="connDB.properties";
private static Properties prop=null;
private static String dbClassName="";
private static String url="";
private static String username="";
private static String pwd="";
public ConnDB() throws IOException
{
try
{
prop=new Properties();
InputStream in=getClass().getResourceAsStream(propFileName);
prop.load(in);
dbClassName=prop.getProperty("DB_CLASS_NAME");
url=prop.getProperty("DB_URL");
username=prop.getProperty("DB_USERNAME");
pwd=prop.getProperty("DB_PASSWORD");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static Connection getConnection()
{
Connection conn=null;
try
{
Class.forName(dbClassName);
conn=DriverManager.getConnection(url, username, pwd);
}
catch(Exception ex)
{
ex.printStackTrace();
System.err.println("数据库连接失败!");
return conn;
}
return conn;
}
public ResultSet executeQuery(String sql)
{
try
{
conn=getConnection();
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
return rs;
}
return rs;
}
public void close()
{
try
{
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
获取商品信息GoodsServlet类
package com.servlet;
import java.util.List;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.GoodsForm;
import com.tool.ConnDB;
/**
* Servlet implementation class GoodsServlet
*/
public class GoodsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GoodsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter("action");
if("query".equals(action))
{
this.query(request,response);
}
}
private void query(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ConnDB conn=new ConnDB();
String sql="select * from from tb_goods";
ResultSet rs=conn.executeQuery(sql);
List<GoodsForm> list=new ArrayList<GoodsForm>();
try
{
while(rs.next())
{
GoodsForm goods=new GoodsForm();
goods.setName(rs.getString(2));
goods.setPrice(rs.getDouble(3));
goods.setUnit(rs.getString(4));
goods.setManufacturer(rs.getString(5));
list.add(goods);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
request.setAttribute("goodsList", list);
request.getRequestDispatcher("goodsList.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>shopInfo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.CharacterEncodingFilter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*r</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<description></description>
<servlet-name>GoodsServlet</servlet-name>
<servlet-class>com.servlet.GoodsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GoodsServlet</servlet-name>
<url-pattern>/GoodsServlet</url-pattern>
</servlet-mapping>
</web-app>
显示页面:
index.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<c:redirect url="./GoodsServlet">
<c:param name="action" value="query"></c:param>
</c:redirect>
</body>
</html>
goodsList.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<table style="width:450px;height:47px;border:0px;align:center;bgcolor:#333333">
<tr>
<td>编号</td>
<td>商品名称</td>
<td>单价</td>
<td>单位</td>
<td>厂商</td>
</tr>
<c:forEach var="goods" items="${requestScope.goodsList}">
<tr>
<td>${goods.id}</td>
<td><c:out value="${goods.name}"></c:out></td>
<td><c:out value="${goods.price}"></c:out></td>
<td><c:out value="${goods.unit}"></c:out></td>
<td><c:out value="${goods.manufacturer}"></c:out></td>
</tr>
</c:forEach>
</table>
</body>
</html>