1. package book.database;  
  2.  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6.  
  7. /**  
  8.  * 连接各类数据库的方法  
  9.  */ 
  10. public class DBConnector {  
  11.  /**  
  12.   * 获得数据库连接  
  13.   * @param driverClassName 连接数据库用到的驱动类的类名  
  14.   * @param dbURL  数据库的URL  
  15.   * @param userName 登陆数据库的用户名  
  16.   * @param password 登陆数据库的密码  
  17.   * @return  
  18.   * @throws ClassNotFoundException  
  19.   * @throws SQLException  
  20.   */ 
  21.  public static Connection getConnection(String driverClassName,  
  22.    String dbURL, String userName, String password)  
  23.    throws ClassNotFoundException, SQLException {  
  24.   Connection con = null;  
  25.  
  26.   // 加载连接数据库的驱动类  
  27.   Class.forName(driverClassName);  
  28.   // 用用户名、密码连接数据库  
  29.   con = DriverManager.getConnection(dbURL, userName, password);  
  30.  
  31.   return con;  
  32.  }  
  33.    
  34.  /**  
  35.   * 获得Oracle数据库的连接  
  36.   * @param dricerClassName 连接数据库用到的驱动类的类名  
  37.   * @param serverHost 数据库所在服务器的IP或域名  
  38.   * @param serverPort 数据库所在服务器的端口  
  39.   * @param dbName  数据库名  
  40.   * @param userName  登陆数据库的用户名  
  41.   * @param password  登陆数据库的密码  
  42.   * @return  
  43.   * @throws ClassNotFoundException  数据库驱动类无法找到是抛出该异常  
  44.   * @throws SQLException  创建连接时可能抛出该异常  
  45.   */ 
  46.  public static Connection getOracleConnection(String dricerClassName,  
  47.    String serverHost, String serverPort, String dbName,  
  48.    String userName, String password) throws ClassNotFoundException,  
  49.    SQLException {  
  50.   // 如果没有提供这些连接参数,则用默认值  
  51.   if (dricerClassName == null) {  
  52.    dricerClassName = "oracle.jdbc.driver.OracleDriver";  
  53.   }  
  54.   if (serverHost == null) {  
  55.    serverHost = "127.0.0.1";  
  56.   }  
  57.   if (serverPort == null) {  
  58.    serverPort = "1521";  
  59.   }  
  60.   // 构建访问Oracle数据库的URL  
  61.   String dbURL = "jdbc:oracle:thin:@" + serverHost + ":" + serverPort  
  62.     + ":" + dbName;  
  63.   return getConnection(dricerClassName, dbURL, userName, password);  
  64.  }  
  65.    
  66.  /**  
  67.   * 获得DB2数据库的连接  
  68.   */ 
  69.  public static Connection getDB2Connection(String dricerClassName,  
  70.    String serverHost, String serverPort, String dbName,  
  71.    String userName, String password) throws ClassNotFoundException,  
  72.    SQLException {  
  73.   // 如果没有提供这些连接参数,则用默认值  
  74.   if (dricerClassName == null) {  
  75.    dricerClassName = "com.ibm.db2.jdbc.app.DB2Driver";  
  76.   }  
  77.   if (serverHost == null) {  
  78.    serverHost = "127.0.0.1";  
  79.   }  
  80.   if (serverPort == null) {  
  81.    serverPort = "5000";  
  82.   }  
  83.   // 构建访问DB2数据库的URL  
  84.   String dbURL = "jdbc:db2://" + serverHost + ":" + serverPort  
  85.     + "/" + dbName;  
  86.   return getConnection(dricerClassName, dbURL, userName, password);  
  87.  }  
  88.    
  89.  /**  
  90.   * 获得SQL Server数据库的连接  
  91.   */ 
  92.  public static Connection getSQLServerConnection(String dricerClassName,  
  93.    String serverHost, String serverPort, String dbName,  
  94.    String userName, String password) throws ClassNotFoundException,  
  95.    SQLException {  
  96.   // 如果没有提供这些连接参数,则用默认值  
  97.   if (dricerClassName == null) {  
  98.    dricerClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";  
  99.   }  
  100.   if (serverHost == null) {  
  101.    serverHost = "127.0.0.1";  
  102.   }  
  103.   if (serverPort == null) {  
  104.    serverPort = "1433";  
  105.   }  
  106.   // 构建访问SQL Server数据库的URL  
  107.   String dbURL = "jdbc:microsoft:sqlserver://" + serverHost + ":" + serverPort  
  108.     + "; DatabaseName=" + dbName;  
  109.   return getConnection(dricerClassName, dbURL, userName, password);  
  110.  }  
  111.    
  112.  /**  
  113.   * 获得MySQL数据库的连接  
  114.   */ 
  115.  public static Connection getMySQLConnection(String dricerClassName,  
  116.    String serverHost, String serverPort, String dbName,  
  117.    String userName, String password) throws ClassNotFoundException,  
  118.    SQLException {  
  119.   // 如果没有提供这些连接参数,则用默认值  
  120.   if (dricerClassName == null) {  
  121.    dricerClassName = "com.mysql.jdbc.Driver";  
  122.   }  
  123.   if (serverHost == null) {  
  124.    serverHost = "127.0.0.1";  
  125.   }  
  126.   if (serverPort == null) {  
  127.    serverPort = "3306";  
  128.   }  
  129.   // 构建访问SQL Server数据库的URL  
  130.   String dbURL = "jdbc:mysql://" + serverHost + ":" + serverPort  
  131.     + "/" + dbName;  
  132.   return getConnection(dricerClassName, dbURL, userName, password);  
  133.  }  
  134.    
  135.  /**  
  136.   * 获得Sybase数据库的连接  
  137.   */ 
  138.  public static Connection getSybaseConnection(String dricerClassName,  
  139.    String serverHost, String serverPort, String dbName,  
  140.    String userName, String password) throws ClassNotFoundException,  
  141.    SQLException {  
  142.   // 如果没有提供这些连接参数,则用默认值  
  143.   if (dricerClassName == null) {  
  144.    dricerClassName = "com.sybase.jdbc3.jdbc.SybDriver";  
  145.   }  
  146.   if (serverHost == null) {  
  147.    serverHost = "127.0.0.1";  
  148.   }  
  149.   if (serverPort == null) {  
  150.    serverPort = "5007";  
  151.   }  
  152.   // 构建访问SQL Server数据库的URL  
  153.   String dbURL = "jdbc:sybase:Tds:" + serverHost + ":" + serverPort  
  154.     + "/" + dbName;  
  155.   return getConnection(dricerClassName, dbURL, userName, password);  
  156.  }  
  157.    
  158.  /**  
  159.   * 获得PostgreSQL数据库的连接  
  160.   */ 
  161.  public static Connection getPostgreSQLConnection(String dricerClassName,  
  162.    String serverHost, String serverPort, String dbName,  
  163.    String userName, String password) throws ClassNotFoundException,  
  164.    SQLException {  
  165.   // 如果没有提供这些连接参数,则用默认值  
  166.   if (dricerClassName == null) {  
  167.    dricerClassName = "org.postgresql.Driver";  
  168.   }  
  169.   if (serverHost == null) {  
  170.    serverHost = "127.0.0.1";  
  171.   }  
  172.   if (serverPort == null) {  
  173.    serverPort = "5432";  
  174.   }  
  175.   // 构建访问SQL Server数据库的URL  
  176.   String dbURL = "jdbc:postgresql://" + serverHost + ":" + serverPort  
  177.     + "/" + dbName;  
  178.   return getConnection(dricerClassName, dbURL, userName, password);  
  179.  }  
  180.  
  181.  public static void main(String[] args) throws ClassNotFoundException,   
  182.    SQLException {  
  183.   // 获得本地MySQL的连接实例,使用MySQL需要去www.mysql.com下载最新的MySQL安装程序和Java驱动  
  184.   // MySQL有多个连接MySQL的驱动类,如org.gjt.mm.mysql.Driver。  
  185.   // 这里使用MySQL官方网站上提供的驱动类  
  186.   String mySQLDirver = "com.mysql.jdbc.Driver";  
  187.   String dbName = "studentdb";  
  188.   String userName = "test";  
  189.   String password = "test";  
  190.   Connection con = DBConnector.getMySQLConnection(mySQLDirver,  
  191.     nullnull, dbName, userName, password);  
  192.   System.out.println("连接MySQL数据库成功!");  
  193.   con.close();  
  194.   System.out.println("成功关闭与MySQL数据库的连接!");  
  195.   String url = "jdbc:mysql://127.0.0.1:3306/" +  dbName;  
  196.   con = DBConnector.getConnection(mySQLDirver, url, userName, password);  
  197.   System.out.println("连接MySQL数据库成功!");  
  198.   con.close();  
  199.   System.out.println("成功关闭与MySQL数据库的连接!");  
  200.  }  
  201. }  
  202.