高手帮小弟看看那里不对?
我是想把表shouTable中的count项的值付给数据表monthTble相对应的receive项,程序运行没有报错,但是count的值并没有付给receive,请高手帮忙看下,小弟感激不尽!
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<%@ include file= "checkadmin.jsp "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<body>
<%
String s=session.getAttribute( "s ").toString();
String sqlString= "select * from shouTable where s_date like '% "+s+ "% ' ";
try
{ Connection con;
Statement sql1,sql2,sql3;
ResultSet rs,rs1;
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
con=DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shangdianDB ", "sa ", " ");
sql1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql2=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql3=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql1.executeQuery(sqlString);
while(rs.next()){
String e_name=rs.getString( "e_name ");
String e_size=rs.getString( "e_size ");
int count=Integer.parseInt(rs.getString( "amount "));
rs1=sql2.executeQuery( "select * from monthTable where e_name= ' "+e_name+ " ' and e_size= ' "+e_size+ " ' and mon= ' "+s+ " ' ");
rs1.next();
int receive=rs1.getInt( "receive ");
int amount=count+receive;
sql3.executeUpdate( "update monthTable set receive= "+amount+ " where e_name= ' "+e_name+ " ' and e_size= ' "+e_size+ " ' and mon= ' "+s+ " ' ");
}
con.close();
}
catch(SQLException e1)
{
out.print(e1);
}
%>
<%response.sendRedirect( "mon3.jsp ");%>
</body>
</html>
[解决办法]
很不优美,呵呵!逻辑很不清晰!这样的逻辑为什么要写在页面上.为什么要建立3个STATEMENT
在你的基础上改了一下,你在看看吧
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<%@ include file= "checkadmin.jsp "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN " "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<body>
<%
String s=session.getAttribute( "s ").toString();
String sqlString= "select * from shouTable where s_date like '% "+s+ "% ' ";
try
{ Connection con;
Statement sta;
ResultSet rs,rs1;
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
con=DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shangdianDB ", "sa ", " ");
sta=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
// sql2=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//sql3=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql1.executeQuery(sqlString);
while(rs.next()){
String e_name=rs.getString( "e_name ");
String e_size=rs.getString( "e_size ");
int count=Integer.parseInt(rs.getString( "amount "));
rs1=sta.executeQuery( "select * from monthTable where e_name= ' "+e_name+ " ' and e_size= ' "+e_size+ " ' and mon= ' "+s+ " ' ");
//rs1.next();
int receive ;
while(r1s.next){
receive=rs1.getInt( "receive ");
}
int amount=count+receive;
sql3.executeUpdate( "update monthTable set receive= "+amount+ " where e_name= ' "+e_name+ " ' and e_size= ' "+e_size+ " ' and mon= ' "+s+ " ' ");
}
//以下要判断关闭
rs1.close();
rs.close();
sta.close();
con.close();
}
catch(SQLException e1)
{
out.print(e1);
//以下要判断关闭
rs1.close();
rs.close();
sta.close();
con.close();
}
%>
<%response.sendRedirect( "mon3.jsp ");%>
</body>
</html>