Solved

Reading database with a string array

Posted on 2011-09-28
2
159 Views
Last Modified: 2012-05-12
I am wondering how I can create a String array without knowing how big it should be.
Look at my code
public String[] getAirports() {
    String[] temp = new String[100];

    
    try {
		mStmt = mCon.createStatement();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
    try {
		if (mStmt.execute("SELECT * FROM `mylab3database`.`airport")) {
			mRs = mStmt.getResultSet();
		} else {
		    System.err.println("select failed");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    try {
		for (int i = 0; mRs.next(); i++) {
		    temp[i] = mRs.getString(1);
		    System.out.println(temp[i]);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}


    
    return temp;
  }

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36715164
You cannot do it.

You can create ArrayList and accumulate yours string to ArrayList
I you then need really and array you can coy arraylist to array
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36715288
Something like that:

public String[] getAirports() {
   // String[] temp = new String[100];
    ArrayList<String>tempList = new ArrayList<String>();
    
    try {
		mStmt = mCon.createStatement();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
    try {
		if (mStmt.execute("SELECT * FROM `mylab3database`.`airport")) {
			mRs = mStmt.getResultSet();
		} else {
		    System.err.println("select failed");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    try {
		for (int i = 0; mRs.next(); i++) {
		 String s = mRs.getString(1);  
                  tempList.add(s);
		    System.out.println(s);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

     String [] temp = new String[tempList.size()];
   for(int j=0; j<tempList.size(); j++)temp[j] = tempList.get(j);
    
    return temp;
  }

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question