0

I'm working on a project where I have an SQL table with just one column that contains strings of holiday names. I am trying to get all of the holiday names in that column into an arraylist using a prepared statement (I have to use a prepared statement). However, I just am lost at to what I am doing wrong. Thanks! Feel free to ask for any clarifications. (FYI the database url is correct and the column name i wish to access is holidays

public class Holidays
{
private  ArrayList<String> holidays;
final String DATABASE_URL = "jdbc:derby://localhost:1527/MovieAgent";
final String SELECT_QUERY = "SELECT HOLIDAYS FROM HOLIDAY";
final String USERNAME = "james";
final String PASSWORD = "am";
private Connection connection;
private Statement statement;
private PreparedStatement selectHolidays;
private PreparedStatement insertHoliday;
private int rowCount;

public Holidays()
{        
    String holidayName;
    holidays = new ArrayList<>();

    try{
    connection = DriverManager.getConnection(DATABASE_URL, "al", "al");   
    statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    selectHolidays = connection.prepareStatement(SELECT_QUERY);
    ResultSet resultSet;
    resultSet = selectHolidays.executeQuery();

    while(resultSet.next())
    {
        holidayName = resultSet.getString(1);
        holidays.add(holidayName);
    }

    }catch(SQLException sqlException)
    {
        sqlException.printStackTrace();
        System.exit(1);
    }     
}

Edit: Sorry I left out that it's simply not working. The values are just not appearing the arraylist. The arraylist (holidays) appears to be empty.

  • 3
    And what's the problem? What's not working? – MadProgrammer Nov 20 '14 at 05:34
  • Sorry about that, forgot to include that. The arraylist just appears empty. I'm not getting any errors but somewhere along the way, I have something wrong that's preventing the arraylist from getting values. – user2921180 Nov 20 '14 at 06:31
  • where have you tried to excess the array list? – Nabin Nov 20 '14 at 06:33
  • That was not included but that is guaranteed to work as my professor gave us that portion of the code. – user2921180 Nov 20 '14 at 06:35
  • @user2921180 : Have you seen my answer? Is `---start---` in holidays ? Can you see data in Netbeans ? [Netbeans Database)](http://stackoverflow.com/a/16764522/1322642) : look at the connection string ! – moskito-x Dec 01 '14 at 12:28

2 Answers2

0

Try changing

final String SELECT_QUERY = "SELECT HOLIDAYS FROM HOLIDAY";

to

 final String SELECT_QUERY = "SELECT * FROM HOLIDAY";

since the table has only one column.

Venkatesh
  • 1
  • 1
0

Generating the list so set a start value and then look whether this is on the list at least.
If so something is wrong with the query or the prepare.

public Holidays()
{        
    String holidayName;
    ArrayList<String> holidays = new ArrayList<String>();
    holidays.add("-- start --");
    try{
 [...]
    while(resultSet.next())
    {

    }
    System.out.println("Holidays names are : "+holidays);

 [...]
moskito-x
  • 11,832
  • 5
  • 47
  • 60