예제 #1
0
  public static void test01() throws Exception {
    Page p = new Page();
    p.setTmsp("2011-03-04 10:30:00");
    p.setUrl("http://www.google.com");
    p.setBody("my body");
    p.insert();

    Page e = new Page("2011-03-04 10:31:00", "http://www.usp.br", "o corpo");
    e.insert();
    e.setBody("usp usp usp");
    e.update();

    Database db = new Database();
    db.connect();
    ResultSet rs = Page.findAll(db);

    /*
    while (rs.next()) {
    	System.out.println(rs.getString("tmsp")
    		+", "+ rs.getString("url")
    		+", "+ rs.getString("body"));
    }*/

    Page p0 = null;
    while ((p0 = Page.next(rs)) != null) {
      System.out.println(p0.getTmsp());
      System.out.println(p0.getUrl());
      System.out.println(p0.getBody());
    }

    db.close();

    p.remove();
    e.remove();
  }
  /** Method declaration */
  public void run() {

    Channel c = init();

    if (c != null) {
      try {
        while (true) {
          String sql = mInput.readUTF();

          mServer.trace(mThread + ":" + sql);

          if (sql == null) {
            break;
          }

          write(mDatabase.execute(sql, c).getBytes());
        }
      } catch (Exception e) {
      }
    }

    try {
      mSocket.close();
    } catch (IOException e) {
    }

    if (mDatabase.isShutdown()) {
      System.out.println("The database is shutdown");
      System.exit(0);
    }
  }
예제 #3
0
  // Chamando Robot
  public static void robot() throws Exception {
    Database db = new Database();
    db.connect();
    ResultSet rs = Page.findAll(db);

    Page p = null;
    while ((p = Page.next(rs)) != null) {
      String body = Robot.get(p.getUrl());

      // procurar por urls dentro do body
      // buscar por essas paginas

      // String expr = "href=\"([^\"]*)";
      String ereg = "href=\"https{0,1}:\\/\\/([^\"]*)\"";
      Pattern pt = Pattern.compile(ereg);
      Matcher m = pt.matcher(body);

      while (m.find()) {
        System.out.println(m.group());
        String[] _url = m.group().split("\"");
        Page.newUrl(_url[1]);
      }

      p.setBody(body);
      p.update();
    }

    db.close();
  }
예제 #4
0
 /**
  * * sendResponse checks for the mode of the server and sends a Response object to the client with
  * the appropriate string stored and the client's Cookie.
  *
  * @param clientCookie - Cookie object received by client
  * @param responseToClient - Response object that will be sent to client
  * @param outputObject - Used to write serialized version of object back to client
  * @throws IOException - Thrown if outputObject is interrupted while processing or fails to
  *     process
  */
 static void sendResponse(
     Cookie clientCookie, Response responseToClient, ObjectOutputStream outputObject)
     throws IOException {
   if (mode.getMode() == ServerMode.JOKE) { // If the mode of the server is set to JOKE, send joke
     responseToClient.addResponse(
         joke.say(
             clientCookie
                 .getJokeKey())); // gets joke from Database and stores string in responseToClient
     clientCookie.nextJoke(); // clientCookie increments the index of the joke to be accessed later
     responseToClient.setCookie(clientCookie); // stores clientCookie in responseToClient
     System.out.println("Sending joke response..."); // notify server joke is being sent
     outputObject.writeObject(
         responseToClient); // send a serialized version of Response object to client
   } else if (mode.getMode()
       == ServerMode.PROVERB) { // If the mode of the server is set to PROVERB, send proverb
     responseToClient.addResponse(proverb.say(clientCookie.getProverbKey()));
     clientCookie.nextProverb();
     responseToClient.setCookie(clientCookie);
     System.out.println("Sending proverb response...");
     outputObject.writeObject(
         responseToClient); // send Response object with proverb and client's Cookie to the client
   } else if (mode.getMode()
       == ServerMode
           .MAINTENANCE) { // If the mode of the server is set to MAINTENANCE, notify clients
                           // server is down for maintenance
     responseToClient.addResponse("Joke server temporarily down for maintenance.\n");
     responseToClient.setCookie(clientCookie);
     System.out.println("Sending maintenance response...");
     outputObject.writeObject(
         responseToClient); // send Response object with maintenance message and client's Cookie to
                            // the client
   }
 }
