本人在编写jsp调用javabean中 某个类执行sql查询时resultset总是返回空值(sql语句正确, 数据库连接正确,没有报错)
根据网上的写法,现在bean中将resultset返回的值封装再给jsp页面调用。
代码如下为什么myeclipse总是提示行 String[] a=new String[];错误
Variable must provide either dimension expressions or an array initializerLinkedList<String[]> res=new LinkedList<String[]>();
 public LinkedList<String[]> getTeacherName(){
  String sql="select teacher.Tno,teacher.Tname,dept.Deptname,teacher.Ttitle,course.Cname,teach_course.CRno,teach_course.Weekday,teach_course.Period from teacher,course,teach_course,dept where teacher.Tname='"+Tname+"' and teacher.Tno=teach_course.Tno and teacher.Deptno=dept.Deptno and teach_course.Cno=course.Cno";
  conndb  conndb = new conndb();
     ResultSet rs = conndb.executeQuery(sql);
      while(rs.next()){
      String[] a=new String[];
      a[0]=rs.getString(Tno);
      a[1]=rs.getString("Tname");
      a[2]=rs.getString("Deptname");
      a[3]=rs.getString("Ttitle");
      a[4]=rs.getString("Cname");
      a[5]=rs.getString("CRno");
      a[6]=rs.getString("Weekday");
      a[7]=rs.getString("Period");
      res.add(a);
            }
      return res;  
      }

解决方案 »

  1.   

    本人参考的是本网站上的类外一篇帖子。http://bbs.bccn.net/thread-155517-1-1.html
      

  2.   

    楼主没有必要在List里面再存一个数组 直接用ArrayList就好了
      

  3.   


    这里应该是用一个数组一列一列的封装再调用把!我主要是想解决resultset 返回值为空的问题 直接用ArrayList感觉又是逐个循环输出。和没用差不多啊。
    可能是我对ArrayList 了解错误。我等等试下
      

  4.   

    你的变异通过了?
    String[] a=new String[];
    设置一下数组大小
      

  5.   

    楼主   你的数组声明有语法错误。String[] a=new String[];是错误的。
         List<String[]> list = new ArrayList<String[]>(); 
         while(rs.next()){
             String[] a = new String[8];
             a[0]=rs.getString(Tno);
             a[1]=rs.getString("Tname");
             a[2]=rs.getString("Deptname");
             a[3]=rs.getString("Ttitle");
             a[4]=rs.getString("Cname");
             a[5]=rs.getString("CRno");
             a[6]=rs.getString("Weekday");
             a[7]=rs.getString("Period");
             list.add(a);
                   }
          return list;  
          }
    楼主还需要把你的方法改写为public List<String[]> getTeacherName()
      

  6.   


    的确,我好像忘记了我的数据不是动态的,只要在填上数组大小就好了。
    只是又出现问题了。在jsp页面怎么调用啊!
    (LinkedList<String[]>)res=(LinkedList<String[]>)request.setAttribute("res");
    不行,不知道怎么弄。刚学习javabean。
      

  7.   


    return 的不是list集合么 楼主有用servlet吗
    request.setAttribute("list",list);
    在页面就能获取值 
      

  8.   


    request.setAttribute("list",list);
    <%
    ArrayList list= (ArrayList)request.getAttribute("list");
    %>