954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Result set objects into an Array.

I am attempting to take 2 integers from a MySql table.

X represents a number that will be stored into an array.
Y represents the number of times that X will be put into the array.

I am attempting to make a loop that will go through, find all the Xs, along with their corresponding Ys and store X into an array Y amount of times...

This is an example of what I have been attempting...

try{
    		   	Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
    		   	Statement st=con.createStatement();
                        // Selects the highest index of X so all Xs get stored into array
    		   	ResultSet rs = st.executeQuery("SELECT MAX(X) FROM table");
    		   	int[] mainArray = {};
    		   	int maxIndex = (Integer) rs.getObject(1);
                        int j = 0;
    		   	for (int i = 0; i < maxIndex; i++){
                                // Selects amount of times to insert given X into array  
    		   		ResultSet rs2 = st.executeQuery("select Y from table where X = '"+i+"'");
    		   		int numberOfEntries = (Integer)rs2.getObject(1); 	
    		   		While(j < numEntries + temp){
    		   			mainArray[j] = i;
                                        j++;
    		   		}
                                int temp = j;
    		   	}
    		   	
    	   	}catch(Exception e1){
    	   		System.out.println(e1);
    	   	}


Though, I have gotten every syntax error possible for what I am trying to do.

I have attempted user rs.next() with my result sets and for some reason can not figure out how to in the right syntax.

Can someone show me how to do what I am trying to do?

Thanks tons,
peace and love

oldezwe

oldezwe
Light Poster
36 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

let's start here:

int[] mainArray = {};

you say that mainArray is an array without elements, and later on, you try to store information in the array for indexes it doesn't have.

an option would be:

int[] mainArray;
int y = getNumberOfElementsForArray();
mainArray = new int[y];


or you could use an arraylist. that way you don't need to know up front the number of elements your array should be able to hold.

if you have any more errors, could you tell us what errors they are? not all of us have much time to go over all of your code.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class siteQueueArrayMake {
	public static void main(String[]args){
		try{
		   	Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
		   	Statement st=con.createStatement();
                    // Selects the highest index of X so all Xs get stored into array
		   	ResultSet rs = st.executeQuery("SELECT MAX(X) FROM table");
		   	int[] mainArray = {};
		   	int temp = 0;
		   	int maxIndex = (Integer) rs.getObject(1);
                    int j = 0;
		   	for (int i = 0; i < maxIndex; i++){
                            // Selects amount of times to insert given X into array  
		   		ResultSet rs2 = st.executeQuery("select Y from table where X = '"+i+"'");
		   		int numberOfEntries = (Integer)rs2.getObject(1); 	
		   		while(j < numberOfEntries + temp){
		   			mainArray[j+temp] = i;
                                    j++;
		   		}
                            temp += j;
		   	}
		   	
	   	}catch(Exception e1){
	   		System.out.println(e1);
	   	}
	}
}


Updated code. The error I get is "Before start of result set."

I have googled syntax of using prepared statements and still can not figure out how to fix this error.

oldezwe
Light Poster
36 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

You must call rs.next() to advance through your result set. Initially your row pointer is positioned before the first result.

Typically you will use if(rs.next()) or while(rs.next()) to advance while there are records to read.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: