Foros del Web » Programación para mayores de 30 ;) » Java »

Ayuda para encontrar conceptos en estos ejemplos parte 2

Estas en el tema de Ayuda para encontrar conceptos en estos ejemplos parte 2 en el foro de Java en Foros del Web. Y en la segunda clase digamos que viene un ABC (Altas, Bajas, Cambios), en la Base de datos Utilizando la clase anterior. Ademas de los ...
  #1 (permalink)  
Antiguo 08/07/2005, 10:27
Avatar de ko^ke  
Fecha de Ingreso: septiembre-2001
Ubicación: México D.F.
Mensajes: 364
Antigüedad: 22 años, 7 meses
Puntos: 1
Ayuda para encontrar conceptos en estos ejemplos parte 2

Y en la segunda clase digamos que viene un ABC (Altas, Bajas, Cambios), en la Base de datos Utilizando la clase anterior.

Ademas de los conceptos que manejan estas clases que me gustaria entender cualquier comentario relacionado es bienvenido.

Código PHP:
package samples.restaurant;

import java.util.ArrayList;
import java.sql.*;

public class 
RestaurantService {

    public 
ArrayList getRestaurantList() {

        
ArrayList list = new ArrayList();

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
Statement s c.createStatement();
            
ResultSet rs s.executeQuery("SELECT * FROM restaurant ORDER BY name");
            while (
rs.next()) {
                list.
add(new Restaurant(
                        
rs.getInt("restaurant_id"),
                        
rs.getString("name"),
                        
rs.getString("address"),
                        
rs.getString("city"),
                        
rs.getString("zip"),
                                }
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        return list;

    }

    public 
ArrayList getRestaurantListByArea(double x1double x2double y1double y2) {

        
ArrayList list = new ArrayList();

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
Statement s c.createStatement();
            
ResultSet rs s.executeQuery("SELECT * FROM restaurant WHERE map_x>="+x1+" AND map_x<="+x2+" AND map_y>="+y1+" AND map_y<="+y2+" ORDER BY name");
            while (
rs.next()) {
                list.
add(new Restaurant(
                        
rs.getInt("restaurant_id"),
                        
rs.getString("name"),
                        
rs.getString("address"),
                        
rs.getString("city"),
                        
rs.getString("zip"),
                            }
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        return list;

    }

    public 
ArrayList getRestaurantListByCategories(double x1double x2double y1double y2ArrayList selectedCategories) {

        
int size=selectedCategories.size();
        
StringBuffer where=new StringBuffer();

        if (
size>0) {
            
where.append("AND (");
            for (
int i=0i<sizei++) {
                if (
i>0where.append(" OR ");
                
where.append("category_id="+((Double)selectedCategories.get(i)).intValue());
            }
            
where.append(")");
        }

        
ArrayList list = new ArrayList();

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
Statement s c.createStatement();
            
String sql="SELECT r.restaurant_id, name, address, city, zip, phone, link, image, description, review FROM restaurant as r, restaurant_restaurant_category as rrc WHERE r.restaurant_id=rrc.restaurant_id AND map_x>="+x1+" AND map_x<="+x2+" AND map_y>="+y1+" AND map_y<="+y2+"  "+where+" GROUP BY r.restaurant_id, name, address, city, zip, phone, link, image, description, review ORDER BY name";
            
ResultSet rs s.executeQuery(sql);
            while (
rs.next()) {
                list.
add(new Restaurant(
                        
rs.getInt("restaurant_id"),
                        
rs.getString("name"),
                        
rs.getString("address"),
                        
rs.getString("city"),
                        
rs.getString("zip"),
                     }
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        return list;
    }

    public 
Restaurant getRestaurant(int restaurantId) {

        
Restaurant restaurant=null;
        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
Statement s c.createStatement();
            
ResultSet rs s.executeQuery("SELECT * FROM restaurant WHERE restaurant_id="+restaurantId);
            if (
rs.next()) {
                
restaurant=new Restaurant(
                        
rs.getInt("restaurant_id"),
                        
rs.getString("name"),
                        
rs.getString("address"),
                        
rs.getString("city"),
                             }
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        return 
restaurant;

    }

    public 
void updateRestaurantBundle(Restaurant restaurantArrayList categoriesArrayList links) {

        try {
            
updateRestaurant(restaurant);
            
updateCategories(restaurant.getRestaurantId(), categories);
            
updateLinks(restaurant.getRestaurantId(), links);
        } catch (
Exception e) {
            
e.printStackTrace();
        }

    }

    public 
void updateCategories(int restaurantIdArrayList categories) {

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
Statement s c.createStatement();
            
s.execute("DELETE FROM restaurant_restaurant_category WHERE restaurant_id="+restaurantId);
            
PreparedStatement ps c.prepareStatement("INSERT INTO restaurant_restaurant_category (restaurant_id, category_id) VALUES (?,?)");
            
int size=categories.size();
            for (
int i=0i<sizei++) {
                
ps.setInt(1restaurantId);
                
ps.setInt(2, ((Double) categories.get(i)).intValue());
                
ps.execute();
            }
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }

    }

       public 
int addRestaurant(Restaurant restaurant) {

        
int pk=0;
        
Connection c=null;
        try {
            
c=ConnectionManager.getConnection();
            
PreparedStatement ps c.prepareStatement("INSERT INTO restaurant (name, address, city, zip, phone, link, image, description, review, map_x, map_y) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
ps.setString(1restaurant.getName());
            
ps.setString(2restaurant.getAddress());
            
Statement s=c.createStatement();
            
ResultSet rs=s.executeQuery("SELECT last_insert_id() FROM restaurant");
            
rs.next();
            
pk=rs.getInt(1);
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        return 
pk;

    }

    public 
int addRestaurantBundle(Restaurant restaurantArrayList categoriesArrayList links) {

        
// TO DO: Implement appropriate COMMIT logic

        
int pk=addRestaurant(restaurant);
        
this.updateCategories(pkcategories);
        
this.updateLinks(pklinks);

        return 
pk;

    }

    public 
void updateRestaurant(Restaurant restaurant) {

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
PreparedStatement ps c.prepareStatement("UPDATE restaurant SET name=?, address=?, city=?, zip=?, phone=?, link=?, image=?, description=?, review=?, map_x=?, map_y=? WHERE restaurant_id=?");
            
ps.setString(1restaurant.getName());
            
ps.setString(2restaurant.getAddress());
                    
ps.execute();
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }

    }

    public 
void deleteRestaurant(int restaurantId) {

        
Connection c=null;

        try {
            
c=ConnectionManager.getConnection();
            
PreparedStatement ps c.prepareStatement("DELETE FROM restaurant WHERE restaurant_id=?");
            
ps.setInt(1restaurantId);
            
ps.execute();
        } catch (
Exception e) {
            
e.printStackTrace();
        } 
finally {
            try {
                
c.close();
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }

    }

      }

    
    }



Un saludo,

Ko^Ke
__________________
Aquí va la firma...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:55.