0

I have my tables already set up in MySQL and believe they are all connected correctly, and I am simply trying to display specific information. I am asking for user input and using that integer value as an object for result.absolute(). Whenever I run the timeresult input, I get the incorrect row for "actualtime". Is it the java code or some MySQL setting that results in the incorrect row to be displayed? All of the other info from println's is correct.

public static void main(String[] args) throws Exception {      

    Scanner user_input = new Scanner( System.in );

    String dayinput;
    System.out.print("Choose A Day (1-7): ");
    dayinput = user_input.next();

    //Accessing driver from the JAR file
    Class.forName("com.mysql.jdbc.Driver");

    //Creating a variable for the connection called "con"
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/axamschedule","root","password");
    //jdbc:mysql://localhost:3306/shows-----> This is the database
    //root is the database username
    //password is the database password

    System.out.println("Connected To Database");

    //Creating query for Sundays Table
    if (dayinput.equals("1")){
            PreparedStatement statement = con.prepareStatement("select * FROM suntimes, days, shows WHERE (days.idday = suntimes.idday and suntimes.idshow = shows.idshow)");

            //Creating a variable to execute query
            ResultSet result = statement.executeQuery();

            System.out.println("---------------------");

            Integer timeinput;
            System.out.print("Choose A Timeslot (1-24): ");
            timeinput = 0;
            timeinput = user_input.nextInt();


            if(result.absolute(timeinput)){

                    System.out.println("Time: " + result.getString("actualtime"));
                    System.out.println("Day: " + result.getString("day"));
                    System.out.println("Title: " + result.getString("title"));
                    System.out.println("Length: " + result.getString("length"));
                    System.out.println("Host: " + result.getString("author"));
            }                      
    }

     user_input.close();
   else{
         System.out.println("No Info");
   }
4
  • What do you mean by: I get the incorrect row for "actualtime"? Commented Aug 14, 2014 at 1:11
  • A value from a different row is printed. It appears to be random, but if I input '5', row 5 doesn't print. Another row prints; the same one consistently. Commented Aug 14, 2014 at 1:13
  • 'actualtime' is the column I want to pull from that row to print. Commented Aug 14, 2014 at 1:15
  • 1
    Wouldn't it be better to just select the timeslot? Now you are querying all timeslots to only display one. Commented Aug 14, 2014 at 6:41

1 Answer 1

2

The resultset that you are getting is not sorted. The order that they are being returned is determined my mysql.

try

select * FROM suntimes, days, shows WHERE (days.idday = suntimes.idday and suntimes.idshow = shows.idshow) order by actualtime;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I changed to "select * FROM suntimes, days, shows WHERE (days.idday = suntimes.idday and suntimes.idshow = shows.idshow) ORDER BY idtime"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.