agtOracleToDomino.java import lotus.domino.*; import java.sql.*; import java.io.*; import java.util.Vector; import java.io.PrintWriter; import java.util.Enumeration; import java.text.*; public class JavaAgent extends AgentBase { private static Connection conn = null; private static String aJdbcStr; private static String aUsername; private static String aPassword; private static String aDrv; private static String aPath; public void NotesMain() { PrintWriter pw = getAgentOutput(); String ReturnMSG = null; Session session = null; AgentContext agentContext = null; Document docContext = null; Document doc = null; String strSQL = null; String strSQL2 = null; String strUNID = null; String strTITLE2 = null; //公民姓名 String strGM_NAME = null; Connection conn = null; Statement stmt = null; Statement stmt2 = null; Statement stmt3 = null; ResultSet rs = null; ResultSet rs2 = null; ResultSet rs3 = null; String sql = null; int bytesum=0; int byteread=0; String FileName = null; int Count = 0; /** *<br>方法说明:获得数据连接 *<br>输入参数: *<br>返回类型:Connection 连接对象 */ try { session = getSession(); agentContext = session.getAgentContext(); docContext = agentContext.getDocumentContext(); Database cdb = agentContext.getCurrentDatabase(); Agent agent = agentContext.getCurrentAgent(); Vector v2 = new Vector(); Document prodoc = cdb.getProfileDocument("Form_Profile", ""); aDrv = prodoc.getItemValueString("pro_Driver"); // 数据库驱动 aJdbcStr = prodoc.getItemValueString("pro_JdbcStr"); // 数据库连接字符串 aUsername = prodoc.getItemValueString("pro_Username"); // 数据库用户名 aPassword = prodoc.getItemValueString("pro_Password"); // 数据库密码 aPath = prodoc.getItemValueString("pro_Path"); // 数据库路径 System.out.println(aDrv); System.out.println(aJdbcStr); System.out.println(aUsername); //System.out.println(aPassword); // (Your code goes here) try { Class.forName(aDrv).newInstance(); conn = DriverManager.getConnection(aJdbcStr, aUsername, aPassword); }catch(ClassNotFoundException cnf){ System.out.println("driver not find:"+cnf); }catch(SQLException sqle){ System.out.println("can't connection db:"+sqle); } catch (Exception e) { System.out.println("Failed to load JDBC/ODBC driver."); } if(conn != null){ stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); strSQL = "SELECT * FROM oracletooa WHERE STATUS = '0'"; rs = stmt.executeQuery(strSQL); while(rs.next()){ strUNID = rs.getString("UNID"); //公民姓名 strGM_NAME = rs.getString("GM_NAME"); doc = cdb.createDocument(); doc.appendItemValue("Form", "tempDoc"); RichTextItem rtitem = doc.createRichTextItem("ACCESSORIES"); stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); strSQL2 = "SELECT * FROM ORACLETOOA_FILE WHERE UNID='" + strUNID + "' "; System.out.println(strSQL2); rs2 = stmt2.executeQuery(strSQL2); String titles = ""; String attname = ""; int ListAtt = 0; while(rs2.next()){ ListAtt = ListAtt + 1; Blob blob = rs2.getBlob("ACCESSORIES"); strTITLE2 = rs2.getString("TITLE"); titles = titles + "," + strTITLE2; if(ListAtt == 1) { attname = strTITLE2; } else { attname = attname + "~" + strTITLE2; } InputStream inStream = blob.getBinaryStream(); FileName = aPath + strTITLE2; FileOutputStream fs = new FileOutputStream(FileName); byte[] buffer = new byte[1444]; int length; while ((byteread = inStream.read(buffer)) != -1) { //out.println(byteread); bytesum += byteread; //System.out.println(bytesum); fs.write(buffer,0,byteread); } fs.close(); //File file = New File(FileName); rtitem.embedObject(EmbeddedObject.EMBED_ATTACHMENT,null, FileName, "附件"); File file = new File(FileName); file.delete(); } Vector v = session.evaluate("@Explode( /"" + titles + "/"; /",/")", doc); doc.appendItemValue("att_1", v); doc.appendItemValue("attname_1", attname); if (!doc.save()) System.out.println("Unable to save document"); doc.recycle(); doc=null; Count = Count + 1; } } //String htmlMsg = "<Script>" + "alert('" + "已成功导入了【" + Count + "】条数据!" + "');" + "self.close();</Script>"; //pw.println(htmlMsg); } catch(Exception e) { e.printStackTrace(); }finally{ try{ if (docContext != null){ docContext.recycle(); } if (agentContext != null){ agentContext.recycle(); } if(session != null){ session.recycle(); } if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } }catch(Exception e){ } } } } agtDominoToOracle.java import lotus.domino.*; import java.sql.*; import java.io.*; public class JavaAgent extends AgentBase { private static Connection conn = null; private static String aJdbcStr; private static String aUsername; private static String aPassword; private static String aDrv; public void NotesMain() { Session session = null; AgentContext agentContext = null; Document docContext = null; Document cdoc = null; String strSQL = null; String strname = null; String strgeneral = null; String strbir_year = null; String strbir_month = null; String strbir_day = null; Connection conn = null; Statement stmt = null; Statement stmt2 = null; Statement stmt3 = null; String sql = null; InputStream inStream = null; int iUpdate = 0; ResultSet rs = null; ResultSet rs3 = null; String srcFile = null; InputStream inps = null; byte[] bytes = null; /** *<br>方法说明:获得数据连接 *<br>输入参数: *<br>返回类型:Connection 连接对象 */ try { session = getSession(); agentContext = session.getAgentContext(); docContext = agentContext.getDocumentContext(); Database cdb = agentContext.getCurrentDatabase(); Agent agent = agentContext.getCurrentAgent(); cdoc = cdb.getDocumentByID(agent.getParameterDocID()); Document prodoc = cdb.getProfileDocument("Form_Profile", ""); aDrv = prodoc.getItemValueString("pro_Driver"); // 数据库驱动 aJdbcStr = prodoc.getItemValueString("pro_JdbcStr"); // 数据库连接字符串 aUsername = prodoc.getItemValueString("pro_Username"); // 数据库用户名 aPassword = prodoc.getItemValueString("pro_Password"); // 数据库密码 // (Your code goes here) try { Class.forName(aDrv).newInstance(); conn = DriverManager.getConnection(aJdbcStr, aUsername, aPassword); }catch(ClassNotFoundException cnf){ System.out.println("driver not find:"+cnf); }catch(SQLException sqle){ System.out.println("can't connection db:"+sqle); } catch (Exception e) { System.out.println("Failed to load JDBC/ODBC driver."); } if(conn != null){ String UNID = cdoc.getFirstItem("UNID").getText(); String TITLE = cdoc.getFirstItem("TITLE").getText(); String CREATED = cdoc.getFirstItem("CREATED").getText(); String CONTENT = cdoc.getFirstItem("CONTENT").getText(); String TYPE = cdoc.getFirstItem("TYPE").getText(); //得到所有的附件 java.util.Vector vtr = session.evaluate("@AttachmentNames",cdoc); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); strSQL = "SELECT * FROM GOVOPEN WHERE UNID='" + UNID + "' "; System.out.println(strSQL); rs = stmt.executeQuery(strSQL); if(rs.next()){ stmt3 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); sql="update GOVOPEN SET FLAG ='0', " + "TITLE = '" + TITLE + "', " + "CREATED = '" + CREATED + "', " + "CONTENT = '" + CONTENT + "', " + "TYPE = '" + TYPE + "' " + " where UNID='" + UNID + "' "; System.out.println("SQL " + sql); stmt3.executeUpdate(sql); //删除附件 sql="delete ACCESSORIES where UNID='" + UNID + "' AND TYPE = '1' "; System.out.println("SQL " + sql); stmt3.executeUpdate(sql); } else { //添加主记录 sql = "insert into GOVOPEN Values('"+ UNID+"','"+ TITLE+"','"+ CREATED+"','"+ CONTENT +"','1','0','')"; System.out.println(sql); iUpdate = stmt.executeUpdate(sql); } //循环取得附件 for (int i=0;i<vtr.size();i++){ srcFile = new String(vtr.get(i).toString()); System.out.println(srcFile); //拆离附件到服务器 EmbeddedObject ebd = cdoc.getAttachment(srcFile); if (ebd==null) continue; if (ebd.getType()!=EmbeddedObject.EMBED_ATTACHMENT) continue; ebd.extractFile(srcFile); ebd.remove(); ebd.recycle(); ebd=null; //创建文件流 inps = new FileInputStream(srcFile); int lenght = inps.available(); bytes = new byte[lenght]; stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //添加附件 sql = "insert into ACCESSORIES Values('"+ UNID+"'," + "empty_blob(),'" + srcFile + "', '1')"; System.out.println(sql); iUpdate = stmt2.executeUpdate(sql); conn.setAutoCommit(false); Statement stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); sql="select ACCESSORIES from ACCESSORIES where UNID='" + UNID + "' AND TITLE = '" + srcFile + "' AND TYPE = '1' for update "; System.out.println(sql); oracle.jdbc.OracleResultSet rs1 =(oracle.jdbc.OracleResultSet)stmt1.executeQuery(sql); if (rs1.next()) { oracle.sql.BLOB blob = rs1.getBLOB("ACCESSORIES"); OutputStream outStream = blob.getBinaryOutputStream(); int nbytes = 0; while((nbytes=inps.read(bytes))!=-1){ outStream.write(bytes,0,nbytes); } outStream.flush(); conn.commit(); outStream.close(); } } } } catch(Exception e) { e.printStackTrace(); }finally{ try{ if (docContext != null){ docContext.recycle(); } if (agentContext != null){ agentContext.recycle(); } if(session != null){ session.recycle(); } if(stmt != null){ stmt.close(); } if(stmt2 != null){ stmt2.close(); } if(stmt3 != null){ stmt3.close(); } if(conn != null){ conn.close(); } }catch(Exception e){ } } } }