gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:55:20 25 4
gpt4 key购买 nike

我已经在 MySQL 中设置了表,并相信它们都已正确连接,我只是想显示特定信息。我要求用户输入并使用该整数值作为 result.absolute() 的对象。每当我运行 timeresult 输入时,我都会得到“实际时间”的错误行。是java代码还是某些MySQL设置导致显示不正确的行? 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");
}

最佳答案

您获得的结果集未排序。它们返回的顺序由我的 mysql 决定。

尝试

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

关于java - 如何使用 Scanner user_input ResultSet 中的整数变量在 Java 中选择特定的 MySQL 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com