隨筆-0  評論-0  文章-16  trackbacks-0

          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">&nbsp;

                <c:out value="${goods.id}"/></td>

                <td bgcolor="#FFFFFF">&nbsp;

                <c:out value="${goods.name}"/></td>

                <td bgcolor="#FFFFFF">&nbsp;

                <c:out value="${goods.price}"/>(元)</td>

                <td bgcolor="#FFFFFF">&nbsp;

                <c:out value="${goods.unit}"/></td>

                <td bgcolor="#FFFFFF">&nbsp;

                <c:out value="${goods.manufacturer}"/></td>

              </tr>

              </c:forEach> 

           </table>

          </body>

          </html>

          11 顯示數據庫中的商品信息

           

          posted on 2012-07-11 14:35 jhtchina 閱讀(397) 評論(0)  編輯  收藏 所屬分類: servlet
          主站蜘蛛池模板: 垣曲县| 晋中市| 宁河县| 卢湾区| 商南县| 新民市| 巫山县| 怀来县| 伊金霍洛旗| 玉龙| 遵化市| 溧水县| 广平县| 原平市| 额济纳旗| 乌拉特中旗| 东港市| 兴安盟| 闻喜县| 安宁市| 梅州市| 信宜市| 永州市| 英超| 德庆县| 房产| 威远县| 出国| 长汀县| 绥江县| 武邑县| 肥西县| 屯门区| 宁海县| 贡嘎县| 蒙自县| 安泽县| 无极县| 神池县| 中卫市| 新闻|