?
Solved

Reading database with a string array

Posted on 2011-09-28
2
Medium Priority
?
160 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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