如何从数据库中获取值以在我的JSF bean构造函数中使用?

时间:2014-06-12 17:43:12

标签: jsf jpa

我正在尝试开发一个谷歌地图应用程序,从数据库中获取经度和纬度。我遵循了一个类似的教程,但它使用了静态坐标。

如何使用JPA从mysql数据库中读取值并解析为构造函数?

例如,在下面的代码中coord3是常量,但应该来自数据库。 如果看起来很明显,请原谅我。我对java编码很陌生。

    package com.track.mobile;
    /* Skipped a bunch of standard imports */  
    import org.primefaces.model.map.Circle;
    import org.primefaces.model.map.DefaultMapModel;
    import org.primefaces.model.map.LatLng;
    import org.primefaces.model.map.MapModel;
    import org.primefaces.model.map.Marker;
    import org.primefaces.model.map.Polyline;

    @ManagedBean
    @RequestScoped
    public class MapBean implements Serializable{
        private final double DISTANCE_NM = 2800; //Nautical Miles
        private final double DISTANCE_METERS = DISTANCE_NM*1852; //Meters
        private String centerCoords = "6.920833,103.578611";
        private final MapModel mapModel;

        public MapBean() {
            mapModel = new DefaultMapModel();

            // Draw circle
            //3.1357 N,101.6880 E
            LatLng coord1 = new LatLng(6.920833, 103.578611);
            Circle circle1 = new Circle(coord1, DISTANCE_METERS);
            circle1.setStrokeColor("#FFFF66");
            circle1.setFillColor("#FFFF66");
            circle1.setFillOpacity(0.2);
            mapModel.addOverlay(circle1);

            // Airport Coordinates
            LatLng coord2  = new LatLng(3.1357, 101.6880);
            mapModel.addOverlay(new Marker(coord2));

            // TODO Read Last known position from Database

**List<Points> itemList1 = emf.createEntityManager().
                        createNamedQuery("Points.findAll").getResultList();
                //Iterator i = itemList.iterator();
                // count records
                int cnt = itemList1.size();
                //

                 Points item1 = itemList1.get(0);
                 //
                lat3 =  Long.getLong(item1.getLatitude().toString());
        lon3 =  Long.getLong(item1.getLongitude().toString());
        //**





            LatLng coord3  = new LatLng(6.920833, 103.578611);
            mapModel.addOverlay(new Marker(coord3));


            // flight Path
            Polyline polyline = new Polyline();
            polyline.getPaths().add(coord2);
            polyline.getPaths().add(coord3);
            polyline.setStrokeWeight(5);
            polyline.setStrokeColor("660066");
            polyline.setStrokeOpacity(0.7);
            mapModel.addOverlay(polyline);
        }

        public String getCenterCoords() {
            return centerCoords;
        }

        public MapModel getMapModel() {
            return mapModel;
        }
    }

0 个答案:

没有答案