baseDao()

 
 
//普通的baseDao
public class baseDao(){
    public Connection getConn(){
        Connection conn = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://losthost/3306/表名","root","root");
        }catch(ClassNotFoundException e){
                e.printStackTrace();
        }catch(SQLException e){
        e.printStackTrace();
        } 
        return conn;
    } 
    public void colseAll(ResultSet rs,Statement stat,Connection conn){
        try{
            if(rs != null)
                rs.close();
            if(stat != null)
                stat.close();
            if(conn != null)
                conn.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
//懒汉BaseDao
public class BaseDao(){
    private static String bdDriver = "com.mysql.jdbc.Driver";
    private static String  bdUrl = "jdbc:mysql://localhost:3306/表名";
    private static String  bdName = "root";
    private static String  bdpwd = "root";
    private static Connection conn = null;
    private BaseDao(){
        try{
            Class.forName(bdDriver);
            conn = DriverManger.getConnection(bdUrl,bdName,bdpwd);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    public static Connection getConn(){
        if(conn == null)
            new BaseDao();
        return conn;
    }

    public static void ColseAll(ResultSet rs,Statement stat){
       try{
            if(rs != null)
                rs.close();
            if(stat != null)
                stat.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
//饿汉BaseDao
public class BaseDao(){
    private static String bdDriver = "com.mysql.jdbc.Driver";
    private static String bdUrl = "jdbc:mysql://localhost:3306/表名";
    private static String bdName = "root";
    private static String bdPwd = "root";
    private static Connection conn = null;
    private BaseDao(){}
    static {
        try{
            Class.forName(bdDriver);
            conn = DriverManager.Connection(bdUrl,bdName,bdPwd);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    public static Connection getConn(){
        if(conn == null)
            new BaseDao();
        return conn;
    }
    public static void closeAll(ResultSet rs,Statement stat){
        try{
            if(rs != null)
                rs.close();
            if(stat != null)
                stat.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41565123/article/details/80738532