数据库操作类(SQLHelper)

it2022-05-05  180

本文主要参考了周老师的科研站 地址: http://tmsoft.lsxy.com/index.php?load=read&id=475 代码已经经过简单测试,如有发现问题请大家指出,我会及时改正 package  sqlhelp; import  java.sql.Connection; import  java.sql.DriverManager; import  java.sql.PreparedStatement; import  java.sql.ResultSet; import  java.util. * ; import  java.sql. * ; import  javax.servlet.jsp.jstl.sql.Result; import  javax.servlet.jsp.jstl.sql.ResultSupport; /** */ /** *  * @author libiao * @date   2008/04/06 * 功能: *         ①:存储过程有参数时,Insert/Update処理,返回成功或者失败的FLG(FLG>0时,表示成功); *         ②:存储过程没有参数时,Insert/Update処理,返回成功或者失败的FLG(FLG>0时,表示成功); *         ③:存储过程有参数时,Select処理,返回一个ArrayList; *         ④:存储过程没有参数时,Select処理,返回一个ArrayList; *         ⑤:存储过程有参数时,Select処理,返回一个SortedMap[]; *         ⑥:存储过程没有参数时,Select処理,返回一个SortedMap[]; *         ⑦:存储过程有参数时,Select処理,返回结果集中的第一行的name所指定的列的值,当结果集为空时,返回null; *         ⑧:存储过程没有参数时,Select処理,返回结果集中的第一行的name所指定的列的值,当结果集为空时,返回null; *         ⑨:存储过程有参数时,Select処理,返回结果集中的第一行的index所指定的列的值,当结果集为空时,返回null; *         ⑩:存储过程没有参数时,Select処理,返回结果集中的第一行的index所指定的列的值,当结果集为空时,返回null; *  * * */ public   class  SQLHelper  {    /** *//**     * @author libiao     * @return Connection     * 功能:获得Connection     */    private static Connection getConnect() {        try {//            Class.forName("net.sourceforge.jtds.jdbc.Driver");//            return DriverManager.getConnection("jdbc:jtds:sqlserver://local:1433;DatabaseName=biao;", "sa","");            return DriverManager.getConnection("proxool.SQL");        } catch (Exception e) {            System.out.println("SQLHepler-getConnect:" + e.getMessage());            return null;        }    }    /** *//**     *      * ①     * 功能:存储过程有参数时,Insert/Update処理,返回成功或者失败的FLG(FLG>0时,表示成功);     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return 更新成功或者失败的FLG     * @throws Exception     *      */    public static int ExecuteNonQuery(String cmdtext, String[] parms) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;                try {            conn = getConnect();            pstmt = conn.prepareStatement(cmdtext);            prepareCommand(pstmt, parms);                        return pstmt.executeUpdate();                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteNonQuery-parms:" + e.getMessage());        } finally {            try {                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteNonQuery-parms-close:" + e.getMessage());            }        }    }        /** *//**     *      * ②     * 功能:存储过程没有参数时,Insert/Update処理,返回成功或者失败的FLG(FLG>0时,表示成功);     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @return 更新成功或者失败的FLG     * @throws Exception     *      */    public static int ExecuteNonQuery(String cmdtext) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;                try {            conn = getConnect();            pstmt = conn.prepareStatement(cmdtext);                        return pstmt.executeUpdate();                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteNonQuery:" + e.getMessage());        } finally {            try {                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteNonQuery-close:" + e.getMessage());            }        }    }        /** *//**     *      * ③     * 功能:存储过程有参数时,Select処理,返回一个ArrayList;     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return 结果集(ArrayList)     * @throws Exception     */    public static ArrayList ExecuteReaderList(String cmdtext, String[] parms) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);                        prepareCommand(pstmt, parms);            rs = pstmt.executeQuery();                        ArrayList<Object[]> al = new ArrayList<Object[]>();            ResultSetMetaData rsmd = rs.getMetaData();            int column = rsmd.getColumnCount();                        while (rs.next()) {                Object[] ob = new Object[column];                for (int i = 1; i <= column; i++{                    ob[i - 1= rs.getObject(i);                }                al.add(ob);            }                        return al;                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteReaderList-parms:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteReaderList-parms-close:" + e.getMessage());            }        }    }            /** *//**     *      * ④     * 機能:存储过程没有参数时,Select処理,返回一个ArrayList;     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @return 结果集(ArrayList)     * @throws Exception     */    public static ArrayList ExecuteReaderList(String cmdtext) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);            rs = pstmt.executeQuery();                        ArrayList<Object[]> al = new ArrayList<Object[]>();            ResultSetMetaData rsmd = rs.getMetaData();            int column = rsmd.getColumnCount();                        while (rs.next()) {                Object[] ob = new Object[column];                for (int i = 1; i <= column; i++{                    ob[i - 1= rs.getObject(i);                }                al.add(ob);            }                        return al;                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteReaderList:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteReaderList-close:" + e.getMessage());            }        }    }            /** *//**     *      * ⑤     * 機能:存储过程有参数时,Select処理,返回一个SortedMap[];     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return 结果集(SortedMap[])     * @throws Exception     */    public static SortedMap[] ExecuteReaderMap(String cmdtext, String[] parms) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;        Result result = null;        SortedMap[] sortedMap = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);                        prepareCommand(pstmt, parms);            rs = pstmt.executeQuery();                        result = ResultSupport.toResult(rs);                        sortedMap = result.getRows();                        return sortedMap;                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteReaderMap-parms:" + e.getMessage());        } finally {            try {                if(rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteReaderMap-parms-close:" + e.getMessage());            }        }    }        /** *//**     *      * ⑥     * 機能:存储过程没有参数时,Select処理,返回一个结SortedMap[];     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @return 结果集(SortedMap[])     * @throws Exception     */    public static SortedMap[] ExecuteReaderMap(String cmdtext) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;        Result result = null;        SortedMap[] sortedMap = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);            rs = pstmt.executeQuery();                        result = ResultSupport.toResult(rs);                        sortedMap = result.getRows();                        return sortedMap;                    } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteReaderMap:" + e.getMessage());        } finally {            try {                if(rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteReaderMap-close:" + e.getMessage());            }        }    }        /** *//**     *      * ⑦     * 機能:存储过程有参数时,Select処理,返回结果集中的第一行的name所指定的列的值,当结果集为空时,返回null     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param name 需要取る列名     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return 结果集中的第一行的name所指定的列的值,当结果集为空时,返回null     * @throws Exception     */    public static Object ExecuteScalar(String cmdtext, String name,String[] parms) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);            prepareCommand(pstmt, parms);                        rs = pstmt.executeQuery();            if (rs.next()) {                return rs.getObject(name);            } else {                return null;            }        } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteScalar-name-parms:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteScalar-name-parms-close:" + e.getMessage());            }        }    }            /** *//**     *      * ⑧     * 機能:存储过程没有参数时,Select処理,返回结果集中的第一行的name所指定的列的值,当结果集为空时,返回null     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param name 需要取る列名     * @return 结果集中的第一行的name所指定的列的值,当结果集为空时,返回null     * @throws Exception     */    public static Object ExecuteScalar(String cmdtext, String name) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);                        rs = pstmt.executeQuery();            if (rs.next()) {                return rs.getObject(name);            } else {                return null;            }        } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteScalar-name:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteScalar-name-close:" + e.getMessage());            }        }    }            /** *//**     *      * ⑨     * 機能:存储过程有参数时,Select処理,返回结果集中的第一行的index所指定的列的值,当结果集为空时,返回null     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param index 需要取る列番号     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return 结果集中的第一行的index所指定的列的值,当结果集为空时,返回null     * @throws Exception     */    public static Object ExecuteScalar(String cmdtext, int index, String[] parms) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);            prepareCommand(pstmt, parms);                        rs = pstmt.executeQuery();            if (rs.next()) {                return rs.getObject(index);            } else {                return null;            }        } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteScalar-index-parms:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteScalar-index-parms-close:" + e.getMessage());            }        }    }            /** *//**     *      * ⑩     * 機能:存储过程没有参数时,Select処理,返回结果集中的第一行的index所指定的列的值,当结果集为空时,返回null     * @author libiao     * @param cmdtext SQL语句/存储过程名     * @param index 需要取る列番号     * @return 结果集中的第一行的index所指定的列的值,当结果集为空时,返回null     * @throws Exception     */    public static Object ExecuteScalar(String cmdtext, int index) throws Exception {        PreparedStatement pstmt = null;        Connection conn = null;        ResultSet rs = null;                try {            conn = getConnect();                        pstmt = conn.prepareStatement(cmdtext);                        rs = pstmt.executeQuery();            if (rs.next()) {                return rs.getObject(index);            } else {                return null;            }        } catch (Exception e) {            throw new Exception("SQLHepler-ExecuteScalar-index:" + e.getMessage());        } finally {            try {                if (rs != null)                    rs.close();                if (pstmt != null)                    pstmt.close();                if (conn != null)                    conn.close();            } catch (Exception e) {                throw new Exception("SQLHepler-ExecuteScalar-index-close:" + e.getMessage());            }        }    }    /** *//**     *      * 機能:分割参数数组,并且插入到PreparedStatement对象中     * @author libiao     * @param pstmt PreparedStatement対象     * @param parms 存储过程需要的参数(参数是以数组的形式)     * @return      * @throws Exception     */    private static void prepareCommand(PreparedStatement pstmt, String[] parms) throws Exception {        try {            if (parms != null{                for (int i = 0; i < parms.length; i++{                    /**//*                    try {                        pstmt.setString(i + 1, parms[i]);                    } catch (Exception errs) {                        System.out.print("SQLHelper-PrepareCommand ErrString:"+ errs);                    }*/                                        try {                        pstmt.setDate(i + 1, java.sql.Date.valueOf(parms[i]));                    } catch (Exception e) {                        try {                            pstmt.setDouble(i + 1, Double.parseDouble(parms[i]));                        } catch (Exception e1) {                            try {                                pstmt.setInt(i + 1, Integer.parseInt(parms[i]));                            } catch (Exception e2) {                                try {                                    pstmt.setString(i + 1, parms[i]);                                } catch (Exception errs) {                                    System.out.print("SQLHelper-PrepareCommand ErrString:"+ errs);                                }                            }                        }                    }                }            }        } catch (Exception e) {            System.out.print("SQLHelper-PrepareCommand Error:" + e);        }    }}

转载于:https://www.cnblogs.com/lblxiaoyu/archive/2008/04/07/1140509.html

相关资源:ASP.NET数据库操作类 SqlHelper.rar

最新回复(0)