Search code examples
javasql-serverdatabaseresultsetmssql-jdbc

Java sql Data pulling in the columnName but not the data


On this java method I am trying to get data from a ms-sql server. I am trying to get the int value from a column , Now the columns I am using are all int's but for some reason when i try pulling it as a INT I am getting a number format error saying that the column is a nvarchar. Not sure what is happening and when i ran the System.out I am noticing I am only pulling the column name but no data that the column has. Here is my method, I am not sure what I am doing wrong or what is missing from this. Any help will be greatly appreciated thank you.

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;
    }

Solution

  • For some reason what i did was add an AS to my query along with adding a if statement to my code caused the resultset to work with my code and allowed me to pull numbers from my database. Thank you for your help. Here is the updated code i added if it helps anyone.

    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;
    }