package com.connor.bhdz.pdm.partToCapp;

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

import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.soaictstubs.booleanSeq_tHolder;

public class ConnOra {
	static String driverClass = "oracle.jdbc.driver.OracleDriver"; // 更新驱动类名
	private String url ;//= "jdbc:oracle:thin:@10.41.71.60:1521:orcl"; // port sid
    private String user ;//= "system";   // 用户是数据库的用户名
    private String password ;//= "Aa12345678";  // 用户登录密码
    
    Connection conn=null;
    ResultSet resultSet = null;
    Statement statement;
    
    public ConnOra(TCSession session) throws TCException {
		// TODO Auto-generated constructor stub
    	TCPreferenceService preferenceService = session.getPreferenceService();
    	String[] values = preferenceService.getStringValues("BH_CAPP_Oracle");
    	if (values.length != 4) {
			System.out.println("未正确获取到首选项信息！");
		}else {
			this.url = "jdbc:oracle:thin:@"+values[0]+":1521/"+values[1];//10.41.71.60  orcl
			this.user = values[2];
			this.password = values[3];	
			System.out.println("获取连接Oracle数据库信息成功！\n url = "+this.url+"\n user = "+this.user+"\n pw = "+this.password);

		}	
	}
    

    public Connection getconn() throws SQLException {
        System.out.println("...连接Oracle工具开始执行");
        
        try {
            //首先建立驱动
            Class.forName(driverClass);//反射
            //驱动成功后进行连接
            conn=DriverManager.getConnection(url, user, password);
            System.out.println("连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
        return conn; //返回一个连接
    }
    
    /**
     * 查询
     * @param sql
     * @return
     * @throws SQLException
     */
    public ResultSet query(String sql) throws SQLException {
    	System.out.println("开始执行query方法");
    	
    	conn = getconn();
    	try {
    		System.out.println("开始组织并执行具体的sql语句···");
    		statement = conn.createStatement();
        	resultSet = statement.executeQuery(sql);
		} catch (SQLException e) {
			 e.printStackTrace();
		} 
		return resultSet;
	}
    
    public void closeConn() {
    	try {
    		conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
	}
    
    /**
     * 删除 or 更新 or 插入
     * @param sql 
     * @return
     * @throws SQLException
     */
    public boolean delOrUp(String sql) throws SQLException {
		conn = getconn();
		boolean actResult = false;
    	try {
    		Statement statement = conn.createStatement();
    		int i = statement.executeUpdate(sql);
    		if (i>0) {
				actResult = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
		}
    	return actResult;
	}
}
