码迷,mamicode.com
首页 > 数据库 > 详细

DB的封装

时间:2016-03-07 20:45:00      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

public class DB
{
    
    public static Connection getConn()
    {
        Connection conn=null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bbs", "root", "root");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return conn;
    }
    
    public static Statement getStatement(Connection conn)
    {
        Statement stmt=null;
        try
        {
            stmt=conn.createStatement();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return stmt;
    }
    
    public static ResultSet executeQuery(Statement stmt,String sql)
    {
        ResultSet rs=null;
        try
        {
            rs=stmt.executeQuery(sql);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return rs;
    }
    
    public static void close(Connection conn)
    {
        if(conn != null)
            try
            {
                conn.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
    }
    
    public static void close(Statement stmt)
    {
        if(stmt != null)
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
    }
    
    public static void close(ResultSet rs)
    {
        if(rs != null)
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
    }
}

 

DB的封装

标签:

原文地址:http://www.cnblogs.com/liu-Gray/p/5251663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!