日期:2014-05-20  浏览次数:20790 次

ResultSet 的问题!!在线等!菜鸟求救
我的程序是这样的:
package   brick0906;

import   java.sql.*;
import   java.sql.Date;
import   java.util.*;
import   java.io.*;

public   class   ArrayListText   {

public   static   String   drivername= "oracle.jdbc.driver.OracleDriver ";
public   Connection   conn=null;
ResultSet   rs=null;
String   classno=null;
String   classname=null;
String   classteacher=null;
//Date   classdate=null;

public   static   void   main(String   args   [])   throws   ClassNotFoundException,   SQLException,   IOException{

ArrayListText   ar=new   ArrayListText();
ArrayList   al=new   ArrayList();
ar.conninit();
ar.select();
al.add(ar.classno);
al.add(ar.classname);
al.add(ar.classteacher);
PrintWriter   pw   =new   PrintWriter(new   FileWriter(new   File( "F://java.txt ")));
pw.println(al.get(0));
pw.println(al.get(1));
pw.println(al.get(2));
pw.close();
ar.connclose();

}

public   void   conninit()   throws   ClassNotFoundException,   SQLException{
Class.forName(drivername);
String   url= "jdbc:oracle:thin:@169.254.179.82:1521:brick ";
conn=DriverManager.getConnection(url, "brick ", "brick ");
}

public   void   connclose()   throws   SQLException{
conn.close();
}

public   void   select()   throws   SQLException{
Statement   ps=null;
String   sql= "select   *   from   classinfo ";
ps=conn.createStatement();
rs=ps.executeQuery(sql);
while(rs.next()){
this.classno=rs.getString( "classno ");
this.classname=rs.getString( "classname ");
this.classteacher=rs.getString( "classteacher ");
}
}

}

这样的话,我只能在java.txt中显示查询的最后一行,但是我要每行都显示.我弄了很久没搞出来,头都大了,各位大哥帮帮

------解决方案--------------------
while(rs.next()){
  this.classno=rs.getString( "classno ");
  this.classname=rs.getString( "classname ");
  this.classteacher=rs.getString( "classteacher ");
}

你这个当然只能显示最后一行了,若记录集有多条的话,后面的把前的冲掉了,需要更改代码结构。
------解决方案--------------------
楼上的强,