Link to home
Start Free TrialLog in
Avatar of Mickeys
MickeysFlag for Sweden

asked on

Reading database with a string array

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

Avatar of for_yan
for_yan
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial