Below is what I have been attempting to do.
retrive number of rows from MySql table;
for(int i = 1; i <= numberOfRows; i++){
retrive userID, level at position(i) in table;
// next i need to insert userID into array, level amount of times (ex. userID = 4, level = 6, insert [4,4,4,4,4,4] into array)
while (j < level + numberOfItemsInArrayPrior){
hugeArray[j] = userID;
j++;
}
numberOfItemsInArrayPrior += level;
}
I finally got it to print out without errors, but it is printing a ton of zeros between numbers when i print the array.
static int maxIndex;
static int numberOfEntries;
static int[] mainArray;
static int userID;
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(userIDnum) FROM userInfo");
while(rs.next()){
maxIndex = (Integer) rs.getObject(1);
mainArray = new int[maxIndex];
}
int temp = 0;
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 level, userIDnum from userInfo where userIDnum = '"+i+"'");
while(rs2.next()){
numberOfEntries = (Integer) rs2.getObject(1);
userID = (Integer) rs2.getObject(2);
}
while(j < numberOfEntries + temp){
mainArray[j+temp] = userID;
j++;
}
temp += numberOfEntries;
}
}catch(Exception e1){
System.out.println(e1);
}
for (int k = 0; k < mainArray.length; k++){
System.out.println(mainArray[k]);
}
}
How do i fix this?
Thanks,
oldezwe