⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnection.java

📁 基于SSH (struts+spring+hibernate)框架设计的 CRM客户关系管理系统
💻 JAVA
字号:
package com.csu.crm.common;

import java.sql.*;
import java.net.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;

/**
 * 连接数据库
 * 
 * @author 3eCRM小组:曾东
 * @since Oct 3, 2007 9:33:57 AM
 * @version 1.0 创建时间:Oct 3, 2007 9:33:57 AM,初始版本
 */
public class DBConnection {

	private  Connection conn = null;
	public static final int ORACLE_LOCAL_ZD = 0;
	public static final int WEBLOGIC_JDBCPOOL = 1;
	public static final int ORACLE_LOCAL_LHB = 2;
	
	/**
	 * 快速连接数据库
	 * @param type 连接数据库方法类型
	 * @return Connection
	 */
	public  Connection getConnection(int type) {
		switch(type){
		case ORACLE_LOCAL_LHB:return getConnection("oracle","192.168.1.167","1521","ibss","lhb","lhb");
		case ORACLE_LOCAL_ZD:return getConnection("oracle","localhost","1521","zd","system","zd");
		case WEBLOGIC_JDBCPOOL:try {
					return getConnection("ds");
				} catch (Exception e) {	e.printStackTrace();}
		}
		return null;
	}
	
	
	/**
	 * 使用Oracle9i获得连接
	 * @param dbtype 数据库类型(这里只能是Oracle)
	 * @param servip 服务器ip
	 * @param port	服务器端口号
	 * @param db 数据库名字
	 * @param usr 用户名
	 * @param pwd 密码
	 * @return Connection
	 */
	public  Connection getConnection(String dbtype, String servip,
			String port, String db, String usr, String pwd) {
		Connection con = null;
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@" + servip + ":" + port + ":" + db;
		System.out.println(url);
		try {
			Class.forName(driver);// Connection的registerDriver(Driver driver)
			con = DriverManager.getConnection(url, usr, pwd);
			if (con != null) {
				System.out.println("数据库连接成功");
			} else {
				System.out.println("数据库连接失败");
			}
		} catch (ClassNotFoundException e) {
			System.out.println("fail 1");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("fail 2");
			System.out.println(e.getMessage());
		}
		return con;
	}
	
	/**
	 * 使用Weblogic中的连接池来获得连接
	 * 
	 * @param dataSource 数据源名称
	 * @return Connection
	 * @throws Exception
	 */
	public  Connection getConnection(String dataSource) throws Exception {
		try {
			Hashtable env = new Hashtable();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"weblogic.jndi.WLInitialContextFactory");
			env.put(Context.PROVIDER_URL, "t3://localhost:7001");
			Context ctx = new InitialContext(env);
			DataSource ds = (DataSource) ctx.lookup(dataSource);
			conn = ds.getConnection();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new Exception("获取连接错误!原因:" + ex.getMessage());
		}
		return conn;
	}

	public  void closeStatement(Statement st) {
		if (st != null) {
			try {
				st.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public  void closeConnection(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public  void closeResult(ResultSet st) {
		if (st != null) {
			try {
				st.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
	
	public  void main(String[] args) {
		getConnection(DBConnection.ORACLE_LOCAL_ZD);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -