Exemplo n.º 1
0
 protected boolean insertComment() throws SQLException {
   try {
     String dburl = getServletContext().getInitParameter("DBUrl");
     String dbuser = getServletContext().getInitParameter("DBUser");
     String dbpasswd = getServletContext().getInitParameter("DBPasswd");
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(dburl, dbuser, dbpasswd);
     String sqlqry = "INSERT INTO feedback (name, email, feedback) VALUES (?,?,?)";
     pstmt = conn.prepareStatement(sqlqry);
     pstmt.setString(1, name);
     pstmt.setString(2, email);
     pstmt.setString(3, comment);
     if (pstmt.executeUpdate() > 0) return true;
   } catch (ClassNotFoundException e) {
     // TODO Auto-generated catch block
     logger.error(e);
     e.printStackTrace();
   } catch (SQLException e) {
     // TODO Auto-generated catch block
     logger.error(e);
     e.printStackTrace();
   } finally {
     conn.close();
     pstmt.close();
   }
   return false;
 }
Exemplo n.º 2
0
  protected void processData(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException {
    try {
      String dispatchUrl = "/feedback.jsp";
      name = request.getParameter("txtName");
      email = request.getParameter("txtEmail");
      comment = request.getParameter("txtComment");

      if (!insertComment()) {
        logger.error("Error. Could not insert comment");
        request.setAttribute("status", "fail");
      } else {
        request.setAttribute("status", "success");
      }
      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(dispatchUrl);
      dispatcher.include(request, response);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      logger.error(e);
      e.printStackTrace();
    } finally {
      conn.close();
      pstmt.close();
    }
  }
 /**
  * Checks if a lock is left behind in the DB.
  *
  * @param con
  * @param conID
  * @return true if a lock is found.
  */
 public static boolean checkLocks(Connection connection, long conID) {
   try {
     Statement stmt = connection.createStatement();
     ResultSet res = stmt.executeQuery("exec sa_locks " + conID);
     while (res.next()) {
       return true;
     }
   } catch (SQLException ex) {
     logger.error("Unable to retrieve connection ID", ex);
   }
   return false;
 }
  /**
   * Gets the ConnectionID attribute of the SybaseConnector class
   *
   * @param connection
   * @return The ConnectionID value
   */
  public static long getConnectionID(Connection connection) {
    try {
      Statement stmt = connection.createStatement();
      ResultSet res = stmt.executeQuery("select connection_property('Number')");
      res.next();
      return res.getLong(1);
    } catch (SQLException ex) {
      logger.error("SQL exception retrieving connection ID:" + ex.getMessage());
    }

    return InvalidConnectionId;
  }
  /**
   * @return Connection
   * @roseuid 3F3A5FFD0338
   */
  public Connection getConnection() throws EmanagerDatabaseException {
    long connectionId;
    Connection connection;
    PooledConnection pooledConnection;

    connection = null;
    pooledConnection = null;
    connectionId = InvalidConnectionId;

    try {
      synchronized (connectionPool) {
        if (!connectionPool.isEmpty()) {
          try {
            boolean connectionClosed;

            connectionClosed = false;

            pooledConnection = (PooledConnection) connectionPool.remove(0);
            connection = pooledConnection.getConnection();
            connection.clearWarnings();
            connectionId = getConnectionID(connection);
            connectionClosed = connection.isClosed();
            if (connectionId == InvalidConnectionId || connectionClosed == true) {
              logger.debug("Pooled connection closed.");
              connection = null;
            }
          } catch (SQLException sqe) {
            logger.debug("Pooled connection closed.");
            connection = null;
          }
        }
      }

      if (connection == null) {
        logger.debug("Getting a new connection.");
        pooledConnection = poolDataSource.getPooledConnection();
        pooledConnection.addConnectionEventListener(this);
        connection = pooledConnection.getConnection();
        connection.clearWarnings();
        connectionId = getConnectionID(connection);
      }
    } catch (SQLException sqe) {
      String logString;
      EmanagerDatabaseException ede;

      logString =
          EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription()
              + sqe.getMessage();

      logger.error(logString);
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection, logString);
      throw ede;
    }

    if (connectionId == InvalidConnectionId) {
      EmanagerDatabaseException ede;
      ede =
          new EmanagerDatabaseException(
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection,
              EmanagerDatabaseStatusCode.UnableToGetPooledConnection.getStatusCodeDescription());
      throw ede;
    }

    logger.debug(
        "\n*****************************"
            + "\nPooled Connection Init"
            + "\nCon ID:"
            + connectionId
            + "\nCon Object:"
            + pooledConnection
            + "\nPool Object:"
            + connection
            + "\n*****************************");

    return connection;
  }