title: Array returned using method reading Column names from MySQL database is full of "null"
body: <p>I am writing a webservice using Java to communicate with a database that I have set up on a MySQL server running on my localhost machine. My method,ListColumns(), to look up the names of the columns in a specified table and convert them from a ResultSet to an array then return that array; is returning an array of the correct size but that is full "null" values. </p>
  
  <pre><code>    import java.sql.*;
  
  public class DBConnect {
  
  private Connection con;
  private Statement st;
  private ResultSet rs;
  
  public DBConnect(){
      try{
          Class.forName("com.mysql.jdbc.Driver");
          String database = "jdbc:mysql://localhost:3306/binapartslist";
          String userID = "root";
          String userPW = "root";     
          //jdbc:mysql://localhost:3306/`database name` --&gt; This is the database
          //userID database user
          //userPW user password
  
          //Connecting to the database
          con = DriverManager.getConnection(database, userID, userPW);
          st = con.createStatement();
      }
      catch(Exception ex){
          System.out.println("Error: "+ex);
      }
  }
  public String getUser(){
      try{
          DatabaseMetaData dmd = con.getMetaData();
          String username = dmd.getUserName();
          //System.out.println("Current User: "+username);
          return username;
      }catch(Exception ex){
          System.out.println(ex);
          return null;
      }
  }
  public void getData(String table){
      int i=1;
      try{
  
          String query = "select * from "+table;
          rs = st.executeQuery(query);
          System.out.println("Records from Database");
  
          String[] columnNames = ListColumns(table);
          int count = columnNames.length;
          System.out.println(ListColumns(table));
  
          System.out.println("number of columns "+count);         
          for(i=0; i&lt;count; i++){
              System.out.println(columnNames[i]);
          }
  
      }catch(Exception ex){
          System.out.println(ex);
      }
  
  }
  public int countTables (String database){
      try{
          DatabaseMetaData dmd = con.getMetaData();
          rs= dmd.getTables(null, database, "%", null);
  
          int count=0;
          while(rs.next()){
              count++;
          }
          return count;
  
      }catch(Exception ex){
          System.out.println(ex);         
          return 0;
      }
  
  }   
  public int countColumns (String table){
      try{
          DatabaseMetaData dmd = con.getMetaData();
          rs = dmd.getColumns(null, null, table, "%");
  
          int count=0;
          while(rs.next()){
              count++;
          }
          return count;
  
      }catch(Exception ex){
          System.out.println(ex);         
          return 0;
      }
  
  }
  public String[] ListColumns(String table){
      String[] columnsList = new String[countColumns(table)];
      try{            
          DatabaseMetaData dmd = con.getMetaData();
          rs = dmd.getColumns(null, null, table, "%");
  
          int i=0;
          while(rs.next()){
              columnsList[i]=rs.getString("COLUMN_NAME");
              i++;
          }
          System.out.println("columnsList[] has "+columnsList.length+" values");
          for(i=0;i&lt;columnsList.length;i++){
              System.out.println("Column Name: "+columnsList[i]);
          }
          return columnsList;         
      }catch(Exception ex){
          System.out.println("i screwed up");
          System.out.println(ex);         
          return columnsList;
      }
  }
  public String[] ListTables(String database){
      try{
          DatabaseMetaData dmd = con.getMetaData();
          rs = dmd.getTables(null, database, "%", null);
  
          System.out.println(database);
          String[] tablesList = new String[countTables(database)];
          int i=0;
          while (rs.next()) {
              tablesList[i]=rs.getString("TABLE_NAME");
              System.out.println("table name: "+rs.getString(3));
              i++;
          }               
          return tablesList;
      }catch(Exception ex){
          System.out.println(ex);
          return null;
      }
  }
  public void createUser(String username, String password, String rank) throws SQLException{
      try{
          con.setAutoCommit(false);
          st.executeUpdate("INSERT INTO `users` VALUES ('"+username+"', '"+password+"', '"+rank+"');");
          con.commit();
          con.setAutoCommit(true);
      }catch (Exception ex ) {
          System.out.println(ex);
              if (con != null) {
                  try {
                      System.err.print("Transaction is being rolled back");
                      con.rollback();
                  }catch(Exception excep) {
                      if (con != null) {
                          con.rollback();
                          con.setAutoCommit(true);
                        }
                      System.out.println(excep);
                  }
              }
      }
  }
  public void deleteUser(String username) throws SQLException{
      try{
          con.setAutoCommit(false);
          st.executeUpdate("DELETE FROM `users` WHERE username = '"+username+"'");
          con.commit();
          con.setAutoCommit(true);
      }catch (Exception ex ) {
          System.out.println(ex);
              if (con != null) {
                  try {
                      System.err.print("Transaction is being rolled back");
                      con.rollback();
                  }catch(Exception excep) {
                      if (con != null) {
                          con.rollback();
                          con.setAutoCommit(true);
                        }
                      System.out.println(excep);
                  }
              }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am calling the class that those methods are in with the following code:</p>
  
  <pre><code>    import java.sql.SQLException;
  
      public class Main
      {
          public static void main(String[] args) throws SQLException {
              DBConnect connect = new DBConnect();
              System.out.println(connect.getUser());
              System.out.println(connect.ListColumns("parts list"));
              connect.ListColumns("parts list");
          }
  
  
      }
  </code></pre>
  
  <p>I need columnsList() to return the array with the Strings of the column names.
  EDIT: I have already confirmed that the array columnsList[] is actually receiving the column names using the for loop and println. Just can not figure out why the return is still full of nulls</p>
  
  <p>Also added all of the code I am using. The ListTables() method is very similar and having the same issues but I figure if I get one fixed the other will be fixed also</p>
  
  <p>Thank you in advance for any help</p>