SKY外语计算机学习

标题: JSP MVC模式思路 [打印本页]

作者: Itzhangyoufu    时间: 2013-11-5 23:18
标题: JSP MVC模式思路
作者:ITZhangyoufu 日期:2013-11-05 原文地址:http://www.skywj.com/thread-9320-1-1.html



新建项目web project 起名为:Weblog

在项目WeblogWebRoot\WEB-INF\lib中导入jar架包。

三在项目Weblog的WebRoot下新建文件夹:admin,front,css;再将images放在WebRoot下。


四再将WebRoot做为DW的站点,在相应的位置保存jsp界面。


五 在src中新建四个类:
1. com.mstf.base(用于连接数据库和通用的增删改查和乱码处理,时间转换格式)
2.com.mstf.dao(用于对所有表的所有操作)
3. Com.mstf.entity (用于封装所有表中表的字段)
4.com.mstf.servlet (用于连接javaBean和jsp界面)

六 com.mstf.base中新建DBHelp类,如
public class DBHelper {
    private final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String URL="jdbc:sqlserver://localhost:1433;databasename=Text";
    private final String PWD="123456";
    private final String UID="sa";
   
    Connection conn=null;
    PreparedStatement psd=null;
    ResultSet res=null;
   
    public Connection GetConn()
    {
     try {
         Class.forName(DRIVER);
         conn=DriverManager.getConnection(URL, UID, PWD);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
     
     return conn;
    }
    /***
     * 功能:通用 增删改
     * @param sql 传入的SQL语句
     * @param pas 传入的参数集合
     * @return 返回 影响行数
     * @throws SQLException
     */
    public int updateSQL(String sql,Object[] pas) throws SQLException{
     int result=0;
     try {
         GetConn();
         psd=conn.prepareStatement(sql);
         if(pas!=null && pas.length!=0)
         {
             for(int i=0;i<pas.length;i++)
             {
                 psd.setObject(i+1,pas);
             }
         }
         result=psd.executeUpdate();
        } catch (Exception e) {
            // TODO: handle exception
        }
        finally
        {
            conn.close();
            psd.close();
        }
     return result;
    }
    //能用查询
    /***
     * 功能:通用 查询
     * @param sql 传入的SQL语句
     * @param pas 传入的参数集合
     * @return 返回 影响行数
     * @throws SQLException
     */
    public ResultSet querySQL(String sql,Object[] pas) throws SQLException{
     try {
         GetConn();
         psd=conn.prepareStatement(sql);
         if(pas!=null && pas.length!=0)
         {
             for(int i=0;i<pas.length;i++)
             {
                 psd.setObject(i+1,pas);
             }
         }
         res=psd.executeQuery();
        } catch (Exception e) {
            // TODO: handle exception
        }
     return res;      
}

如:
      packagecom.scce.tools;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
/**
* 字符串工具类
* @authorAdministrator
*/
public class StringTool {
/**
  * 将日期转换为特定格式的字符串
  * @param date
  * @return
  */
  public staticString convertDate(Date date){
   String dateStr=null;
   SimpleDateFormat sdf=newSimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
   dateStr=sdf.format(date);
   return dateStr;
  }
}

作者: admin    时间: 2013-11-5 23:30
有福的处女贴,应该支持下!
作者: 小卢_铃    时间: 2013-11-6 09:17
必须的 顶下 嘿嘿
作者: sky_yx    时间: 2013-11-6 09:32
嘿嘿,
绝对的支持
作者: Itzhangyoufu    时间: 2013-11-6 10:48
谢谢亲们




欢迎光临 SKY外语计算机学习 (http://www.skywj.com/) Powered by Discuz! X2.5