Java:添加 boolean 检查方法来检查 SQL 表中的数据以复制到文本文件

标签 java mysql boolean text-files

我对这个问题的措辞不好感到抱歉,但这就是我陷入困境的地方。

我有一个数据库表,其中包含有关捐赠者的数据,例如 ID、姓名、地址、联系方式、年龄(小于或等于 34 岁的人)和血型。

我必须根据血型将捐献者的数据分类为两个文本文件,即 APositive.txtBNegative.txt,即那些具有 A+ 血型的人将有他们的数据位于 APositive.txt 中,B 血型也是如此。应使用 boolean 方法 verifyAge(int, int) 来验证要包含在文本文件中的人员的年龄。

这是我到目前为止所做的:

public class Donor {

    public static boolean verifyAge(int age, int maxAge) {
        if (age <= maxAge) 
            return true;
        else 
            return false;
    }

    private static Formatter writeToFileFormatter;
    private static Formatter writeToFileFormatter1;

    public static void main(String[] args) {

        try {
            writeToFileFormatter = new Formatter("APositive.txt");
            writeToFileFormatter1 = new Formatter("BNegative.txt");
        }

        catch(SecurityException se) {
            System.out.println("Access Denied");
            System.exit(1);
        }

        catch(FileNotFoundException fnfe) {
            System.out.println("Error creating file");
            System.exit(1);
        }

        try {

            Connection con = null;
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/examques?autoReconnect=true&useSSL=false", "root", "admin");

            Statement st = con.createStatement();

            ResultSet rs = st.executeQuery("Select * from donor");

            while (rs.next()) {
                int id = rs.getInt("donor_id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                int tel = rs.getInt("tel");
                int age = rs.getInt("age");
                String blood_grp = rs.getString("blood_group");

                if (blood_grp.equals("A+")) {
                    writeToFileFormatter.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, verifyAge(age, 34));
                }

                if (blood_grp.equals("B-")) {
                    writeToFileFormatter1.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, verifyAge(age, 34));
                }
            }

            if (writeToFileFormatter != null) {
                writeToFileFormatter.close();
            }

            if (writeToFileFormatter1 != null) {
                writeToFileFormatter1.close();
            }

        }

        catch(Exception e) {
            e.printStackTrace();
        }

    }

}

下面是我收到的错误的屏幕截图:

enter image description here

编辑:

谢谢大家的回答

我是这样解决的:

if(blood_grp.equals("A+")&& verifyAge(age, 34)==true){    
    writeToFileFormatter.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel,age);
}

if(blood_grp.equals("B-")&&verifyAge(age, 34)==true){
    writeToFileFormatter1.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, age);
}

最佳答案

您在“姓名:%s 地址:%s 联系人:%d 年龄:%d\n” 字符串中使用 %d 代表年龄。因此,最后一个参数应为十进制,但您的 verifyAge() 函数返回 boolean

关于Java:添加 boolean 检查方法来检查 SQL 表中的数据以复制到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42935299/

相关文章:

php - 调用非对象的成员函数 bindParam()

mysql - 如何优化使用 IN 的 MySQL

c++ - 哪个更快 : if (bool) or if(int)?

python - 如何将整数的pytorch张量转换为 boolean 值的张量?

php - 在 jQuery 中检索 JSON 响应

c - ../src/main.c :16:17: error: 'free' redeclared as different kind of symbol (Boolean)

java - 使用 Maven 在 Java 8 中找不到适合 jdbc 的驱动程序

java - Android异步任务不会改变ui

java - 如何判断矩形是否是黄金矩形?

java - 静态 Servlet 上下文变量