Java 需要帮助将用户输入发送到其他方法吗
让我快速解释一下。7月份,我在一家公司上了一门为期5周的Java课程。他们介绍了一些基本的东西,比如控制台应用程序、crud操作、mysql和n层架构。课程结束后,我没怎么用它,因为我回去工作了,其他的医疗原因浮出水面……等等 公司让我制作一个简单的程序来反映我所学到的东西。结果我保留了很少 我决定制作一个视频游戏starage程序。它将被用来盯着你的电子游戏,这样你就不必搜索你的书架(或者你如何储存你的游戏) 直截了当地说,我似乎无法从一个类别到另一个类别获取用户输入。我的表示层中的AddGame方法应该将用户输入发送到逻辑层中的NewGame方法。我一直得到的错误是列“title”不能为null 这是我的游戏方法Java 需要帮助将用户输入发送到其他方法吗,java,mysql,crud,Java,Mysql,Crud,让我快速解释一下。7月份,我在一家公司上了一门为期5周的Java课程。他们介绍了一些基本的东西,比如控制台应用程序、crud操作、mysql和n层架构。课程结束后,我没怎么用它,因为我回去工作了,其他的医疗原因浮出水面……等等 公司让我制作一个简单的程序来反映我所学到的东西。结果我保留了很少 我决定制作一个视频游戏starage程序。它将被用来盯着你的电子游戏,这样你就不必搜索你的书架(或者你如何储存你的游戏) 直截了当地说,我似乎无法从一个类别到另一个类别获取用户输入。我的表示层中的AddGa
public static Games AddGame() {
Games g = new Games();
Logic aref = new Logic();
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
scanline.nextLine();
System.out.println("Please enter the rating of your game:");
scanline.nextLine();
System.out.println("Please enter the platform for your game:");
scanline.nextLine();
System.out.println("Please thenter the developer of your game:");
scanline.nextLine();
aref.NewGame(g);
return g;
}
这是我的新游戏方法
public static void NewGame(Games g)
{
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
PreparedStatement ps = (PreparedStatement) conn.prepareStatement("INSERT INTO games(Title,Rating,Platform,Developer) " +
"VALUES(?,?,?,?)");
ps.setString(1, g.getTitle());
ps.setString(2, g.getRating());
ps.setString(3, g.getPlatform());
ps.setString(4, g.getDeveloper());
ps.executeUpdate();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我还没有学会如何使用hibernate,只知道如何制作一个控制台应用程序。我查找的所有内容都是hibernate或web应用程序。(如果代码看起来很邋遢或蹩脚,很抱歉)
请告诉我任何建议都会很有帮助。提前谢谢 假设游戏类具有实例变量的setter 用这个
System.out.println("Please enter the title of your game:");
g.setTitle(scanner.nextLine());
System.out.println("Please enter the rating of your game:");
g.setRating(scanner.nextLine());
System.out.println("Please enter the platform for your game:");
g.setPlatform(scanner.nextLine());
System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanner.nextLine());
nextLine方法返回用户输入的文本行,该行使用setter将数据存储在游戏对象的实例变量中。您需要修改代码,如下所示 您需要从
scanline.nextLine()传递值代码>到您的g.setTitle(),
g.setRating()
,g.setPlatform()
和g.setDeveloper()。
在AddGame
方法中,不使用任何数据填充Games
对象。您从用户处读取数据,但只是将其丢弃
我建议您在游戏
对象中添加一个构造函数,该构造函数接受所有必要的参数,例如:
public static Games addGame() {
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
String title = scanline.nextLine();
System.out.println("Please enter the rating of your game:");
String rating = scanline.nextLine();
System.out.println("Please enter the platform for your game:");
String platform = scanline.nextLine();
System.out.println("Please thenter the developer of your game:");
String developer = scanline.nextLine();
Games g = new Games(title, rating, platform, developer);
aref.NewGame(g);
return g;
}
旁注:Java方法应该是camelCase
,例如addGame
和newGame
我看到的最明显的问题是:
Games g = new Games();
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
scanline.nextLine();
System.out.println("Please enter the rating of your game:");
scanline.nextLine();
System.out.println("Please enter the platform for your game:");
scanline.nextLine();
System.out.println("Please thenter the developer of your game:");
scanline.nextLine();
实际上,您忽略了用户传递的任何内容。我假设你想这样做:
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
...
这将基本上填充您的游戏
对象。您遇到的问题是Games
对象已初始化,但从未真正填充任何内容,这导致程序抱怨null
值。我建议您使用mysql的hibernate教程,很好的选择是mykong教程如果解决了你的问题,别忘了接受其中一个答案。祈祷得到了回答!谢谢。@user2910146由于您可以访问构建游戏
对象所需的所有数据,请使用填充对象,而不是多次调用setXXX
。
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
...