《各种数据库的连接连接connection方法》由会员分享,可在线阅读,更多相关《各种数据库的连接连接connection方法(2页珍藏版)》请在金锄头文库上搜索。
1、package com.jdbc.myConnection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class ConnectionFactory/获取Oracle连接 public static Connection getOracleConnection() Connection con = null; try Class.forName(oracle.jdbc.OracleDriver); String url = jdbc:oracle:th
2、in:localhost:1521:orcl; con = DriverManager.getConnection(url, scott, tiger); catch (Exception e) e.printStackTrace(); return con; /获取SQLServer的test数据库连接 public static Connection getSQLServerConnection() return getMySQLConnection(test); /获取SQLServer 连接 public static Connection getSQLServerConnection
3、(String databaseName) Connection con = null; try Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver); String url = jdbc:microsoft:sqlServer:/localhost:1433;databaseName= + databaseName; con = DriverManager.getConnection(url, sa, sa); catch (Exception e) e.printStackTrace(); return con; /获取My
4、SQL连接 public static Connection getMySQLConnection() return getMySQLConnection(test); public static Connection getMySQLConnection(String databaseName) Connection con = null; try Class.forName(com.mysql.jdbc.Driver); String url = jdbc:mysql:/localhost/ + databaseName; con = DriverManager.getConnection(url, root, root); catch (Exception e) e.printStackTrace(); return con; /关闭连接 public static void close(Connection con) try con.close(); catch (SQLException e) e.printStackTrace();