读取代码如下:         ResultSet rs=
         s.executeQuery("select * " +
         "from userProblemSolvedTable");
         ResultSetMetaData metaData=rs.getMetaData();
        
         int numOfColumns=metaData.getColumnCount(); //获得列数
        
         Vector columnNames=new Vector();
//          columnNames.addElement("User Name");
//          columnNames.addElement("Problem AC");
//          columnNames.addElement("Problem WA");
//          columnNames.addElement("Problem PE");
         for(int col=0;col<numOfColumns;col++){
         columnNames.addElement(metaData.getColumnLabel(col+1));
         }
         Vector rows=new Vector();
        
         while(rs.next()){
         Vector newRow=new Vector();
        
         for(int i=1;i<=metaData.getColumnCount();i++){
         newRow.addElement(rs.getObject(i));
         System.out.println(newRow.elementAt(i-1));
         }
        
         rows.addElement(newRow);
         }
显示结果为:
userName problemAc problemWA problemPE
         1         1         1
         1         1         1
就是第一列userName无法显示,我打印出来是null的。不知道为什么?(数据库建表和显示结果的顺序一样) 

解决方案 »

  1.   

    肯定是索引的问题,username里你没有附值!
      

  2.   


    // 這樣看看,你插入的數據和打印出來的是否一樣!
     Object o = rs.getObject(i);
            newRow.addElement(o);
            System.out.println(o);
            System.out.println(newRow.elementAt(i-1));
      

  3.   

    数据库中的表必须建自增的ID序号吗?
    我的表中只有这些列:userName  problemAC  problemWA  problemPE,建表的时候没有加自增的ID列的。
    我觉得ID应该可有可无啊。
    打印输出的结果一样的,但是插入的userName(即newRow.elementAt(0))一列打印的是null;
      

  4.   

    恩, 错误已经解决,是数据类型的问题,貌似JTable不能显示nvarchar的类型数据。把表中nvarchar的数据类型全改成了varchar的了,结果可以显示。谢谢anqini!