[Java] 连接数据库的方法(Oracle DB2 SQL Server MySQL...) →→→→→进入此内容的聊天室

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

回复 "连接数据库的方法(Oracle DB2 SQL Server MySQL...)"

这儿你可以回复上面这条便签

captcha