📄 jdbcinputsynapse.java
字号:
/** * Sets the sQLQuery attribute of the JDBCInputSynapse object * * @param newSQLQuery The new database SQL Query. */ public void setSQLQuery(java.lang.String newSQLQuery) { // Check that it's actually changed. if (!SQLQuery.equals(newSQLQuery) ) { SQLQuery = newSQLQuery; this.resetInput(); this.setTokens(null); } // Check all params have been set then call the initInputStream to read the data in. /*if ( (driverName != null) && (!driverName.equals("")) ) if ( (dbURL != null) && (!dbURL.equals("")) ) if ( (SQLQuery != null) && (!SQLQuery.equals(""))) initInputStream(); */ } /** * Writes this JDBCSynapseInput object to the ObjectOutputStream out. * * @param out The ObjectOutputSteeam that this object should be written to * @exception IOException The Input Output Exception if any */ private void writeObject(ObjectOutputStream out) throws IOException { super.writeObjectBase(out); if (out.getClass().getName().indexOf("xstream") == -1) { out.writeObject(driverName); out.writeObject(dbURL); out.writeObject(SQLQuery); } } /** * Connects to the database using Driver name and db URL and selects data using the SQLQuery. */ protected void initInputStream() throws JooneRuntimeException { Connection con; // This will contain pattern set // Ensure all properties have been set if ((driverName != null) && (!driverName.equals(new String("")))) { // Check that Driver Name has been set if ((dbURL != null) && (!dbURL.equals(new String("")))) { // Check that Database URL has been set if ((SQLQuery != null) && (!SQLQuery.equals(new String("")))) { // Check that SQLQuery has been set try { Class.forName(driverName); // E.g sun.jdbc.odbc.JdbcOdbcDriver to use JDBC:ODBC bridge con = DriverManager.getConnection(dbURL); // URL if you have Fred ODBC Data source then e.g jdbc:odbc:Fred Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(SQLQuery); StringBuffer SQLNetInput = new StringBuffer(); while (rs.next()) { if (rs.getMetaData().getColumnCount() >= 1) { SQLNetInput.append(rs.getDouble(1)); // Get first Column } for (int counter = 2; counter <= rs.getMetaData().getColumnCount(); counter++) { // Loop through remaining columns SQLNetInput.append(";"); SQLNetInput.append(rs.getDouble(counter)); // Add data from ResultSet } SQLNetInput.append('\n'); // Add a new line after completion of a pattern } StreamInputTokenizer sit; if (getMaxBufSize() > 0) sit = new StreamInputTokenizer(new StringReader(SQLNetInput.toString()), getMaxBufSize()); else sit = new StreamInputTokenizer(new StringReader(SQLNetInput.toString())); super.setTokens(sit); } catch (ClassNotFoundException ex) { // Use Log4j log.error( "Could not find Database Driver Class while initializing the JDBCInputStream. Message is : " + ex.getMessage(), ex ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"Could not find Database Driver Class while initializing the JDBCInputStream. Message is : " + ex.getMessage()); //System.out.println("Could not find Database Driver Class while initializing the JDBCInputStream. Message is : " + ex.getMessage()); // SYS LOG } catch (SQLException sqlex) { // Use Log4j log.error( "SQLException thrown while initializing the JDBCInputStream. Message is : " + sqlex.getMessage(), sqlex ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"SQLException thrown while initializing the JDBCInputStream. Message is : " + sqlex.getMessage()); //System.out.println("SQLException thrown while initializing the JDBCInputStream. Message is : " + sqlex.getMessage()); // SYS LOG } catch (IOException ex) { // Use Log4j log.error( "IOException thrown while initializing the JDBCInputStream. Message is : " + ex.getMessage(), ex ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"IOException thrown while initializing the JDBCInputStream. Message is : " + ex.getMessage()); //System.out.println("IOException thrown while initializing the JDBCInputStream. Message is : " + ex.getMessage()); // SYS LOG } } else { // Warn the user or app that the SQLQuery has not been set String err = "The SQL Query has not been entered!"; log.warn( err ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"The SQL Query has not been entered!"); } } else { // Warn the user or app that the Database URL has not been set String err = "The Database URL has not been entered!"; log.warn( err ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"The Database URL has not been entered!"); } } else { // Warn user or app that the Driver Name has not been set String err = "The Driver Name has not been entered!"; log.warn( err ); // LOG4J if ( getMonitor() != null ) new NetErrorManager(getMonitor(),"The Driver Name has not been entered!"); } } /** * Check that parameters are set correctly for the this JDBCInputSynapse object. * * @see Synapse * @return validation errors. */ public TreeSet check() { // Get the parent's check messages. TreeSet checks = super.check(); Connection con; // To test the connection. // See if the driver name has been entered. if ((driverName == null)||(driverName.compareTo("")==0)) { checks.add(new NetCheck(NetCheck.FATAL, "Database Driver Name needs to be entered.", this)); } else { // Driver Name entered check that it is valid try { Class.forName(driverName); // Check driver class is correct E.g sun.jdbc.odbc.JdbcOdbcDriver to use JDBC:ODBC bridge // See if we can get a connection if there is a URL entered.... if ((dbURL != null)&&(dbURL.compareTo("")!=0)) { con = DriverManager.getConnection(dbURL); } } catch (ClassNotFoundException ex) { // Use Log4j checks.add(new NetCheck(NetCheck.FATAL, "Could not find Database Driver Class. Check Database Driver is in the classpath and is readable.",this)); // LOG4J } catch (SQLException sqlex) { checks.add(new NetCheck(NetCheck.FATAL, "The Database URL is incorrect. Connection error is : "+sqlex.toString(),this)); // LOG4J } } // Check that the dbURL has been entered. if ((dbURL == null)||(dbURL.compareTo("")==0)) { checks.add(new NetCheck(NetCheck.FATAL, "Database URL needs to be entered.", this)); } // Check that the SQLQuery has been entered if ((SQLQuery == null)||(SQLQuery.compareTo("")==0)) { checks.add(new NetCheck(NetCheck.FATAL, "Database SQL Query needs to be entered.", this)); } return checks; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -