在运行程序时获得0> = 0

时间:2017-11-15 09:46:26

标签: java sql sql-server-2008

我运行此代码

public void loadtwo()
{
    try{
        String query="SELECT Grade.GradeName, [Group].GroupName, AcademicInfo.EntranceYear, Semester.SemesterName, AcademicRank.AcademicRankName, Others.OthersName, AcademicInfo.AcademicInfoID,AcademicInfo.Major, AcademicInfo.Department\n "+
                "FROM AcademicInfo INNER JOIN\n" +
                "AcademicRank ON AcademicInfo.AcademicRankID = AcademicRank.AcademicRankID INNER JOIN\n" +
                "Grade ON AcademicInfo.GradeID = Grade.GradeID INNER JOIN\n" +
                "[Group] ON AcademicInfo.GroupID = [Group].GroupID INNER JOIN\n" +
                "Others ON AcademicInfo.OthersID = Others.OthersID INNER JOIN\n" +
                "Semester ON AcademicInfo.SemesterID = Semester.SemesterID";
        DBlabprj.rs=DBlabprj.stmt.executeQuery(query);
        int i=0;
        while(DBlabprj.rs.next())
        {
            edutbl.setValueAt(DBlabprj.rs.getString("GradeName"),i,0);
            edutbl.setValueAt(DBlabprj.rs.getString("GroupName"),i,1);
            edutbl.setValueAt(DBlabprj.rs.getInt("EntranceYear"),i,2);
            edutbl.setValueAt(DBlabprj.rs.getString("SemesterName"),i,3);
            edutbl.setValueAt(DBlabprj.rs.getString("AcademicRankName"),i,4);
            edutbl.setValueAt(DBlabprj.rs.getString("OthersName"),i,5);
            edutbl.setValueAt(DBlabprj.rs.getInt("AcademicInfoID"),i,6);
            edutbl.setValueAt(DBlabprj.rs.getString("Major"),i,7);
            edutbl.setValueAt(DBlabprj.rs.getString("Department"),i,8);

            i++;
        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(rootPane,e.getMessage());
    }
}

我收到此错误:

  

0> = 0

我无法理解错误,当我从群组中移除[]时会收到此错误:

  

单词'组'

附近的语法不正确

2 个答案:

答案 0 :(得分:0)

第二个错误是由于group是T-SQL的关键字,因此您可以尝试使用其他变量对表进行别名。

答案 1 :(得分:0)

我已从您的查询中删除/ n并使用此替换空格。 Group是保留关键字,但如果它在括号内,则不会产生错误。最好尝试不同的名字。

String query="SELECT Grade.GradeName, [Group].GroupName, AcademicInfo.EntranceYear, Semester.SemesterName, AcademicRank.AcademicRankName, Others.OthersName, AcademicInfo.AcademicInfoID,AcademicInfo.Major, AcademicInfo.Department  "+
                       "FROM AcademicInfo INNER JOIN " +
                       "AcademicRank ON AcademicInfo.AcademicRankID = AcademicRank.AcademicRankID INNER JOIN " +
                       "Grade ON AcademicInfo.GradeID = Grade.GradeID INNER JOIN " +
                       "[Group] ON AcademicInfo.GroupID = [Group].GroupID INNER JOIN " +
                       "Others ON AcademicInfo.OthersID = Others.OthersID INNER JOIN " +
                       "Semester ON AcademicInfo.SemesterID = Semester.SemesterID";

JTable列以1开头而不是0,因此make int i=1;

edutbl.setValueAt(DBlabprj.rs.getString("GradeName"),i,0);