代码如下 :
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.ResultSetMetaData;
import java.sql.*;
public class Servlet1
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
ResultSetMetaData resultmetadata = null;
DbConnect db = new DbConnect();
Connection conn=null;
Statement statement = null;
ResultSet resultset = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("JDBC:ODBC:test","test","test");
statement = conn.createStatement();
resultset = statement.executeQuery("select * from grade1_table");
//Get the ResultSet information
resultmetadata = resultset.getMetaData();
//Determine the number of columns in the ResultSet
int numCols = resultmetadata.getColumnCount();
String sql="";
int a=0,b=0,c=0,d=0,e=0;
float n=0;
while (resultset.next()) {
for (int i = 9; i <= numCols; i++) {
//For each column index, determine the column name
String colName = resultmetadata.getColumnName(i);
for (int j = 1; j <= resultset.getRow(); j++) {
sql="select "+colName+" from grade1_table";
System.out.println(sql);
resultset = statement.executeQuery(sql);
// String colVal = resultset.getString(j);
while(resultset.next()){
n = resultset.getFloat(colName);
if (n >= 90.0){
a = a + 1;
}else if(n>=80.0){
b = b + 1;
}else if(n>=70.0){
c = c + 1;
}else if(n>=60.0){
d = d + 1;
}else{
e = e + 1;
}
}
//Get the column value
// String colVal = resultset.getString(i);
//Output the name and value
System.out.println(colName);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
//Output a line feed at the end of the row
System.out.println(" ");
// sql="select *from grade2_table where grade='"+gradename+"' and class='"+classname+"'";
// System.out.println(sql);
}
}
resultset.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
问题是 :
内for循环 ,但外for不循环
结果如下 :
select chinese from grade1_table
chinese
1 1 1 1 2
ResultSet is closed
只输出 语文的 ,其他科目 成绩没统计出来 。