关于动态生成WML文件的一个Java实例(3)

具体实现:
 通过创建数据表,我们可以写出表WAP_PageStructure的JavaBean,其中不仅包含了Bean属性,还添加了add()方法用于添加数据,delete()方法用于删除数据,update()方法用于修改数据,checkDuple()方法用于判断添加或修改的数据是否与数据库中重复,checkCanDelete()方法用于判断能否删除数据,checkExisted()方法用于判断是否存在ID为选定值的数据,如果存在则加载该数据;几个静态的数据成员用于长度数据验证。具体类如下:

package  com.data.bean;

import  java.sql.PreparedStatement;
import  java.sql.SQLException;
import  java.sql.Timestamp;

import  javax.sql.DataSource;

import  com.util.db.DBService;

public   class  WAP_PageStructure  {
    
private static String TABLE_NAME = "WAP_PageStructure";
    
private static String INSERT_SQL = "INSERT INTO " + TABLE_NAME 
                + " (URL, Name, Content, IsMain, ParentPage, ParentURL,"
                " MainPage, Type, ShowType, CreateTime, UpdateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
private static String UPDATE_SQL = "UPDATE " + TABLE_NAME
               + " SET URL = ?, Name = ?, Content = ?, IsMain = ?, "
               +  " MainPage = ?, ParentPage = ?, ParentURL = ?, Type = ?, ShowType = ?, UpdateTime = ? " 
               + " WHERE ID = ?";
    
private static String DELETE_SQL = "DELETE FROM " + TABLE_NAME + " WHERE ID = ?";
    
private static String SELECT_EXISTED_SQL = "SELECT * FROM " + TABLE_NAME 
               + " WHERE ID = ?";
    
private static String SELECT_DUPLE_SQL = "SELECT * FROM " + TABLE_NAME 
              + " WHERE URL = ? AND Name = ? AND ID != ?";
    
private static String SELECT_USED_SQL = "SELECT * FROM " + TABLE_NAME 
              + " WHERE ParentPage = ?";
    
// 定义字段长度
    public static int LENGTH_URL    = 20;
    
public static int LENGTH_NAME        =    9;
    
public static int LENGTH_Content        =    255;

    
// 数据源
    private DataSource dataSource;
    
    
// ID
    private int id = 0;
    
// URL
    private String url = "";
    
// Name
    private String name = "";
    
// Content
    private String content = "";
    
// IsMain
    private boolean isMain = false;
    
// ParentPage 
    private int parentPage = 0;
    
// ParentURL
    private String parentURL = ""
    
// Type
    private int type = 0;
    
// showType
    private int showType = 0;
    
// MainPage
    private int mainPage = 0;
    
    
    
public WAP_PageStructure() {        
    }


    
public WAP_PageStructure(DataSource dataSource) {        
        
this.dataSource = dataSource;
    }

    
    
public WAP_PageStructure(DataSource dataSource, int id) {        
        
this.dataSource = dataSource;
        
this.id = id ;
    }

    
    
public String toString() {
        StringBuffer sBuff 
= new StringBuffer();
        sBuff.append(TABLE_NAME).append(
"");
        sBuff.append(
"|URL = ").append(this.url);
        sBuff.append(
"|Name = ").append(this.name);
        sBuff.append(
"|Content = ").append(this.content);
        sBuff.append(
"|IsMain = ").append(this.isMain);
        sBuff.append(
"|ParentPage = ").append(this.parentPage);
        sBuff.append(
"|ParentURL = ").append(this.parentURL);
        sBuff.append(
"|type = ").append(this.type);
        sBuff.append(
"|ShowType = ").append(this.showType);
        
        
return sBuff.toString();
    }

    
    
/**
     * 新增信息记录
     * 
@throws SQLException
     
*/

    
public void add() throws SQLException {
        Timestamp now 
= new Timestamp(new java.util.Date().getTime());
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(INSERT_SQL);
            pStmt.setString(
1this.url);
            pStmt.setString(
2this.name);
            pStmt.setString(
3this.content);
            pStmt.setBoolean(
4this.isMain);
            pStmt.setInt(
5this.parentPage);
            pStmt.setString(
6this.parentURL);
            pStmt.setInt(
7this.mainPage);
            pStmt.setInt(
8this.type);
            pStmt.setInt(
9this.showType);
            pStmt.setTimestamp(
10, now);
            pStmt.setTimestamp(
11, now);
            dbSrv.executeUpdate();
        }
 finally {
            dbSrv.close();
        }

    }

    
    
