jdbc操作關係數據庫(oracle)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import java.text.DecimalFormat;
import java.util.Vector;

import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.Session;
import lotus.domino.View;

public class SavetToCmms extends AgentBase{

    private Session       session      = null;

    private AgentContext  agentContext = null;

    private Database      db           = null;

    private Connection    conn         = null;

    private Document      currDoc          = null;

    //private StringBuffer  html         = new StringBuffer("<br>");

    private DecimalFormat df;

    // 定義oracle數據庫連接的domino數據庫
    private static String dbName       = "sys_dbconfig.nsf";

    private static String strView      = "Sysconfig";

    private Database      dbConn       = null;

    // 數據庫連接參數
    private String        databaseName = "";

    private String        driverType   = "thin";

    private String        netProtocol  = "tcp";

    private String        password     = "";

    private String        serverName   = "";

    private String        serverPort   = "1521";

    private String        userName     = "";

    // 統計條件
    private String        module       = "";

    private String        deptCode     = "";

    private String        startTime    = "";

    private String        endTime      = "";

    public void NotesMain (){
        try{
            session = getSession();
            agentContext = session.getAgentContext();
            db = agentContext.getCurrentDatabase();
            currDoc = agentContext.getDocumentContext();

            // 初始化oracle數據庫連接
            this.initOracleConn();


            // 生成每個模塊的數據
            this.SaveToDb();

 
        } catch (Exception e){
            e.printStackTrace();
            try{
                currDoc.replaceItemValue("Str_msg", e.getMessage());
            } catch (Exception ee){
            }
        } finally{
            try{
                conn.close();
            } catch (Exception fe){
            }
            conn = null;
            try{
                session.recycle();
            } catch (Exception fe){
            }
            session = null;
        }
    }  

    // 取Oracle連接參數
    private String getParameter (View view, String Parameter){
        String returnValue = "";
        try{
            Document docCFG = view.getDocumentByKey(Parameter);
            if (docCFG != null){
                returnValue = docCFG.getItemValueString("Configvalue");
            }
        } catch (Exception e){
            e.printStackTrace();
            return "";
        }
        return returnValue;
    }

    // 取總量
    private void SaveToDb(){
     boolean hasOracleResultSet = false;
        Statement stmt = null; // 數據庫Statement
        ResultSet rs = null;
        String rtn = "";
       
        try{
            //String sql = "SELECT * from INTF_INFO";
                 
           // stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);  
         //  stmt = conn.createStatement();
          // rs = stmt.executeQuery(sql);
      PreparedStatement pstmt=conn.prepareStatement("insert into intf_info_biz(INFO_ID,INFO_NAME,PROD_ID,PROD_TYPE,PROD_NUM,PROD_INCOME) values(?,?,?,?,?,?)");
  pstmt.setInt(1,Integer.parseInt(currDoc.getItemValueString("LiuShuiHao")));
  pstmt.setString(2,currDoc.getItemValueString("projectname"));
  pstmt.setString(3,"業務小類名稱");
  pstmt.setString(4,"業務大類名稱");
  pstmt.setInt(5,100);
  pstmt.setInt(6,1000);
      pstmt.execute();
        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try{
                rs.close();
            } catch (Exception e){
            }
            try{
                stmt.close();
            } catch (Exception e){
            }
        }
    }

    // 初始化oracle數據庫連接
    private void initOracleConn () throws Exception{
        try{
            // 取得系統數據目錄
            String strPath = db.getFilePath().toString().substring(
                                                                   0,
                                                                   db.getFilePath().length()
                                                                           - db.getFileName()
                                                                                   .length());

            // Oracle數據庫連接的配置庫
            dbConn = session.getDatabase(session.getServerName(), strPath + dbName, false);

            // 取oracle數據庫連接參數
            View view = dbConn.getView(strView);
            serverName = getParameter(view, "ServerName");
            databaseName = getParameter(view, "DatabaseName");
            serverPort = getParameter(view, "ServerPort");
            driverType = getParameter(view, "DriverType");
            netProtocol = getParameter(view, "NetProtocol");
            userName = getParameter(view, "UserName");
            password = getParameter(view, "PassWord");

            // 建立Oracle數據庫連接
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@" + serverName + ":" + serverPort
                    + ":" + databaseName, userName, password);

        } catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章