1. 程式人生 > >jdbc操作關係資料庫(oracle)

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());
        }
    }
}