/**
     * 修改信息记录
     * 
@throws SQLException
     
*/

    
public void update() throws SQLException {
        Timestamp now 
= new Timestamp(new java.util.Date().getTime());
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(UPDATE_SQL);
            pStmt.setString(
1this.url);
            pStmt.setString(
2this.name);
            pStmt.setString(
3this.content);
            pStmt.setBoolean(
4this.isMain);
            pStmt.setInt(
5this.mainPage);
            pStmt.setInt(
6this.parentPage);
            pStmt.setString(
7this.parentURL);
            pStmt.setInt(
8this.type);
            pStmt.setInt(
9this.showType);
            pStmt.setTimestamp(
10, now);
            pStmt.setInt(
11this.id);
            dbSrv.executeUpdate();
        }
 finally {
            dbSrv.close();
        }

    }

    
    
/**
     * 删除信息记录
     * 
@throws SQLException
     
*/

    
public void delete() throws SQLException {
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(DELETE_SQL);
            pStmt.setInt(
1this.id);
            dbSrv.executeUpdate();
        }
 finally {
            dbSrv.close();
        }

    }

    
    
/**
     * 检查是否存在该ID
     * 
@throws SQLException
     
*/

    
public boolean checkExisted() throws SQLException {
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(SELECT_EXISTED_SQL);
            pStmt.setInt(
1this.id);
            dbSrv.executeQuery();
            
if (dbSrv.next()) {
                
this.url = dbSrv.getString("URL");
                
this.name = dbSrv.getString("Name");
                
this.content = dbSrv.getString("Content");
                
this.isMain = dbSrv.getBoolean("IsMain");
                
this.parentPage = dbSrv.getInt("ParentPage");
                
this.parentURL = dbSrv.getString("ParentURL");
                
this.type = dbSrv.getInt("Type");
                
this.showType = dbSrv.getInt("ShowType");
                
this.mainPage = dbSrv.getInt("MainPage");
                
return true;
            }
 else 
                
return false;
        }
 finally {
            dbSrv.close();
        }

    }
    
    
    
/**
     * 检查是否信息重复
     * 
@throws SQLException
     
*/

    
public boolean checkDuple() throws SQLException {
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(SELECT_DUPLE_SQL);
            pStmt.setString(
1this.url);
            pStmt.setString(
2this.name);
            pStmt.setInt(
3this.id);
            dbSrv.executeQuery();
            
if (dbSrv.next()) {
                
return true;
            }
 else 
                
return false;
        }
 finally {
            dbSrv.close();
        }

    }

    
    
/**
     * 检查是否能删除该信息
     * 
@throws SQLException
     
*/

    
public boolean checkCanDelete() throws SQLException {
        DBService dbSrv 
= new DBService(this.dataSource);
        
try {
            PreparedStatement pStmt 
= dbSrv.prepareStatement(SELECT_USED_SQL);
            pStmt.setInt(
1this.id);
            dbSrv.executeQuery();
            
if (dbSrv.next()) {
                
return false;
            }
 else {
                
return true;
            }

        }
 finally {
            dbSrv.close();
        }

    }


    
public String getContent() {
        
return content;
    }


    
public void setContent(String content) {
        
this.content = content;
    }


    
public int getID() {
        
return id;
    }


    
public void setID(int id) {
        
this.id = id;
    }


    
public boolean isMain() {
        
return isMain;
    }


    
public void setMain(boolean isMain) {
        
this.isMain = isMain;
    }


    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
    }


    
public int getParentPage() {
        
return parentPage;
    }


    
public void setParentPage(int parentPage) {
        
this.parentPage = parentPage;
    }


    
public String getParentURL() {
        
return parentURL;
    }


    
public void setParentURL(String parentURL) {
        
this.parentURL = parentURL;
    }


    
public int getShowType() {
        
return showType;
    }


    
public void setShowType(int showType) {
        
this.showType = showType;
    }


    
public int getType() {
        
return type;
    }


    
public void setType(int type) {
        
this.type = type;
    }


    
public String getURL() {
        
return url;
    }


    
public void setURL(String url) {
        
this.url = url;
    }


    
public int getMainPage() {
        
return mainPage;
    }


    
public void setMainPage(int mainPage) {
        
this.mainPage = mainPage;
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值