分享

struct加Ajax (5)

 WindySky 2007-01-22

com.san.strutsAjax.utils.EngenderID (主键生成类)

package com.san.strutsAjax.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: huxinsheng
 * Date: 2006-4-9
 * Time: 15:41:39
 * To change this template use File | Settings | File Templates.
 */
public class EngenderID {

    public static int engenderTableId(String table) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        int i = 0;
        String sql = "select max(id)as id from " + table;
        try {
            conn = ConnectionDB.ConnectToDb();
            pstmt = conn.prepareStatement(sql);
            res = pstmt.executeQuery();
            while(res.next()){
                i = res.getInt("id");
            }
            if (i == 0) {
                return 1;
            } else {
                return ++i;
            }
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw new Exception(e);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多