Solved

Reading database with a string array

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 85
Where to store the queries for modification of table 4 65
map related example 6 49
Is there a simpler dropbox system? 10 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

832 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