コード例 #1
0
ファイル: DAO_Seccion.java プロジェクト: GeroLopez/SGE
 /**
  * Consulta la base de datos para obtener una lista con las secciones.
  *
  * @return Una lista con las secciones en la base de datos.
  */
 public LinkedList<Seccion> consultarSecciones() {
   LinkedList<Seccion> secciones = new LinkedList<Seccion>();
   String sql = "SELECT id, nombre FROM sections order by id";
   try {
     conexion.consultar(sql);
     while (conexion.getRes().next()) {
       secciones.add(
           new Seccion(
               Integer.parseInt(conexion.getRes().getString(1).toString()),
               conexion.getRes().getString(2).toString()));
     }
   } catch (SQLException | NumberFormatException e) {
     System.out.println(e.toString());
   }
   return secciones;
 }
コード例 #2
0
ファイル: User.java プロジェクト: yolanda6/moblieApp
  @SuppressWarnings({"unchecked", "unused"})
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String s = request.getParameter("data");
    if (s == null || s.equals("")) {
      return;
    }
    Connection conn = null;

    try {

      Class.forName("com.mysql.jdbc.Driver");
      if (conn == null) conn = DriverManager.getConnection(Global.jdbcUrl);

    } catch (ClassNotFoundException | SQLException e) {
      throw new RuntimeException("Cannot find the driver in the classpath!", e);
    }

    JSONObject json = null;
    String op = null;
    String uid = null;
    try {
      json = new JSONObject(s);
      op = json.getString("OP");
      uid = json.getString("userid");
    } catch (JSONException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    if (op.equals("insert")) {
      Statement writeStatement = null;

      try {
        writeStatement = conn.createStatement();
        writeStatement.execute(
            "INSERT INTO User (userid,isblock,isactive) VALUES('" + uid + "','0','1');");
        writeStatement.close();

      } catch (SQLException e) {
        e.printStackTrace();
      }

    } else if (op.equals("select")) {
      Statement stmt;
      String sendingText;
      JSONObject rt = new JSONObject();
      try {
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM User WHERE userid = '" + uid + "';");
        while (rs.next()) {
          rt.put("isactive", (Integer.parseInt(rs.getString("isactive")) == 1) ? true : false);
          rt.put("isblock", (Integer.parseInt(rs.getString("isblock")) == 1) ? true : false);
        }
        rs.close();
        stmt.close();

      } catch (SQLException | NumberFormatException | JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
      writer.write(rt.toString());
      writer.flush();

    } else if (op.equals("block")) {

      Statement writeStatement = null;

      try {
        writeStatement = conn.createStatement();
        writeStatement.execute("UPDATE User SET isblock = 1 WHERE userid='" + uid + "';");
        writeStatement.close();

      } catch (SQLException e) {
        e.printStackTrace();
      }

    } else if (op.equals("unblock")) {

      Statement writeStatement = null;

      try {
        writeStatement = conn.createStatement();
        writeStatement.execute("UPDATE User SET isblock = 0 WHERE userid='" + uid + "';");
        writeStatement.close();

      } catch (SQLException e) {
        e.printStackTrace();
      }

    } else {
      System.err.println("Operation does not match anything!");
    }

    try {
      conn.close();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return;
  }