有谁能给一个实际开发中的 数据库通用类 来参考? 
[email protected] 谢谢~!

解决方案 »

  1.   

    package commons;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class DbUtils {
        protected DbUtils() {
        }    public static void register(String driverName) {
            try {
                Class.forName(driverName);
            } catch (Exception ex) {
                throw new RuntimeException("Database Driver Register Error: "
                    + driverName, ex);
            }
        }    public static Connection getConn(String url, String username,
            String password) {
            try {
                return DriverManager.getConnection(url, username, password);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }    public static void close(Connection conn) {
            close(conn, null, null);
        }    public static void close(Connection conn, Statement state) {
            close(conn, state, null);
        }    public static void close(Connection conn, Statement state, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    System.err.println(ex);
                } finally {
                    rs = null;
                }
            }        if (state != null) {
                try {
                    state.close();
                } catch (Exception ex) {
                    System.err.println(ex);
                } finally {
                    state = null;
                }
            }        if (conn != null) {
                try {
                    conn.close();
                } catch (Exception ex) {
                    System.err.println(ex);
                } finally {
                    conn = null;
                }
            }
        }
    }