代码如下 :
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
只输出 语文的 ,其他科目 成绩没统计出来 。
----------------解决方案--------------------------------------------------------
for (int i = 9; i <= numCols; i++) {
numCols=多少
----------------解决方案--------------------------------------------------------
是数据库中的列数
----------------解决方案--------------------------------------------------------
我知道
我是问他的值是多少
----------------解决方案--------------------------------------------------------
7列 ,帮帮忙,把我的的程序好好看看,在改改 ?
----------------解决方案--------------------------------------------------------
那就是
for (int i = 9; i <= 7; i++) {
是这个意思吧
----------------解决方案--------------------------------------------------------
for (int i = i; i <= 7; i++) {
数据库中总共7列 ,书写错误 。
但还是存在我说的问题 ,外部不循环 !!!!急啊
帮调调 !!
----------------解决方案--------------------------------------------------------
好象是while循环的事
两个while用的是同一个resultset
你试试吧
----------------解决方案--------------------------------------------------------
行 ,我先试试
----------------解决方案--------------------------------------------------------
还是不行.麻烦你,把程序调调 ,这么看不容易看出问题 ,谢谢.
将正确的发出来
----------------解决方案--------------------------------------------------------