String query = "select email from emp_select";
Statement stmt;
try {
DB db=new DB();
db.connect();
stmt = (Statement) db.conn.createStatement(); // DB is connected here
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnValue = rs.getString(i);
name[i]=columnValue;
System.out.println(name[i]); //Everything executes well till here
}
}
stmt.close();
} catch(Exception ex) {}
for (int i = 1; i < name.length; i++) {
System.out.println(name[i]); // why it gives null value here ?
}
String query = select email from emp_select; Statement stmt; try { DB db=new DB();
Share
I was wondering is this what you really want :
Here what is happening is , the while loop starts, now for loop begins execution, now inside the for loop you are giving all elements of name array the same value i.e. rs.getString(i), but your query returns only one column for your rs, i.e. email (but you coding in your for loop something like names[2] = rs.getString(2), name[3] = rs.getString(3), but here there is nothing other than rs.getString(1)). Seems like what you should do is this, that might can give you expected results
Hopefully this might can solve.
Regards