0

这是我为班级编写的一种方法。它应该使用从查询数据库中获得的数据刷新表。尝试扫描 newResult.next() 行时出现错误。

我尝试调试,但这并没有告诉我任何东西。代码打印出“在while循环中”这一行,所以我知道问题出在它后面的那一行。我经常收到错误消息“结果集开始后”。我尝试查看我的代码,但看起来我也没有在其他任何地方调用该方法。谢谢。

public void refresh()
{

try
{

   Statement statement = gtPort.getConnection().createStatement();
   //this query is also not working, not really sure how it works.              
   String query = "SELECT CRN, Title, Instructor, Time, Day, Location, Letter"
                      + "FROM Section S WHERE CRN NOT IN " 
                      + "(SELECT CRN FROM Registers R WHERE Username = \""
                      + gtPort.userName + "\")";

    System.out.println(query);
    statement.executeQuery(query);
    System.out.println("Statemetne execute ");

    // String[] columns = {"Select", "CRN", "Title",  "Instructor",  "Time",
    // "Days", "location", "Course Code*", "Section"*,"Mode of Grading*"};  

    ResultSet result = statement.getResultSet();

    System.out.println("created result");
    data = new Object[10][10];

    System.out.println("created data");
    Object[] values = new Object[10];

    System.out.println("created values");
    //  values[0] = null;

    if (result == null)
    {
        System.out.println("result is null");
    }

    String[] titles = new String[100];

    //for (int i = 1; i< table.getColumnCount(); i++)
    //model.removeRow(i);

    //table.removeAll();
    //table.repaint()
    model.setRowCount(0);
    table = new JTable(model);

    model.setRowCount(35);
    for (int i = 1; result.next(); i++)
    {
        values[1] = Boolean.FALSE; 
        for (int j = 2; j< 8; j++)
        values[j] = result.getString(j); 

        titles[i] = result.getString(2);
                model.insertRow(i, values);

    }

    String[] codes = new String[table.getColumnCount()];
    System.out.println("count: " + titles.length);

    for (int i = 1; I < titles.length; i++)
    {
        query = new String("SELECT C.Code FROM Code C WHERE C.Title = \""
                        + titles[i] + "\"");
        //this is a different query to check for titles.        
        statement.executeQuery(query);

        System.out.println(query);
        ResultSet newResult = statement.getResultSet();
        //  codes[i] =  newResult.getString(1);

        if (newResult == null)
        {   
            System.out.println("it is null");
            break;
        }
        //this is the loop where it breaks.     
        while(newResult.next());
        {
            System.out.println("in while loop");
                    //this line prints, so the next line must be the problem.                       
            model.setValueAt(newResult.getString(1), i, 8);


        }

        System.out.println("nr: \t" +newResult.getString(1));           
    }       

    System.out.println("before table");
    table = new JTable(model);

    System.out.println("created table");

    }   

    catch (Exception exe)
    {
        System.out.println("errored in course selection");
        System.out.println(exe);
    }
}
4

1 回答 1

1
  1. ResultSet rs = statement.executeQuery(query);改为写。getResultSet()当您从已执行的语句中获得多个结果集时调用。
  2. 不要使用构造函数new String()来创建String. 简单地写:
    String new = "content";
  3. 您无法预测您的第一个查询将返回多少,因此不要创建具有规定大小的数组,而是使用更好的ArrayList

代码:

//creation
List<Object> values = new ArrayList<Object>();
List<String> titles = new ArrayList<String>();
//usage - adding
values.add(someObject);
//usage - getting
for (String title : titles)
//or
titles.get(byIndex);
于 2012-12-04T08:21:54.687 回答