日期:2014-05-16  浏览次数:20545 次

访问远程oracle数据库方法汇总

写之前,先搞清楚我们的目的。大前提是oracle数据库没在本机上;下面以目的分为两类:

1.使PL/SQL Developer能连接上数据库。

方法一:安装oracle client(客户端)

参考:直接下载oracle client安装或者使用oracle安装盘安装时选择客户端。

方法二:安装Instant Client

参考:http://blog.csdn.net/helifengwell/article/details/5820434

方法三:手动操作

参考:http://zhidao.baidu.com/question/215308248.html

2.写代码连接数据库

此处就比较简单了,导入所用语言相应的库文件,直接coding即可。

例如:Java导入oracle-10.2.0.1-jdbc14.jar包即可。

下面顺便share一段java连接oracle的代码(log4j的配置文件就不上传了,需要的说一声):

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class BaseDAO {
    //log4j
    private static final Log log = LogFactory.getLog(BaseDAO.class);
	
    private static String DB_URL = "jdbc:oracle:thin:@192.168.1.169:1521:test";
    private static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static String DB_USERNAME = "test";
    private static String DB_PASSWORD = "test";
    
    // 用户拼接DB.properties的绝对路径,子类中赋值
    public static String basePath = "";
    
    /**
     * 获取数据库连接
     * 
     * @return
     */
    public static Connection getConnection(){
    	// 从配置文件中读取配置
    	Properties p = new Properties();
    	InputStream in = null;
    	try {
    		System.out.println("basePath = " + basePath);
    		log.info("Current directory is : " + basePath);
		in = new BufferedInputStream(new FileInputStream(basePath+"WEB-INF/classes/DB.properties"));
		p.load(in);
		log.info("Load "+basePath+"WEB-INF/classes/DB.properties succeeded.");
	} catch (Exception e) {
		log.error("Load "+basePath+"WEB-INF/classes/DB.properties failed.");
		System.out.println("Load "+basePath+"WEB-INF/classes/DB.properties failed.");
		e.printStackTrace();
	} 
	DB_URL = p.getProperty("DB_URL", "jdbc:oracle:thin:@192.168.1.169:1521:test");
	DB_USERNAME = p.getProperty("DB_USERNAME", "test");
	DB_PASSWORD = p.getProperty("DB_PASSWORD", "test");
	log.info("DB props : "+DB_URL+" "+DB_USERNAME+" "+DB_PASSWORD);
	System.out.println("DB props : "+DB_URL+" "+DB_USERNAME+" "+DB_PASSWORD);
    	
        Connection conn = null;
        try{
            // 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它
            Class.forName(DB_DRIVER);
            // 通过DriverManager的getConnection()方法获取数据库连接
            conn = DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);
            log.info("getConnection ok.");
        }catch(Exception ex){
            log.error("getConnection error.", ex);
            ex.printStackTrace();
        }
        return conn;
    }
    
    /**
     * 关闭数据库连接
     * 
     * @param conn
     */
    public static void closeConnection(Connection conn){
        try{
            if(conn!=null){
                // 判断当前连接,如果没有被关闭就关闭
                if(!conn.isClosed()){
                    conn.close();
                    log.info("connection close ok.");
                }
            }
        }catch(Exception ex){
            log.error("conncetion close error.", ex);
            ex.printStackTrace();
        }
    }
    
    public void insert(Object o) {
    	log.info("BaseDAO insert.");
    }
    
    public void delete(Object o) {
    	log.info("BaseDAO delete.");
    }
    
    public void update(Object o) {
    	log.info("BaseDAO update.");
    }
    
    public Object findAll() {
    	log.info("BaseDAO findAll.");
    	return null;
    }
}

VB6连接oracle的示例: