Solved

Reading database with a string array

Posted on 2011-09-28
2
154 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now