主题:变量名无效问题
我想把程序里的累加后的 int变量grades的值更新到数据库里.表名是userTable,要更新的是里面的user_grade.类型是int的;发现到了更新语句那里,'"+name+"'这个没有数据.怎么才能把用户名赋给name呢? 特来请教
<html>
<title>显示成绩</title>
<body bgcolor="ffe4e1">
以下是
<font color="green" size="5">
<%=session.getValue("name")%></font>
的考试成绩和记录<hr>
<%@page contentType="text/html;charset=gb2312"
import="java.sql.*"
%>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:fengying","sa","123");
Statement smt=con.createStatement();
//Statement stmt = con.createStatement();
// sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_CONCUR_UPDATABLE);//**
// int xint=8;
String sql;
String sqll=null;
String answer,realans;
String name = (String)session.getValue("name");//定义用户帐号变量
/*String an1[]=new String[5];//是非题正确答案的数组
String an2[]=new String[5];//单选题正确答案的数组
String an3[]=new String[xint];//多选题正确答案的数组,共有k道题
String ex1[]=new String[5];//是非题作答答案的数组
String ex2[]=new String[5];//单选题作答答案的数组
String ex3[]=new String[xint];*///多选题作答答案的数组
int i,j;
int grade1,grade2,grade3,xint,yint,zint;
int grades=0;
out.println(name);
sql="select * from grade_1";
ResultSet rs=null;
rs=smt.executeQuery(sql);
if(rs.next())
{grade1=rs.getInt("grade1");//grade1是存判断题的分数
grade2=rs.getInt("grade2");
grade3=rs.getInt("grade3");
xint=rs.getInt("topicx"); //xint是存各大题有多少小题
yint=rs.getInt("topicy");
zint=rs.getInt("topicz");
String an1[]=new String[xint];//是非题正确答案的数组
String an2[]=new String[yint];//单选题正确答案的数组
String an3[]=new String[zint];//多选题正确答案的数组,共有k道题
String ex1[]=new String[xint];//是非题作答答案的数组
String ex2[]=new String[yint];//单选题作答答案的数组
String ex3[]=new String[zint];//多选题作答答案的数组
sql="select keys from test1";
rs=smt.executeQuery(sql);
for(i=1;i<=xint;i++)
{
rs.next();
realans=rs.getString(1); //取得正确答案
try
{
answer=request.getParameter("a"+i);
if(answer.equals(realans))//比较是否正确
grades=grades+grade1;//正确加grade1分
}
catch(Exception e)
{
answer="";
}
an1[i-1]=realans.equals("1")?"是":"否";
if(answer.equals("1"))
ex1[i-1]="是";
else if(answer.equals("0"))
ex1[i-1]="否";
else
ex1[i-1]="无";
}
sql="select keys from test2";
rs=smt.executeQuery(sql);
for(i=1;i<=yint;i++)
{
rs.next();
realans=rs.getString(1);//取得正确答案
try
{
answer=request.getParameter("b"+i);
if(answer.equals(realans))//比较是否正确
grades=grades+grade2;//正确加grade2分
}
catch(Exception e)
{
answer="";
}
an2[i-1]=realans;
ex2[i-1]=answer.equals("")?"无":answer;
}
sql="select keys from test3";
rs=smt.executeQuery(sql);
for(i=1;i<=zint;i++)
{
rs.next();
ex3[i-1]="";
an3[i-1]=rs.getString(1);
for(j=1;j<=zint-1;j++)//
{
answer=request.getParameter("c"+i+"-"+j);
if(answer==null)
answer="";
ex3[i-1]=ex3[i-1]+answer;//将各个选择组合成字符串
}
if(ex3[i-1].equals(""))
ex3[i-1]="无";
if(ex3[i-1].equals(an3[i-1]))//比较是否正确
grades=grades+grade3;//正确加grade3分
}
// sql.execute("update userTable set user_grade="+grades+" where user_name='"+name+"'");
sql="update userTable set user_grade="+grades+" where user_name='"+name+"'";//(这里有问题,+name+的值是空的.如果直接把user_name='lily'是可以把分数更新到数据库里user_name是lily的那行去)
try{
smt.execute(sql);
}
catch(SQLException se){
out.println(se.getMessage());
}
//smt.close();
out.println(grades);
con.close();
out.println("<h3>一、是非题答案(每题"+grade1+"分)</h3>");//判断题
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=xint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</th>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");//打印存放正确答案的框
for(i=0;i<=xint-1;i++) //打印正确答案
out.println("<td>"+an1[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");//打印存放考生答案的框
for(i=0;i<=xint-1;i++) //打印考生答案
out.println("<td>"+ex1[i]+"</td>");
out.println("</tr></table>");
out.println("<h3>二、单选题答案(每题"+grade2+"分)</h3>");
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=yint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");
for(i=0;i<=yint-1;i++)
out.println("<td>"+an2[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");
for(i=0;i<=yint-1;i++)
out.println("<td>"+ex2[i]+"</td>");
out.println("</tr></table>");
out.println("<h3>三、多选题答案(每题"+grade3+"分)</h3>");//
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=zint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");
for(i=0;i<=zint-1;i++)
out.println("<td>"+an3[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");
for(i=0;i<=zint-1;i++)
out.println("<td>"+ex3[i]+"</td>");
out.println("</tr></table>");
}
%>
<!--%
sqll="update userTable set user_name='aaaa' where user_name='"+name+"'";
//smt.executeQuery(sqll);
//smt.close();
%-->
<hr><center>总分:<font color="red" size="5">
<%=grades%></font></center>
</body>
</html>
这程序其他的运行都没问题,就是更新不了user_grade的值这个问题:(
<html>
<title>显示成绩</title>
<body bgcolor="ffe4e1">
以下是
<font color="green" size="5">
<%=session.getValue("name")%></font>
的考试成绩和记录<hr>
<%@page contentType="text/html;charset=gb2312"
import="java.sql.*"
%>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:fengying","sa","123");
Statement smt=con.createStatement();
//Statement stmt = con.createStatement();
// sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_CONCUR_UPDATABLE);//**
// int xint=8;
String sql;
String sqll=null;
String answer,realans;
String name = (String)session.getValue("name");//定义用户帐号变量
/*String an1[]=new String[5];//是非题正确答案的数组
String an2[]=new String[5];//单选题正确答案的数组
String an3[]=new String[xint];//多选题正确答案的数组,共有k道题
String ex1[]=new String[5];//是非题作答答案的数组
String ex2[]=new String[5];//单选题作答答案的数组
String ex3[]=new String[xint];*///多选题作答答案的数组
int i,j;
int grade1,grade2,grade3,xint,yint,zint;
int grades=0;
out.println(name);
sql="select * from grade_1";
ResultSet rs=null;
rs=smt.executeQuery(sql);
if(rs.next())
{grade1=rs.getInt("grade1");//grade1是存判断题的分数
grade2=rs.getInt("grade2");
grade3=rs.getInt("grade3");
xint=rs.getInt("topicx"); //xint是存各大题有多少小题
yint=rs.getInt("topicy");
zint=rs.getInt("topicz");
String an1[]=new String[xint];//是非题正确答案的数组
String an2[]=new String[yint];//单选题正确答案的数组
String an3[]=new String[zint];//多选题正确答案的数组,共有k道题
String ex1[]=new String[xint];//是非题作答答案的数组
String ex2[]=new String[yint];//单选题作答答案的数组
String ex3[]=new String[zint];//多选题作答答案的数组
sql="select keys from test1";
rs=smt.executeQuery(sql);
for(i=1;i<=xint;i++)
{
rs.next();
realans=rs.getString(1); //取得正确答案
try
{
answer=request.getParameter("a"+i);
if(answer.equals(realans))//比较是否正确
grades=grades+grade1;//正确加grade1分
}
catch(Exception e)
{
answer="";
}
an1[i-1]=realans.equals("1")?"是":"否";
if(answer.equals("1"))
ex1[i-1]="是";
else if(answer.equals("0"))
ex1[i-1]="否";
else
ex1[i-1]="无";
}
sql="select keys from test2";
rs=smt.executeQuery(sql);
for(i=1;i<=yint;i++)
{
rs.next();
realans=rs.getString(1);//取得正确答案
try
{
answer=request.getParameter("b"+i);
if(answer.equals(realans))//比较是否正确
grades=grades+grade2;//正确加grade2分
}
catch(Exception e)
{
answer="";
}
an2[i-1]=realans;
ex2[i-1]=answer.equals("")?"无":answer;
}
sql="select keys from test3";
rs=smt.executeQuery(sql);
for(i=1;i<=zint;i++)
{
rs.next();
ex3[i-1]="";
an3[i-1]=rs.getString(1);
for(j=1;j<=zint-1;j++)//
{
answer=request.getParameter("c"+i+"-"+j);
if(answer==null)
answer="";
ex3[i-1]=ex3[i-1]+answer;//将各个选择组合成字符串
}
if(ex3[i-1].equals(""))
ex3[i-1]="无";
if(ex3[i-1].equals(an3[i-1]))//比较是否正确
grades=grades+grade3;//正确加grade3分
}
// sql.execute("update userTable set user_grade="+grades+" where user_name='"+name+"'");
sql="update userTable set user_grade="+grades+" where user_name='"+name+"'";//(这里有问题,+name+的值是空的.如果直接把user_name='lily'是可以把分数更新到数据库里user_name是lily的那行去)
try{
smt.execute(sql);
}
catch(SQLException se){
out.println(se.getMessage());
}
//smt.close();
out.println(grades);
con.close();
out.println("<h3>一、是非题答案(每题"+grade1+"分)</h3>");//判断题
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=xint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</th>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");//打印存放正确答案的框
for(i=0;i<=xint-1;i++) //打印正确答案
out.println("<td>"+an1[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");//打印存放考生答案的框
for(i=0;i<=xint-1;i++) //打印考生答案
out.println("<td>"+ex1[i]+"</td>");
out.println("</tr></table>");
out.println("<h3>二、单选题答案(每题"+grade2+"分)</h3>");
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=yint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");
for(i=0;i<=yint-1;i++)
out.println("<td>"+an2[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");
for(i=0;i<=yint-1;i++)
out.println("<td>"+ex2[i]+"</td>");
out.println("</tr></table>");
out.println("<h3>三、多选题答案(每题"+grade3+"分)</h3>");//
out.println("<table border='1'><tr><th bgcolor='90ee90'>题号</th>");
for(i=1;i<=zint;i++)
out.println("<th bgcolor='90ee90'>"+i+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>正确答案</th>");
for(i=0;i<=zint-1;i++)
out.println("<td>"+an3[i]+"</td>");
out.println("</tr><tr><th bgcolor='90ee90'>输入答案</th>");
for(i=0;i<=zint-1;i++)
out.println("<td>"+ex3[i]+"</td>");
out.println("</tr></table>");
}
%>
<!--%
sqll="update userTable set user_name='aaaa' where user_name='"+name+"'";
//smt.executeQuery(sqll);
//smt.close();
%-->
<hr><center>总分:<font color="red" size="5">
<%=grades%></font></center>
</body>
</html>
这程序其他的运行都没问题,就是更新不了user_grade的值这个问题:(