public static void main(String args[]) {
    try {
      String urlBD = "jdbc:odbc:FOSA";
      String usuarioBD = "SYSDBA";
      String passwordBD = "masterkey";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection conexion = DriverManager.getConnection(urlBD, usuarioBD, passwordBD);
      Statement select = conexion.createStatement();
      ResultSet resultadoSelect = select.executeQuery("SELECT MAX(NUM_REG) AS numreg FROM FACT01");
      System.out.println("COMANDO EXITOSO");
      System.out.println("numreg");
      System.out.println("------");
      int col = resultadoSelect.findColumn("numreg");
      for (boolean seguir = resultadoSelect.next(); seguir; seguir = resultadoSelect.next())
        System.out.println(resultadoSelect.getInt(col));

      resultadoSelect.close();
      select.close();
      conexion.close();
    } catch (SQLException ex) {
      System.out.println("Error: SQLException");
      while (ex != null) {
        System.out.println(
            (new StringBuilder()).append("SQLState: ").append(ex.getSQLState()).toString());
        System.out.println(
            (new StringBuilder()).append("Mensaje: ").append(ex.getMessage()).toString());
        System.out.println(
            (new StringBuilder()).append("Vendedor: ").append(ex.getErrorCode()).toString());
        ex = ex.getNextException();
        System.out.println("");
      }
    } catch (Exception ex) {
      System.out.println("Se produjo un error inesperado");
    }
  }
