1 SQL脚本
CREATE TABLE `tb_goods1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`price` float DEFAULT NULL,
`unit` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`manufacturer` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
)
2 创建网站
3 新建Servlet 名称是GoodsServlet 包名是com.jht.servlet
4 引用Tomcat, 附加jar包文件 <Build Path菜单>
mysql-connector-java-3.0.16-ga-bin :数据库操作类
jstl-api-1.2 JSP标准标签库
jstl-impl-1.2 JSP标准标签库
5 创建实体类 GoodsForm 包名是 com.jht.model
代码如下:
public class GoodsForm {
private int id = 0; // 编号属性
private String name = ""; // 商品名称属性
private float price = 0.0f; // 单价属性
private String unit = ""; // 单位属性
private String manufacturer = ""; // 厂商属性
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPrice(float price) {
this.price = price;
}
public float getPrice() {
return price;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getUnit() {
return unit;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getManufacturer() {
return manufacturer;
}
}
6 编写数据库连接与操作类
package com.jht.tools;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class ConnDB {
public Connection conn=null;
public Statement stmt=null;
public ResultSet rs=null;
private static String propFileName="connDB.properties";
private static Properties prop=new Properties();
private static String dbClassName="com.mysql.jdbc.Driver";
private static String dbUrl="jdbc:mysql://127.0.0.1:3306/db_Database07?user=root&password=111&characterEncoding=UTF-8";
public ConnDB() { //定义构造方法
try { //捕捉异常
//将Properties文件读取到InputStream对象中
InputStream in = getClass().getResourceAsStream(propFileName);
prop.load(in); // 通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动
dbUrl = prop.getProperty("DB_URL", dbUrl); //获取URL
} catch (Exception e) {
e.printStackTrace(); // 输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try { //连接数据库时可能发生异常因此需要捕捉该异常
Class.forName(dbClassName).newInstance(); //装载数据库驱动
//建立与数据库URL中定义的数据库的连接
conn = DriverManager.getConnection(dbUrl);
} catch (Exception ee) {
ee.printStackTrace(); //输出异常信息
}
if (conn == null) {
System.err
.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
+ dbClassName
+ "\r\n链接位置:"
+ dbUrl); //在控制台上输出提示信息
}
return conn; //返回数据库连接对象
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql); //执行SQL语句,并返回一个ResultSet对象rs
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
return rs; // 返回结果集对象
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try { // 捕捉异常
if (rs != null) { // 当ResultSet对象的实例rs不为空时
rs.close(); // 关闭ResultSet对象
}
if (stmt != null) { // 当Statement对象的实例stmt不为空时
stmt.close(); // 关闭Statement对象
}
if (conn != null) { // 当Connection对象的实例conn不为空时
conn.close(); // 关闭Connection对象
}
} catch (Exception e) {
e.printStackTrace(System.err); // 输出异常信息
}
}
}
7 配置文件connDB.properties 内容如下:
DB_CLASS_NAME=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/c2cd?user=root&password=root&characterEncoding=UTF-8
8 web.xml 文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>GoodsServlet</servlet-name>
<servlet-class>com.jht.servlet.GoodsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GoodsServlet</servlet-name>
<url-pattern>/GoodsServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
9 index.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:redirect url="GoodsServlet">
<c:param name="action" value="query" />
</c:redirect>
</body>
</html>
10 GoodsList.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="450" height="47" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#333333">
<tr>
<td height="30" colspan="5" bgcolor="#EFEFEF">·商品列表</td>
</tr>
<tr>
<td width="36" height="27" align="center" bgcolor="#FFFFFF">编号</td>
<td width="137" align="center" bgcolor="#FFFFFF">商品名称</td>
<td width="85" align="center" bgcolor="#FFFFFF">单价</td>
<td width="38" align="center" bgcolor="#FFFFFF">单位</td>
<td width="148" align="center" bgcolor="#FFFFFF">厂商</td>
</tr>
<c:forEach var="goods" items="${requestScope.goodsList}">
<tr>
<td height="27" bgcolor="#FFFFFF">
<c:out value="${goods.id}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.name}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.price}"/>(元)</td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.unit}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.manufacturer}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
11 显示数据库中的商品信息
posted on 2012-07-11 14:35
jhtchina 阅读(396)
评论(0) 编辑 收藏 所属分类:
servlet