예제 #5
0
  public void true_insert() throws Exception {
    // synchronized(variavel) {

    Database db = new Database();
    db.connect();
    // BEGIN WORK -> stored procedure
    db.update(
        "insert into tbNews (tmsp, headline, body, email) values ('"
            + this.tmsp
            + "', '"
            + this.headline
            + "', '"
            + this.body
            + "', '"
            + this.email
            + "')");
    ResultSet rs = db.query("select max(id) as id from tbNews");
    if (rs.next()) {
      this.id = rs.getInt("id");
    }
    // COMMIT

    db.close();

    // }
  }
예제 #6
0
  public void remove() throws Exception {
    Database db = new Database();
    db.connect();

    db.update("delete from page where url = '" + url + "'");

    db.close();
  }
예제 #7
0
 public News(int id) throws Exception {
   Database db = new Database();
   db.connect();
   News news = News.findById(db, id);
   db.close();
   this.id = news.id;
   this.tmsp = news.tmsp;
   this.headline = news.headline;
   this.body = news.body;
   this.email = news.email;
 }
예제 #8
0
  public void update() throws Exception {
    Database db = new Database();
    db.connect();

    db.update(
        "update page set tmsp = '" + tmsp + "', body = '" + body + "' where url = '" + url + "'");
    // URLEncoder.encode(url, "UTF-8")+"'");
    // URLEncoder.encode(url, "ISO-8859-1")+"'");

    db.close();
  }
예제 #9
0
  // sqllers
  public void insert() throws Exception {
    Database db = new Database();
    db.connect();

    db.update(
        "insert into page (tmsp, url, body) values ('"
            + tmsp
            + "', '"
            + url
            + "', '"
            + body
            + "')");

    db.close();
  }
예제 #10
0
 public void update() throws Exception {
   Database db = new Database();
   db.connect();
   db.update(
       "update tbNews set tmsp = '"
           + this.tmsp
           + "', headline = '"
           + this.headline
           + "', body = '"
           + this.body
           + "', email = '"
           + this.email
           + "' where id = "
           + this.id);
   db.close();
 }
예제 #11
0
 public static ResultSet findByQuery(Database db, String query) throws Exception {
   return db.query(
       "select tmsp, url, body from page where url like '%"
           + query
           + "%' or body like '%"
           + query
           + "%'");
 }
  /**
   * Method declaration
   *
   * @return
   */
  private Channel init() {

    try {
      mSocket.setTcpNoDelay(true);

      mInput = new DataInputStream(new BufferedInputStream(mSocket.getInputStream()));
      mOutput = new DataOutputStream(new BufferedOutputStream(mSocket.getOutputStream()));

      String user = mInput.readUTF();
      String password = mInput.readUTF();
      Channel c;

      try {
        mServer.trace(mThread + ":trying to connect user " + user);

        return mDatabase.connect(user, password);
      } catch (SQLException e) {
        write(new Result(e.getMessage()).getBytes());
      }
    } catch (Exception e) {
    }

    return null;
  }
예제 #13
0
 // finders
 public static ResultSet findAll(Database db) throws Exception {
   return db.query("select id, tmsp, headline, body, email from tbNews");
 }
예제 #14
0
 public static ResultSet findByUrl(Database db, String url) throws Exception {
   return db.query("select tmsp, url, body from page where url = '" + url + "'");
 }
예제 #15
0
 // finders
 public static ResultSet findAll(Database db) throws Exception {
   return db.query("select tmsp, url, body from page");
 }
예제 #16
0
 public static News findById(Database db, int id) throws Exception {
   return News.next(
       db.query("select id, tmsp, headline, body, email from tbNews where id = " + id));
 }
예제 #17
0
 public static ResultSet findByEmail(Database db, String email) throws Exception {
   return db.query(
       "select id, tmsp, headline, body, email from tbNews where email = '" + email + "'");
 }
예제 #18
0
 public static ResultSet findLast(Database db, int n) throws Exception {
   return db.query(
       "select id, tmsp, headline, body, email from tbNews order by tmsp desc limit " + n);
 }
예제 #19
0
 public void remove() throws Exception {
   Database db = new Database();
   db.connect();
   db.update("delete from tbNews where id = " + this.id);
   db.close();
 }