예제 #2
0
  public static String getIPContainer(Connection con, String mail) {
    String recherche = "";
    try {
      Statement smt = con.createStatement();
      ResultSet resultset = smt.executeQuery("SELECT * FROM container WHERE email='" + mail + "' ");
      int nbr = 0;
      while (resultset.next()) {
        recherche = resultset.getString(resultset.findColumn("ip"));
      }

    } catch (SQLException ex) {
      Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return recherche;
  }
예제 #3
0
  public static String getBDD(Connection con, String mail) {
    String recherche = "";
    try {
      Statement smt = con.createStatement();
      ResultSet resultset = smt.executeQuery("SELECT * FROM members WHERE email='" + mail + "' ");
      int nbr = 0;
      recherche =
          "<div class=\"form-group\" align=\"center\">\n"
              + "                            <div><SELECT name=\"Bdd\" id=\"required\" align=\"center\">";
      recherche += "<option value=\"123456test**123456\" >Your databases</option>\n";
      while (resultset.next()) {
        String nom = resultset.getString(resultset.findColumn("bdd"));
        String s = "_";
        String s2 = "-";
        String s1 = "" + nom;
        if (s.equals(s1)) {
          // nothing rest = ".";
        } else if (s2.equals(s1)) {
          // nothing  rest = ".";
        } else {
          recherche += "<option VALUE=\"" + nom + "\">" + nom + "</option>\n";
        }
        nbr++;
      }
      if (nbr > 0) {
        recherche +=
            "</SELECT>\n"
                + "                            </div>\n"
                + "                        </div>";
      }

    } catch (SQLException ex) {
      Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return recherche;
  }
예제 #4
0
  private boolean executeQuery() {
    boolean returnVaue = true;
    int beginRow = 0;
    boolean moveIF = true;
    String lineOne = "";
    ResultSet rst = null;
    ResultSetMetaData rstMetaData = null;
    int columnCount = 0;
    String temp = "";
    rowNum = 0;
    if (querySQL == "") {
      writeLog("??????????");
      quickField = "";
      quickValue = "";
      quickRow = -1;
      return false;
    }
    quickRow = -1;
    try {
      if (isProcedure) {
        rst = cstmt.executeQuery();
      } else {
        if (stmt == null) init2();
        rst = stmt.executeQuery(querySQL);
      }
    } catch (Exception e) {
      writeLog(
          (new StringBuilder("??��???????"))
              .append(e.toString())
              .append(",sql=")
              .append(querySQL)
              .toString());
      try {
        if (isProcedure && cstmt != null) cstmt.close();
        else if (stmt != null) stmt.close();
      } catch (SQLException sqlexception) {
      }
      returnVaue = false;
      return returnVaue;
    }
    try {
      rst.last();
      totalRowCount = rst.getRow();
      rst.beforeFirst();
    } catch (Exception ex) {
      totalRowCount = 0;
      writeLog((new StringBuilder("??��???????")).append(ex.toString()).toString());
      return returnVaue;
    }
    pageCount = BigInteger.valueOf(totalRowCount).divide(BigInteger.valueOf(perPage)).intValue();
    if (BigInteger.valueOf(totalRowCount).mod(BigInteger.valueOf(perPage)).intValue() > 0)
      pageCount++;
    if (pageCount == 0) nextPage = 0;
    else if (nextPage > pageCount) nextPage = 0;
    beginRow = nextPage * perPage;
    beginRow++;
    curPage = nextPage;
    if (pageCount <= 1) {
      ifNext = false;
      nextPage = 0;
    } else if (nextPage > pageCount) {
      ifNext = false;
      nextPage = 0;
    } else {
      ifNext = true;
    }
    if (nextPage < 0) nextPage = 0;
    if (quickField != null
        && quickField.compareTo("") != 0
        && pageCount != 0
        && quickValue != null
        && quickValue.compareTo("") != 0)
      try {
        int colIndex = 0;
        String quickFieldName = "";
        String quickFieldType = "";
        String quickFieldTmp[] = (String[]) null;
        boolean haveFound = false;
        int findRow = 0;
        quickFieldTmp = StringUtil.split(quickField, ",");
        if (quickFieldTmp == null)
          throw new Exception((new StringBuilder("??????��??")).append(quickField).toString());
        quickFieldName = quickFieldTmp[0];
        quickFieldType = quickFieldTmp[1];
        colIndex = rst.findColumn(quickFieldName);
        rst.beforeFirst();
        while (!haveFound && rst.next()) {
          String tmpValue = "";
          if (rst.getObject(colIndex) != null) {
            tmpValue = rst.getObject(colIndex).toString();
            if (quickFieldType.compareToIgnoreCase("date") == 0) {
              int indexPos = -1;
              tmpValue = StringUtil.mk_date(tmpValue, 2);
              indexPos = tmpValue.indexOf(quickValue);
              if (indexPos == -1) {
                haveFound = false;
              } else {
                haveFound = true;
                findRow = rst.getRow();
              }
            } else if (quickFieldType.compareToIgnoreCase("long") == 0) {
              int indexPos = -1;
              indexPos = tmpValue.compareTo(quickValue);
              if (indexPos == 0) {
                haveFound = true;
                findRow = rst.getRow();
              } else {
                haveFound = false;
              }
            } else if (quickFieldType.compareToIgnoreCase("char") == 0) {
              int indexPos = -1;
              indexPos = tmpValue.indexOf(quickValue);
              if (indexPos == -1) {
                haveFound = false;
              } else {
                haveFound = true;
                findRow = rst.getRow();
              }
            }
          }
        }
        if (findRow > 0) {
          int pageTmp = 0;
          int modTmp = 0;
          pageTmp = BigInteger.valueOf(findRow).divide(BigInteger.valueOf(perPage)).intValue();
          modTmp = BigInteger.valueOf(findRow).mod(BigInteger.valueOf(perPage)).intValue();
          if (modTmp == 0) {
            nextPage = pageTmp - 1;
            quickRow = perPage;
          } else {
            nextPage = pageTmp;
            quickRow = modTmp;
          }
          beginRow = nextPage * perPage;
          beginRow++;
          curPage = nextPage;
          if (nextPage >= pageCount - 1) {
            ifNext = false;
            nextPage = 0;
          } else {
            ifNext = true;
            nextPage = nextPage + 1;
          }
        }
      } catch (Exception exx) {
        writeLog(exx.toString());
        quickRow = -1;
      }
    quickField = "";
    quickValue = "";
    try {
      rstMetaData = rst.getMetaData();
    } catch (Exception e) {
      writeLog((new StringBuilder("???????")).append(e.toString()).toString());
      returnVaue = false;
    }
    try {
      columnCount = rstMetaData.getColumnCount();
    } catch (Exception e) {
      writeLog((new StringBuilder("??��??")).append(e.toString()).toString());
      returnVaue = false;
    }
    Object object = null;
    for (int i = 1; i <= columnCount; i++)
      try {
        fieldVector.add(rstMetaData.getColumnName(i));
      } catch (Exception e) {
        writeLog(
            (new StringBuilder("Exception while getColumnName?G")).append(e.toString()).toString());
      }

    try {
      if (pageCount > 0) {
        try {
          rst.absolute(beginRow);
          moveIF = true;
        } catch (Exception exx) {
          moveIF = false;
        }
        if (moveIF) {
          for (int i = 1; i <= columnCount; i++) {
            try {
              object = rst.getObject(i);
              if (object == null) temp = "";
              else temp = object.toString();
            } catch (Exception e) {
              temp = "";
            }
            if (temp == null) temp = "";
            lineOne = (new StringBuilder(String.valueOf(lineOne))).append(temp).toString();
            valueVector.add(temp);
          }

          if (lineOne.equals("")) {
            valueVector = new ArrayList();
            rowNum = 0;
          } else {
            rowNum = 1;
          }
          for (int i = 1; i < perPage; i++)
            if (rst.next()) {
              rowNum++;
              for (int k = 1; k <= columnCount; k++) {
                try {
                  if (rst.getObject(k) == null) temp = "";
                  else temp = rst.getObject(k).toString();
                } catch (Exception e) {
                  temp = "";
                }
                if (temp == null) temp = "";
                valueVector.add(temp);
              }
            }
        }
      }
    } catch (Exception e) {
      writeLog((new StringBuilder("??????")).append(e.toString()).toString());
      returnVaue = false;
    }
    try {
      rst.close();
    } catch (Exception exception) {
    }
    try {
      if (stmt != null) stmt.close();
    } catch (Exception exception1) {
    }
    if (!returnVaue) resetBean();
    colCount = columnCount;
    close();
    return returnVaue;
  }
예제 #5
0
  public static String getProfil(Connection con, String mail, int port) {
    String recherche = "";
    try {
      Statement smt = con.createStatement();
      ResultSet resultset = smt.executeQuery("SELECT * FROM members WHERE email='" + mail + "' ");
      int nbr = 0;
      while (resultset.next()) {
        String nom = resultset.getString(resultset.findColumn("bdd"));
        String s = "_";
        String s2 = "-";
        String s1 = "" + nom;
        String rest = "";
        if (s.equals(s1)) {
          // nothing rest = ".";
        } else if (s2.equals(s1)) {
          // nothing  rest = ".";
        } else if (port == 0) {
          // nothing  rest = ".";
        } else {
          rest = nom;
          MongoAPI m = new MongoAPI();
          m.initClient("root", "1*smart-db*2", port, "admin");
          float x = m.getDBSize(rest);
          if (x <= 1000000) // ko
          {
            float y = x / 1000;
            recherche +=
                "<tr>\n"
                    + "                                <th>"
                    + rest
                    + "</th>\n"
                    + "                                <th>"
                    + y
                    + " ko</th> \n"
                    + "                                <th>"
                    + port
                    + "</th> \n"
                    + "                            </tr>";
          } else if (x >= 1000000000) // Go
          {
            float y = x / 1000000000;
            recherche +=
                "<tr>\n"
                    + "                                <th>"
                    + rest
                    + "</th>\n"
                    + "                                <th>"
                    + y
                    + " Go</th> \n"
                    + "                                <th>"
                    + port
                    + "</th> \n"
                    + "                            </tr>";
          } else // Mo
          {
            float y = x / 1000000;
            recherche +=
                "<tr>\n"
                    + "                                <th>"
                    + rest
                    + "</th>\n"
                    + "                                <th>"
                    + y
                    + " Mo</th> \n"
                    + "                                <th>"
                    + port
                    + "</th> \n"
                    + "                            </tr>";
          }
        }

        nbr++;
      }
      if (nbr > 0) {
        recherche +=
            "</SELECT>\n"
                + "                            </div>\n"
                + "                        </div>";
      }

    } catch (SQLException ex) {
      Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return recherche;
  }
  protected void setUp() throws SQLException {
    conControl = MockControl.createControl(Connection.class);
    con = (Connection) conControl.getMock();
    con.isClosed();
    conControl.setDefaultReturnValue(false);

    rsmdControl = MockControl.createControl(ResultSetMetaData.class);
    rsmd = (ResultSetMetaData) rsmdControl.getMock();
    rsmd.getColumnCount();
    rsmdControl.setReturnValue(4, 1);
    rsmd.getColumnLabel(1);
    rsmdControl.setReturnValue("name", 1);
    rsmd.getColumnType(1);
    rsmdControl.setReturnValue(Types.VARCHAR, 1);
    rsmd.getColumnLabel(2);
    rsmdControl.setReturnValue("age", 1);
    rsmd.getColumnType(2);
    rsmdControl.setReturnValue(Types.NUMERIC, 1);
    rsmd.getColumnLabel(3);
    rsmdControl.setReturnValue("birth_date", 1);
    rsmd.getColumnType(3);
    rsmdControl.setReturnValue(Types.TIMESTAMP, 1);
    rsmd.getColumnLabel(4);
    rsmdControl.setReturnValue("balance", 1);
    rsmd.getColumnType(4);
    rsmdControl.setReturnValue(Types.DECIMAL, 1);
    rsmdControl.replay();

    rsControl = MockControl.createControl(ResultSet.class);
    rs = (ResultSet) rsControl.getMock();
    rs.getMetaData();
    rsControl.setReturnValue(rsmd, 1);
    rs.next();
    rsControl.setReturnValue(true, 1);
    rs.getString("name");
    rsControl.setReturnValue("Bubba", 1);
    rs.getLong("age");
    rsControl.setReturnValue(22, 1);
    rs.findColumn("birth_date");
    rsControl.setReturnValue(3, 1);
    rs.getObject(3);
    rsControl.setReturnValue(new java.sql.Timestamp(1221222L), 1);
    rs.getBigDecimal("balance");
    rsControl.setReturnValue(new BigDecimal("1234.56"), 1);
    rs.next();
    rsControl.setReturnValue(false, 1);
    rs.close();
    rsControl.setVoidCallable(1);
    rsControl.replay();

    stmtControl = MockControl.createControl(Statement.class);
    stmt = (Statement) stmtControl.getMock();

    con.createStatement();
    conControl.setReturnValue(stmt, 1);
    stmt.executeQuery("select name, age, birth_date, balance from people");
    stmtControl.setReturnValue(rs, 1);
    stmt.getWarnings();
    stmtControl.setReturnValue(null, 1);
    stmt.close();
    stmtControl.setVoidCallable(1);

    conControl.replay();
    stmtControl.replay();

    jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false));
    jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    jdbcTemplate.afterPropertiesSet();
  }