需要帮助将用户输入发送到另一种方法

时间:2013-10-23 07:16:48

标签: java mysql crud

让我快速解释一下。我在7月份通过Java公司参加了为期5周的课程。他们介绍了基本的东西,比如控制台应用程序,crud操作,mysql和n层架构。自从课程结束以来,我没有使用它,因为我回去工作,其他医疗原因浮出水面......等等等等。

公司告诉我制作一个简单的程序来反映我学到的东西。结果我保留得很少。

我决定制作一个视频游戏starage计划。它将用于凝视您的视频游戏,因此您不必搜索您的书架(或者您存储游戏的方式。)

为了切入追逐,我似乎无法从一个clas到另一个clas获得用户输入。我的方法AddGame在我的表示层中,应该将用户输入发送到我的逻辑层中的NewGame方法。我一直得到的错误是列'title'不能为null。

这是我的AddGame方法

    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;

}

这是我的NewGame方法

    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,只知道如何创建一个控制台应用程序。我查找的所有内容都是休眠或网络应用程序。 (对不起代码似乎草率或蹩脚) 请任何建议都非常有帮助。提前谢谢!

4 个答案:

答案 0 :(得分:1)

假设您的游戏类具有实例变量的设置器,

使用此

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存储在Games对象的实例变量中。

答案 1 :(得分:1)

AddGame方法中,您不会使用任何数据填充Games对象。您从用户那里读取数据,但只是扔掉它。

我建议你在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,例如addGamenewGame

答案 2 :(得分:1)

我看到的最明显的问题是:

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对象。您遇到的问题是Games对象已初始化但从未真正填充任何内容,这会导致程序抱怨null值。

答案 3 :(得分:0)

您需要修改以下代码

您需要将值从scanline.nextLine();传递到g.setTitle(), g.setRating()g.setPlatform()g.setDeveloper().

System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
System.out.println("Please enter the rating of your game:");
g.setRating(scanline.nextLine());
System.out.println("Please enter the platform for your game:");
g.setPlatform(scanline.nextLine());
System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanline.nextLine());
aref.NewGame(g);