简体   繁体   English

如何使用ResultSet中的Scanner user_input Integer变量在Java中选择特定的MySQL行?

[英]How do I select specific MySQL row in Java using Scanner user_input Integer variable in ResultSet?

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. 我已经在MySQL中设置了表,并相信它们均已正确连接,而我只是在尝试显示特定信息。 I am asking for user input and using that integer value as an object for result.absolute(). 我要求用户输入并将该整数值用作result.absolute()的对象。 Whenever I run the timeresult input, I get the incorrect row for "actualtime". 每当我运行timeresult输入时,都会得到“ actualtime”的错误行。 Is it the java code or some MySQL setting that results in the incorrect row to be displayed? 是Java代码还是某些MySQL设置导致显示不正确的行? All of the other info from println's is correct. 来自println的所有其他信息都是正确的。

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");
   }

The resultset that you are getting is not sorted. 您得到的结果集未排序。 The order that they are being returned is determined my mysql. 它们被返回的顺序由我的mysql确定。

try 尝试

select * FROM suntimes, days, shows WHERE (days.idday = suntimes.idday and suntimes.idshow = shows.idshow) order by actualtime;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM