0

I am trying to make an app which import data from database and use it in another function.

I got connection with database and it returns me a variable called miejsce:

public void sprawdzAuto(String nrRej, String miejsce){
     try{
            String query = "select lokalizacja from samochody75 where nrRej=?";
            PreparedStatement pst = con.prepareStatement(query);
            pst.setString(1, nrRej);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                miejsce = rs.getString(1);
                System.out.println(miejsce);

            }


            JOptionPane.showMessageDialog(null, "Znaleziono.");


     } catch(SQLException e) {
         System.out.print(e);
     }
 }

Now when i got miejsce i want to use it in another .java file called Mapa.

public class Mapa extends MapView{
    private Map map;
    public Mapa(String nName)
    {
        JFrame frame = new JFrame(nName);

        setOnMapReadyHandler(new MapReadyHandler() {
            @Override
            public void onMapReady(MapStatus status) {
                if (status == MapStatus.MAP_STATUS_OK){
                    map=getMap();

                    MapOptions mapOptions = new MapOptions();
                    MapTypeControlOptions controlOptions= new MapTypeControlOptions();
                    mapOptions.setMapTypeControlOptions(controlOptions);

                    map.setOptions(mapOptions);
                    map.setCenter(new LatLng(1,1));
                    map.setZoom(17.0);

                    Marker mark = new Marker(map);
                    mark.setPosition(map.getCenter());
                }
            }
        });

        frame.add(this, BorderLayout.CENTER);
        frame.setSize(700,500);
        frame.setVisible(true);
    }

}

I want result of miejsce to map.setCenter(); I tried diffrent things but none of them works. Is there any solution to make it possible?

Thank you in advance.

2
  • "I tried different things": Please show what you tried. Commented Jan 22, 2020 at 13:58
  • I'm not even sure what you want to accomplish... you set your map to latlong 1,1.. Commented Jan 22, 2020 at 13:59

1 Answer 1

1

You seem to missunderstand a lot of concepts about how scope and Java in general works. You should try and find a java tutorial before posting on StackOverflow.

Here are some tips that can help you in your problem.

  • You are iterating through a ResultSet which means you probably won't have only one result. So when you write miejsce = rs.getString(1); only the last value of your result set will be stored in your string.

  • String is an immutable object. Which means that when you pass a String as a parameter to a method, you will not be able to access the value of your String from the calling method unless you return it from the called method. And that's not what you are doing here.

  • To complete my two previous points and to solve partially yours, you can take a look at List in java. Where you can store objects and values, strings included, and you will be able to access the content of your list, filled from the called method (sprawdzAuto here), from your calling method.

I won't do the code for you, I think it is better if you do some digging yourself.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.