| public class ImportSQL extends AgentBase { |
| private OpenLogItem oli; |
| |
| private Session session; |
| |
| private Database db; |
| |
| private Statement qryStatement; |
| |
| private Connection con; |
| |
| public void NotesMain() { |
| oli = new OpenLogItem(); |
| |
| session = getSession(); |
| |
| try { |
| AgentContext agentContext = session.getAgentContext(); |
| Agent agent = agentContext.getCurrentAgent(); |
| db = agentContext.getCurrentDatabase(); |
| Document doc = db.getDocumentByID(agent.getParameterDocID()); |
| if (doc != null) { |
| String dbServer = doc.getItemValueString("DBSERVER"); |
| String dbName = doc.getItemValueString("DBNAME"); |
| String query = doc.getItemValueString("QUERY"); |
| String debug = doc.getItemValueString("DEBUG"); |
| String connectionString = "jdbc:jtds:sqlserver://" + dbServer |
| + ":1433/" + dbName; |
| if (debug.equalsIgnoreCase("true")) |
| oli.logEvent("Connection string = " + connectionString, |
| OpenLogItem.SEVERITY_LOW, null); |
| Class.forName("net.sourceforge.jtds.jdbc.Driver"); |
| con = DriverManager.getConnection(connectionString); |
| |
| createStatement(); |
| ResultSet rs = getRows(query); |
| |
| ResultSetMetaData rsmd = rs.getMetaData(); |
| int i, n = rsmd.getColumnCount(); |
| |
| while (rs.next()) { |
| Document docCur = db.createDocument(); |
| docCur.appendItemValue("Form", "SQLImport"); |
| docCur.appendItemValue("Table", rsmd.getTableName(1)); |
| |
| for (i = 1; i <= n; i++) { |
| |
| |
| docCur.appendItemValue(rsmd.getColumnName(i), rs |
| .getString(i)); |
| |
| } |
| docCur.save(); |
| } |
| con.close(); |
| doc.remove(true); |
| } |
| } catch (Exception e) { |
| oli.logError(e); |
| } finally { |
| con = null; |
| oli.recycle(); |
| try { |
| session.recycle(); |
| } catch (NotesException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| } |
| |
| private void createStatement() { |
| try { |
| qryStatement = con.createStatement( |
| ResultSet.TYPE_SCROLL_INSENSITIVE, |
| ResultSet.CONCUR_READ_ONLY); |
| |
| } catch (SQLException e) { |
| oli.logError(e); |
| } |
| |
| } |
| |
| private ResultSet getRows(String sqlStatement) { |
| ResultSet rs = null; |
| |
| try { |
| rs = qryStatement.executeQuery(sqlStatement); |
| |
| } catch (SQLException e) { |
| oli.logError(e); |
| } |
| return rs; |
| |
| } |
| } |