title: In Java, how do I get my database table to output in columns with headers and lines?
body: <p>I need the output of my code to have columns of ID, NAME and CALORIES, but I'm not sure 'how'. I would like the id# to automatically populate itself, but when I had attempted what I had found on Oracle, it didn't work.  And how do I go about having lines appear that separate everything?</p>
  
  <p>This is my code: </p>
  
  <pre><code>package edu.umsl.java3816.foodItem;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class FoodItemTable {
  
      Connection fit = null;
  
      public static void main(String[] args) {
  
          String createTableSQL = "create table FOOD_ITEM(ID INTEGER,NAME VARCHAR(256),CALORIES INTEGER)";
          String insertTableSQL = "INSERT INTO FOOD_ITEM(ID,NAME,CALORIES) VALUES('1','hamburger','550')";
          String selectSQLStatement = "SELECT * FROM FOOD_ITEM";
  
          FoodItemTable fit = new FoodItemTable();
  
          try {
              fit.getConnection();
              fit.createTable(createTableSQL);
              fit.insertSQL(insertTableSQL);
              fit.selectSQL(selectSQLStatement);
              fit.shutdownDB();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void getConnection() throws Exception {
  
          Class.forName("org.hsqldb.jdbcDriver");
  
          fit = DriverManager.getConnection("jdbc:hsqldb:mem", "sa", // username
                  "");
  
      }
  
      public void createTable(String sqlStatement) {
  
          Statement statement = null;
          try {
              statement = fit.createStatement();
              int i = statement.executeUpdate(sqlStatement);
  
              if (i == -1) {
                  System.out.println("Error: " + sqlStatement);
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  
      public void insertSQL(String insertSQLStatement) {
  
          Statement statement = null;
          try {
              statement = fit.createStatement();
              int i = statement.executeUpdate(insertSQLStatement);
  
              if (i == -1) {
                  System.out.println("Error: " + insertSQLStatement);
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  
      public void selectSQL(String selectSQLStatement) {
  
          Statement statement = null;
  
          try {
              statement = fit.createStatement();
              ResultSet rs = statement.executeQuery(selectSQLStatement);
              while (rs.next()) {
                  System.out.println(rs.getInt("ID"));
                  System.out.println(rs.getString("NAME"));
                  System.out.println(rs.getInt("CALORIES"));
              }
          } catch (SQLException e) {
              e.printStackTrace();
  
          }
      }
  
      public void shutdownDB() {
  
          try {
              fit.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>