-1

この Java メソッドで、ms-sql サーバーからデータを取得しようとしています。列から int 値を取得しようとしていますが、現在使用している列はすべて int ですが、何らかの理由で INT として取得しようとすると、列が nvarchar であるという数値形式エラーが発生します。何が起こっているのかわからず、System.out を実行したときに、列名を取得しているだけで、列に含まれるデータがないことに気付きました。これが私の方法です。何が間違っているのか、何が欠けているのかわかりません。どんな助けでも大歓迎ですありがとう。

private boolean CheckEmployee(long bDays) throws ClassNotFoundException, SQLException {

        PreparedStatement preparedStatement;

        String type = getTypeOfTimeOff().replaceAll("\\s+","");

        Connection conn = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(url, userName, password);

        String selectProject = "SELECT ? FROM EmpVacationTbl Where FullName =? "
                + "AND ManagerName =?";

        preparedStatement = conn.prepareStatement(selectProject);

        preparedStatement.setString(1, getTypeOfTimeOff().replaceAll("\\s+",""));
        preparedStatement.setString(2, getEmpName());
        preparedStatement.setString(3, getManagerName());

        System.out.println(preparedStatement.toString());

        try (ResultSet rs = preparedStatement.executeQuery()) 
        {
            while (rs.next()) 
            {
                //int checker = rs.getInt(1);
                String acheck = rs.getString(1);
                System.out.println("TIME off the user has : " + acheck);
                int checker =  Integer.valueOf(acheck);

                if(checker < bDays)
                {
                    conn.close();
                    message = "Too many days";
                    return false;
                }
                else
                {
                    conn.close();
                    return true;
                }
            }

            if (rs.wasNull()) {
                {
                    conn.close();
                    message = "Unable to find the days";
                    return false;
                }
            }
        }
        conn.close();
        message = "Information not matching recordings.";
        return false;
    }
4

2 に答える 2

1

何らかの理由で、AS をクエリに追加し、if ステートメントをコードに追加すると、結果セットがコードで機能し、データベースから数値を取得できるようになりました。ご協力ありがとうございました。これが誰かに役立つ場合に追加した更新されたコードです。

private boolean CheckEmployee(long bDays) throws ClassNotFoundException, SQLException {


        PreparedStatement preparedStatement;

        Connection conn = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(url, userName, password);

        String selectProject = null;

        if(getTypeOfTimeOff().equalsIgnoreCase("Vacation Day"))
            selectProject = "SELECT VacationDay As dayList FROM EmpVacationTbl Where FullName =? "
                + "AND ManagerName =?";

        else if(getTypeOfTimeOff().equalsIgnoreCase("Bonus Day"))
            selectProject = "SELECT BonusDay As dayList FROM EmpVacationTbl Where FullName =? "
                    + "AND ManagerName =?";

        else if(getTypeOfTimeOff().equalsIgnoreCase("Birthday Day"))
            selectProject = "SELECT BirthdayDay As dayList FROM EmpVacationTbl Where FullName =? "
                + "AND ManagerName =?";

        System.out.println("Query String : " + selectProject);

        preparedStatement = conn.prepareStatement(selectProject);

        preparedStatement.setString(1, getEmpName());
        preparedStatement.setString(2, getManagerName());

        System.out.println(preparedStatement.toString());

        try (ResultSet rs = preparedStatement.executeQuery()) 
        {
            while (rs.next()) 
            {
                int checker = 0 ;
                checker = rs.getInt("dayList");
                System.out.println("Days the user has off are: " + checker );

                if(checker < bDays)
                {
                    conn.close();
                    message = "Too many days";
                    return false;
                }
                else
                {
                    conn.close();
                    return true;
                }
            }

            if (rs.wasNull()) {
                {
                    conn.close();
                    message = "Unable to find the days";
                    return false;
                }
            }
        }
        conn.close();
        message = "Information not matching recordings.";
        return false;
}
于 2015-03-24T19:56:09.113 に答える