Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264298
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:48:39

    我的平台是:Eclipse3.2   MyEclipse5.5 Tomcat5.5 MySql5.0

    第一步:创建数据库:
    这没什么难的,用下面的脚本就OK了。
    CREATEDATABASEpage;
    usepage;
    CREATETABLE `product` (
     `id` varchar(11) NOTNULL,
     `sortid` varchar(11) NOTNULL,
     `name` varchar(50) NOTNULL,
     `price` doubleNOTNULL,
     `saleprice` doubleNOTNULL,
     `descript` text NOTNULL,
     `contents` text NOTNULL,
      `saledate` varchar(255) NOTNULL,
     `salecount` int(11) defaultNULL,
     `image` text,
     PRIMARYKEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    第二步:创建一个项目
    创建一个项目,项目名为”strutsPage”,导入Struts1.2 ,Struts的包采用默认,引用MySql的驱动,要是没有驱动的话,请到这。
    下面设置web.xml和struts-config.xml配置文件,我觉得直接COPY我的就好了。
    web.xml:文件里的内容如下,直接换上就OK了。基本是默认的。
   
   
    
        action
        org.apache.struts.action.ActionServlet
       
          config
          /WEB-INF/struts-config.xml
       

       
          debug
          3
       

       
          detail
          3
       

        0
    

    
        action
        *.do
    

    
        index.jsp
    

   


    struts-config.xml的内容如下:
   
   

   
    
    
    
    
    
                 attribute="productShowForm"
          input="/index.jsp"
          name="productShowForm"
          path="/productShow"
          scope="request"
          type="com.yourcompany.struts.action.ProductShowAction">
         
       
    

    
   

    第三步:创建包和数据库连接
   
    在SRC下创建 dao , dbtool, vo,factory四个包
    在dbtool包里主要放访问JDBC数据库的连接类等。下面提供我用的Bean类。

    DBConnection.java的内容如下:
    package com.yourcompany.dbtool;

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    /**
     * 这是一个连接数据的单模式
     * @author 树下无影
     *
     */
    public class DBConnection {
          private static DBConnection instance;

          private String driver;
          private String url;
          private String user;
          private String password;

          private DBConnection() throws Exception{

           InputStream in=getClass().getClassLoader().getResourceAsStream(
                   "com/yourcompany/dbtool/database.properties");
           Properties prop=new Properties();
           prop.load(in);
           driver=prop.getProperty("driver");
           url=prop.getProperty("url");
           user=prop.getProperty("user");
           password=prop.getProperty("password");
           try{
               Class.forName(driver);
           }catch(Exception e)
           {
               System.out.println("数据库初始化出错");
                 throw e;
           }
           System.out.println(driver+" "+url+" "+user+" "+password);
          }
          public static DBConnection getInstance(){
           try{
               if(instance==null){
                   instance=new DBConnection();
               }
               return instance;
           }catch(Exception e){
               System.out.println("实例化单模子出错");

               return null;
           }
          }

          public Connection getConnection()throws SQLException{
           Connection con;
           try{
               con=DriverManager.getConnection(url, user, password);

           }catch(SQLException e){
              System.out.println("Connection连接出错");
                throw e;
           }
           return con;
          }

          public void closeConnection(Connection con){

           if(con!=null){
               try{
                   con.close();
               }catch(SQLException e)
               {
                   System.out.println("关闭Connection连接出错");

               }

           }
          }
    }

[1]     

【责编:Ken】

--------------------next---------------------

阅读(124) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~