首页 java数据 连接数据库-帮助类

连接数据库-帮助类

连接数据库 mysql ,oracle ,sqlserver import java.sql.Connection; i…

连接数据库 mysql ,oracle ,sqlserver

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 功能描述:连接数据库-mysql/oracle
 *
 * @author :FangHewei
 * @Date :Jul 18, 2008
 * @Time :3:28:59 PM
 * @version :1.0
 */
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, user, password);

		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, user, password);

		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, user, password);

		return conn;
	}
	public static void main(String[] args) {
		try {
			Connection conn = getConnection("MySQL", "127.0.0.1", "root",
					"123456");
			if (conn == null) {
				System.out.println("Connection the database is failled !");
			} else {
				System.out.println(conn.toString());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}

 

 

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

mysql  优化提升

mysql 优化提升

desc  分别使用DESCRIBE和DESC查看表tb dept1和表tb emp1的表结构。 查看表详细结构语句SH...
navicat 能打开root链接不知道密码直接修改mysql 密码

navicat 能打开root链接不知道密码直接修改mysql 密码

找到系统自带 mysql 数据库   update user set password= password(&...
Cannot access aliyunmaven ( xxx ) in offline mode and the artifact

Cannot access aliyunmaven ( xxx ) in offline mode and the artifact

Cannot access aliyunmaven ( xxx ) in offline mode and the ar...
mysql 基础入门

mysql 基础入门

第一章 SQL的介绍 1.1什么是sql SQL:Structure Query Language。(结构化查询语言),...
You can’t specify target table ‘caiji_data_meiwen’ for update in FROM clause

You can’t specify target table ‘caiji_data_meiwen’ for update in FROM clause

  mysql 中不能自己 引用本身 要再加一层 不行 DELETE from `caiji_data_mei...
返回顶部