📄 querysender.java
字号:
statementResult.setUpdateCount(updateCount); } useCount++; statementResult.setSqlWarning(stmnt.getWarnings()); return statementResult; } catch (SQLException e) { statementResult.setSqlException(e); } /* finally { if (stmnt != null) { stmnt.close(); stmnt = null; } closeConnection(conn); } */ return statementResult; } /** <p>Executes the specified query and returns 0 if this * executes successfully. If an exception occurs, -1 is * returned and the relevant error message, if available, * assigned to this object for retrieval. This will * typically be called for a CREATE PROCEDURE/FUNCTION * call. * * @param the SQL query to execute * @return the number of rows affected */ public SqlStatementResult createProcedure(String query) throws Exception { if (!prepared()) { return statementResult; } stmnt = conn.createStatement(); try { stmnt.clearWarnings(); stmnt.setEscapeProcessing(false); boolean isResultSet = stmnt.execute(query); if (!isResultSet) { int updateCount = stmnt.getUpdateCount(); if (updateCount == -1) updateCount = -10000; statementResult.setUpdateCount(updateCount); } else { // should never be a result set ResultSet rs = stmnt.getResultSet(); statementResult.setResultSet(rs); } useCount++; statementResult.setSqlWarning(stmnt.getWarnings()); } catch (SQLException e) { statementResult.setSqlException(e); } finally { if (stmnt != null) { stmnt.close(); } closeConnection(conn); } return statementResult; } /** <p>Executes the specified query and returns * the number of rows affected by this query. * <p>If an exception occurs, -1 is returned and * the relevant error message, if available, assigned * to this object for retrieval. * * @param the SQL query to execute * @return the number of rows affected */ public SqlStatementResult updateRecords(String query) throws SQLException { if (!prepared()) { return statementResult; } stmnt = conn.createStatement(); try { int result = stmnt.executeUpdate(query); statementResult.setUpdateCount(result); useCount++; } catch (SQLException e) { statementResult.setSqlException(e); } finally { if (stmnt != null) { stmnt.close(); } closeConnection(conn); } return statementResult; } /* public SqlStatementResult establishConnection(String query) { statementResult.reset(); String connectString = "CONNECT "; int index = query.indexOf("CONNECT ") + connectString.length(); String name = query.substring(index).trim(); DatabaseConnection dc = ConnectionProperties.getDatabaseConnection(name, true); if (dc == null) { statementResult.setMessage("The connection does not exist"); } return statementResult; } */ /** <p>Commits or rolls back the last executed * SQL query or queries. * * @param true to commit - false to roll back */ public SqlStatementResult commitLast(boolean commit) { try { statementResult.reset(); statementResult.setUpdateCount(0); if (commit) { conn.commit(); Log.info("Commit complete."); statementResult.setMessage("Commit complete."); closeMaxedConn(); } else { conn.rollback(); Log.info("Rollback complete."); statementResult.setMessage("Rollback complete."); closeMaxedConn(); } } catch (SQLException sqlExc) { statementResult.setSqlException(sqlExc); sqlExc.printStackTrace(); } return statementResult; } /** <p>Closes a connection which has reached its * maximum use count and retrieves a new one from * the <code>DBConnection</code> object. */ private void closeMaxedConn() throws SQLException { if (keepAlive && useCount > maxUseCount) { destroyConnection(); } } /** * Destroys the open connection. */ public void destroyConnection() throws SQLException { try { ConnectionManager.close(databaseConnection, conn); conn = null;// prepared();// useCount = 0; } catch (DataSourceException e) { handleDataSourceException(e); } } /** <p>Sets the connection's commit mode to the * specified value. * * @param true for auto-commit, false otherwise */ public void setCommitMode(boolean commitMode) { this.commitMode = commitMode; //Log.debug("commitMode: " + commitMode); try { if (keepAlive && (conn != null && !conn.isClosed())) { conn.setAutoCommit(commitMode); } } catch (SQLException sqlExc) { sqlExc.printStackTrace(); } } /** * Cancels the current SQL statement being executed. */ public void cancelCurrentStatement() { if (stmnt != null) { try { //Log.debug("cancelCurrentStatement"); stmnt.cancel(); stmnt.close(); stmnt = null; closeConnection(conn); statementResult.setMessage("Statement cancelled."); } catch (SQLException e) { e.printStackTrace(); } } } /** * Determines the type of query from the specified query. * * @param the SQL query to analyse * @result the type of SQL query */ public int getQueryType(String query) { int type = -1; query = query.toUpperCase(); if (query.indexOf("CREATE TABLE ") == 0) type = CREATE_TABLE; else if (query.indexOf("CREATE ") == 0 && (query.indexOf("PROCEDURE ") != -1 || query.indexOf("PACKAGE ") != -1)) type = CREATE_PROCEDURE; else if (query.indexOf("CREATE ") == 0 && query.indexOf("FUNCTION ") != -1) type = CREATE_FUNCTION; else if (query.indexOf("CONNECT ") == 0) type = CONNECT; else if (query.indexOf("INSERT ") == 0) type = INSERT; else if (query.indexOf("UPDATE ") == 0) type = UPDATE; else if (query.indexOf("DELETE ") == 0) type = DELETE; else if (query.indexOf("DROP TABLE ") == 0) type = DROP_TABLE; else if (query.indexOf("ALTER TABLE ") == 0) type = ALTER_TABLE; else if (query.indexOf("CREATE SEQUENCE ") == 0) type = CREATE_SEQUENCE; else if (query.indexOf("CREATE SYNONYM ") == 0) type = CREATE_SYNONYM; else if (query.indexOf("GRANT ") == 0) type = GRANT; else if (query.indexOf("EXECUTE ") == 0 || query.indexOf("CALL ") == 0) type = EXECUTE; else if (query.indexOf("COMMIT") == 0) type = COMMIT; else if (query.indexOf("ROLLBACK") == 0) type = ROLLBACK; else if(query.indexOf("SELECT ") == 0) type = SELECT; else if(query.indexOf("EXPLAIN ") == 0) type = EXPLAIN; else if(query.indexOf("DESC ") == 0 || query.indexOf("DESCRIBE ") == 0) type = DESCRIBE; else type = UNKNOWN; return type; } /** <p>Closes the specified database connection. * <p>If the specified connection is NULL, the open * connection held by this class is closed. * * @param the connection to close */ public void closeConnection(Connection c) throws SQLException { try { // if this not the connection assigned to this object if (c != null && c != conn) { c.close(); c = null; } else { // otherwise proceed to close closeConnection(); } /* if (!keepAlive) { c.close(); } else if (c == null) { if (conn != null) { conn.close(); } conn = null; } */ } catch (SQLException e) { e.printStackTrace(); } } /** * Close the database connection of this object. * If destroy is true, the connection will be * closed using connection.close(). Otherwise, * the value of keepAlive for this instance will * be respected. * * @param whether to call close() on the connection object */ public void closeConnection(boolean destroy) { if (destroy) { try { if (conn != null) { conn.close(); } conn = null; } catch (SQLException e) {} } } /** * Closes the database connection of this object. */ public void closeConnection() throws SQLException { // if set to keep the connection open // for this instance - return if (keepAlive) { return; } // otherwise close it closeConnection(true); } /** * Indicates a connection has been closed. * * @param the connection thats been closed */ public void disconnected(DatabaseConnection dc) { if (databaseConnection == dc) { closeConnection(true); databaseConnection = null; } } /** * Handles a DataSourceException by rethrowing as a * SQLException. */ private void handleDataSourceException(DataSourceException e) throws SQLException { if (e.getCause() instanceof SQLException) { throw (SQLException)e.getCause(); } else { throw new SQLException(e.getMessage()); } } /** <p>Releases database resources held by this class. */ public void releaseResources() { //Log.debug("releaseResources: keepAlive - " + keepAlive); try { if(stmnt != null) { stmnt.close(); } if(cstmnt != null) { cstmnt.close(); } stmnt = null; cstmnt = null; if (!keepAlive) { if (conn != null) { conn.close(); } conn = null; } /* if (keepAlive) { conn = dbConn.getConnection(); setCommitMode(commitMode); } */ } catch (SQLException exc) {} } public void releaseStatements() { try { if(stmnt != null) { stmnt.close(); } if(cstmnt != null) { cstmnt.close(); } stmnt = null; cstmnt = null; closeConnection(conn); } catch (Exception exc) {} } public DatabaseConnection getDatabaseConnection() { return databaseConnection; } public void setDatabaseConnection(DatabaseConnection _databaseConnection) { if (databaseConnection != _databaseConnection) { try { // close the current connection if (databaseConnection != null && conn != null) { ConnectionManager.close(databaseConnection, conn); conn = null; } // reassign the connection databaseConnection = _databaseConnection; prepared(); useCount = 0; } catch (DataSourceException e) {} catch (SQLException e) {} } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -