xconnection.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,398 行 · 第 1/3 页
JAVA
1,398 行
} if (nName.equalsIgnoreCase("dburl")) { dbURL = ""; Node n1 = n.getFirstChild(); if (null != n1) { dbURL = n1.getNodeValue(); } } if (nName.equalsIgnoreCase("password")) { String s = ""; Node n1 = n.getFirstChild(); if (null != n1) { s = n1.getNodeValue(); } prop.put("password", s); } if (nName.equalsIgnoreCase("user")) { String s = ""; Node n1 = n.getFirstChild(); if (null != n1) { s = n1.getNodeValue(); } prop.put("user", s); } if (nName.equalsIgnoreCase("protocol")) { String Name = ""; NamedNodeMap attrs = n.getAttributes(); Node n1 = attrs.getNamedItem("name"); if (null != n1) { String s = ""; Name = n1.getNodeValue(); Node n2 = n.getFirstChild(); if (null != n2) s = n2.getNodeValue(); prop.put(Name, s); } } } while ( (n = n.getNextSibling()) != null); init(driver, dbURL, prop); } /** * Initilize is being called because we did not have an * existing Connection Pool, so let's see if we created one * already or lets create one ourselves. * @param driver * @param dbURL * @param prop * @return * @throws SQLException */ private void init( String driver, String dbURL, Properties prop )throws SQLException { Connection con = null; if (DEBUG) System.out.println("XConnection, Connection Init"); String user = prop.getProperty("user"); if (user == null) user = ""; String passwd = prop.getProperty("password"); if (passwd == null) passwd = ""; String poolName = driver + dbURL + user + passwd; ConnectionPool cpool = m_PoolMgr.getPool(poolName); if (cpool == null) { if (DEBUG) { System.out.println("XConnection, Creating Connection"); System.out.println(" Driver :" + driver); System.out.println(" URL :" + dbURL); System.out.println(" user :" + user); System.out.println(" passwd :" + passwd); } DefaultConnectionPool defpool = new DefaultConnectionPool(); if ((DEBUG) && (defpool == null)) System.out.println("Failed to Create a Default Connection Pool"); defpool.setDriver(driver); defpool.setURL(dbURL); defpool.setProtocol(prop); // Only enable pooling in the default pool if we are explicatly // told too. if (m_DefaultPoolingEnabled) defpool.setPoolEnabled(true); m_PoolMgr.registerPool(poolName, defpool); m_ConnectionPool = defpool; } else { m_ConnectionPool = cpool; } m_IsDefaultPool = true; // // Let's test to see if we really can connect // Just remember to give it back after the test. // try { con = m_ConnectionPool.getConnection(); } catch(SQLException e) { if (con != null) { m_ConnectionPool.releaseConnectionOnError(con); con = null; } throw e; } finally { m_ConnectionPool.releaseConnection(con); } } /** * Execute a query statement by instantiating an * @param exprContext * @param queryString the SQL query. * @return XStatement implements NodeIterator. * @throws SQLException * @link org.apache.xalan.lib.sql.XStatement XStatement} * object. The XStatement executes the query, and uses the result set * to create a * @link org.apache.xalan.lib.sql.RowSet RowSet}, * a row-set element. */ public DTM query( ExpressionContext exprContext, String queryString ) { Connection con = null; Statement stmt = null; ResultSet rs = null; DTMManagerDefault mgrDefault = null; SQLDocument doc = null; try { if (DEBUG) System.out.println("query()"); if (null == m_ConnectionPool) { // Build an Error Document, NOT Connected return null; } try { con = m_ConnectionPool.getConnection(); stmt = con.createStatement(); rs = stmt.executeQuery(queryString); } catch(SQLException e) { // We have not created a document yet, so lets close the // connection ourselves then let the process deal with the // error. // try { if (null != rs) rs.close(); } catch(Exception e1) {} try { if (null != stmt) stmt.close(); } catch(Exception e1) { } try { if (null != con) m_ConnectionPool.releaseConnectionOnError(con); } catch(Exception e1) { } buildErrorDocument(exprContext, e); return null; } if (DEBUG) System.out.println("..creatingSQLDocument"); DTMManager mgr = ((XPathContext.XPathExpressionContext)exprContext).getDTMManager(); mgrDefault = (DTMManagerDefault) mgr; int dtmIdent = mgrDefault.getFirstFreeDTMID(); doc = new SQLDocument( mgr, dtmIdent << DTMManager.IDENT_DTM_NODE_BITS , m_ConnectionPool, con, stmt, rs, m_IsStreamingEnabled); if (null != doc) { if (DEBUG) System.out.println("..returning Document"); // Register our document mgrDefault.addDTM(doc, dtmIdent); // also keep a local reference m_OpenSQLDocuments.addElement(doc); return doc; } else { return null; } } catch(SQLException e) { if ((doc != null) && (mgrDefault != null)) { doc.closeOnError(); mgrDefault.release(doc, true); } buildErrorDocument(exprContext, e); return null; } catch (Exception e) { if ((doc != null) && (mgrDefault != null)) { doc.closeOnError(); mgrDefault.release(doc, true); } if (DEBUG) System.out.println("exception in query()"); buildErrorDocument(exprContext, e); return null; } finally { if (DEBUG) System.out.println("leaving query()"); } } /** * Execute a parameterized query statement by instantiating an * @param exprContext * @param queryString the SQL query. * @return XStatement implements NodeIterator. * @throws SQLException * @link org.apache.xalan.lib.sql.XStatement XStatement} * object. The XStatement executes the query, and uses the result set * to create a * @link org.apache.xalan.lib.sql.RowSet RowSet}, * a row-set element. */ public DTM pquery( ExpressionContext exprContext, String queryString ) { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { int indx; try { con = m_ConnectionPool.getConnection(); stmt = con.prepareStatement(queryString); } catch(SQLException e) { // We have not created a document yet, so lets close the // connection ourselves then let the process deal with the // error. // try { if (null != stmt) stmt.close(); } catch(Exception e1) { } try { if (null != con) m_ConnectionPool.releaseConnectionOnError(con); } catch(Exception e1) {} // Re throw the error so the process can handle the error // normally throw e; } if (DEBUG) System.out.println("..building Prepared Statement"); try { Enumeration enum = m_ParameterList.elements(); indx = 1; while (enum.hasMoreElements()) { QueryParameter qp = (QueryParameter) enum.nextElement(); setParameter(indx, stmt, qp); indx++; } rs = stmt.executeQuery(); } catch(SQLException e) { // We have not created a document yet, so lets close the // connection ourselves then let the process deal with the // error. // try { if (null != rs) rs.close(); } catch(Exception e1) { } try { if (null != stmt) stmt.close(); } catch(Exception e1) { } try { if (null != con) m_ConnectionPool.releaseConnectionOnError(con); } catch(Exception e1) { } // Re throw the error so the process can handle the error // normally throw e; } if (DEBUG) System.out.println("..creatingSQLDocument"); DTMManager mgr = ((XPathContext.XPathExpressionContext)exprContext).getDTMManager(); DTMManagerDefault mgrDefault = (DTMManagerDefault) mgr; int dtmIdent = mgrDefault.getFirstFreeDTMID(); SQLDocument doc = new SQLDocument(mgr, dtmIdent << DTMManager.IDENT_DTM_NODE_BITS, m_ConnectionPool, con, stmt, rs, m_IsStreamingEnabled); if (null != doc) { if (DEBUG) System.out.println("..returning Document"); // Register our document mgrDefault.addDTM(doc, dtmIdent); // also keep a local reference m_OpenSQLDocuments.addElement(doc); return doc; } else { // Build Error Doc, BAD Result Set return null; } } catch(SQLException e) { buildErrorDocument(exprContext, e); return null; } catch (Exception e) { buildErrorDocument(exprContext, e); return null; } } /** * Execute a parameterized query statement by instantiating an * @param exprContext * @param queryString the SQL query. * @param typeInfo * @return XStatement implements NodeIterator. * @throws SQLException * @link org.apache.xalan.lib.sql.XStatement XStatement} * object. The XStatement executes the query, and uses the result set * to create a * @link org.apache.xalan.lib.sql.RowSet RowSet}, * a row-set element. * This method allows for the user to pass in a comma seperated * String that represents a list of parameter types. If supplied * the parameter types will be used to overload the current types * in the current parameter list. */ public DTM pquery( ExpressionContext exprContext, String queryString, String typeInfo ) { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { int indx; // Parse up the parameter types that were defined // with the query StringTokenizer plist = new StringTokenizer(typeInfo); // Override the existing type that is stored in the // parameter list. If there are more types than parameters // ignore for now, a more meaningfull error should occur // when the actual query is executed. indx = 0; while (plist.hasMoreTokens()) { String value = plist.nextToken(); QueryParameter qp = (QueryParameter) m_ParameterList.elementAt(indx); if ( null != qp ) { qp.setType(value); } indx++; } try { con = m_ConnectionPool.getConnection(); stmt = con.prepareStatement(queryString); } catch(SQLException e) { // We have not created a document yet, so lets close the // connection ourselves then let the process deal with the // error. // try { if (null != stmt) stmt.close(); } catch(Exception e1) { } try { if (null != con) m_ConnectionPool.releaseConnectionOnError(con); } catch(Exception e1) { } // Re throw the error so the process can handle the error // normally throw e; } if (DEBUG) System.out.println("..building Prepared Statement"); try { Enumeration enum = m_ParameterList.elements(); indx = 1; while (enum.hasMoreElements()) { QueryParameter qp = (QueryParameter) enum.nextElement(); setParameter(indx, stmt, qp); indx++; } rs = stmt.executeQuery(); } catch(SQLException e) { // We have not created a document yet, so lets close the // connection ourselves then let the process deal with the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?