隨筆-1  評論-0  文章-0  trackbacks-0
            1package chapter30;
            2
            3import java.io.UnsupportedEncodingException;
            4import java.sql.Connection;
            5import java.sql.DriverManager;
            6import java.sql.ResultSet;
            7import java.sql.SQLException;
            8import java.sql.Statement;
            9
           10import com.checker.ValueChecker;
           11
           12/**
           13 * Title: 
           14 * Description: 
           15 * Dec 14, 2007 11:13:12 AM
           16 * version: 
           17 * @author: Louis
           18 */

           19
           20public class DBManager
           21{
           22    private String className = "oracle.jdbc.driver.OracleDriver";
           23    private String url = "jdbc:oracle:thin:@172.29.21.40:1521:eprodb";
           24    private String uid = "scott";
           25    private String pwd = "tiger";
           26    private Connection conn;
           27    private Statement stmt;
           28    private String sql;
           29    private ResultSet rset;
           30
           31    /**
           32     * Title: constructor
           33     * Description: 
           34     * @param
           35     * @exception
           36     */

           37    public DBManager()
           38    {
           39        try
           40        {
           41            Class.forName(className);// Load the driver class.
           42            conn = DriverManager.getConnection(url, uid, pwd);// Create a connection through the JDBC method
           43            stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);// Create a statement.
           44        }

           45        catch (ClassNotFoundException cnfe)
           46        {
           47            cnfe.printStackTrace();
           48        }

           49        catch (SQLException sqle)
           50        {
           51            sqle.printStackTrace();
           52        }

           53    }

           54
           55    public void setSql(String cond)
           56    {
           57        sql = cond;
           58        sql = ValueChecker.stringNullZeroProcess(sql);
           59        try
           60        {
           61            sql = new String(sql.getBytes("iso-8859-1"));
           62        }

           63        catch (UnsupportedEncodingException uee)
           64        {
           65            uee.printStackTrace();
           66        }

           67    }

           68
           69    public Connection getConn()
           70    {
           71        return conn;
           72    }

           73
           74    /**
           75     * Title: Query operation and return result sets
           76     * Description: 
           77     * @param
           78     * @return: ResultSet
           79     * @exception
           80     */

           81    public ResultSet query(String cond)
           82    {
           83        setSql(cond);// Calling setSql() method.
           84        try
           85        {
           86            if (stmt != null && !ValueChecker.stringIsNullZero(sql))
           87            {
           88                rset = stmt.executeQuery(sql);
           89            }

           90        }

           91        catch (SQLException sqle)
           92        {
           93            sqle.printStackTrace();
           94        }

           95        return rset;
           96    }

           97
           98    /**
           99     * Title: delete, update, insert, drop operations and return row count of operation.
          100     * Description: 
          101     * @param
          102     * @return: boolean
          103     * @exception
          104     */

          105    public boolean update(String cond)
          106    {
          107        boolean result = false;
          108        setSql(cond);// Calling setSql() method.
          109        try
          110        {
          111            if (stmt != null && !ValueChecker.stringIsNullZero(sql))
          112            {
          113                result = stmt.executeUpdate(this.sql) > 0 ? true : false;
          114            }

          115        }

          116        catch (SQLException sqle)
          117        {
          118            sqle.printStackTrace();
          119        }

          120        return result;
          121    }

          122
          123    /**
          124     * Title: Close connection
          125     * Description: 
          126     * @param
          127     * @return: void
          128     * @exception
          129     */

          130    public void close()
          131    {
          132        try
          133        {
          134            if (rset != null)
          135                rset.close();
          136            if (stmt != null)
          137                stmt.close();
          138            if (conn != null)
          139                conn.close();
          140        }

          141        catch (SQLException sqle)
          142        {
          143            sqle.printStackTrace();
          144        }

          145    }

          146}
          posted on 2008-01-10 10:22 pm6422 閱讀(1051) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 武宣县| 武强县| 拉萨市| 新密市| 忻城县| 迁西县| 鄢陵县| 大足县| 马公市| 龙胜| 北宁市| 井冈山市| 常德市| 石屏县| 台南市| 临汾市| 浙江省| 连云港市| 洪湖市| 晋宁县| 罗定市| 汤原县| 乌鲁木齐县| 洛川县| 永修县| 陈巴尔虎旗| 六安市| 云林县| 大化| 固镇县| 喜德县| 文水县| 泰和县| 大埔县| 右玉县| 临猗县| 宿松县| 海安县| 闽侯县| 湘乡市| 铜川市|