zoukankan      html  css  js  c++  java
  • Java-ConnectDB工具类

    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    /**
     * 功能描述:连接数据库-mysql/oracle
     */
    public class ConnectDB {
    
        private static final String MYSQL = "jdbc:mysql://";
    
        private static final String ORACLE = "jdbc:oracle:thin:@";
    
        private static final String SQLSERVER = "jdbc:microsoft:sqlserver://";
        private ConnectDB() {
        }
    
        public static Connection getConnection(String DBType, String url,
                String user, String password) throws SQLException {
            if ("mysql".equalsIgnoreCase(DBType))
                return getMySqlConn(url, user, password);
            if ("oracle".equalsIgnoreCase(DBType))
                return getOracleConn(url, user, password);
            if ("sqlserver".equals(DBType)){
                return getSqlServerConn(url, user, password);
            }
            return null;
        }
    
        public static void closeConn(Connection conn) {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        private static Connection getMySqlConn(String url, String user,
                String password) throws SQLException {
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");// 加载驱动
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            conn = DriverManager.getConnection(MYSQL + url, "root", "root");
    
            return conn;
        }
    
        private static Connection getOracleConn(String url, String user,
                String password) throws SQLException {
            Connection conn = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");// 加载驱动
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            conn = DriverManager.getConnection(ORACLE + url, "scott", "tiger");
    
            return conn;
        }
    
        private static Connection getSqlServerConn(String url, String user,
                String password) throws SQLException {
            Connection conn = null;
            try {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");// 加载驱动
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            conn = DriverManager.getConnection(SQLSERVER + url, "root", "root");
    
            return conn;
        }
        public static void main(String[] args) {
            try {
                Connection conn = getConnection("MySQL", "127.0.0.1", "root",
                        "root");
                if (conn == null) {
                    System.out.println("Connection the database is failled !");
                } else {
                    System.out.println(conn.toString());
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
  • 相关阅读:
    程序化广告交易中的点击率预估
    C++(三十七) — 字符串的函数重载—案例
    C++(三十六) — 等号操作符重载、&&,||不可重载
    C++(三十五) — 运算符重载
    C++(三十四) — 友元函数、友元类
    C++(三十三) — 全局函数、成员函数的区别
    C++(三十二) — 常对象、常成员变量、常成员函数
    C++(三十一) — 静态成员变量、静态成员函数
    C++(三十) — this 指针
    C++(二十九) — new 和 delete
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210966.html
Copyright © 2011-2022 走看看