我想把数据循环存入数据库,但是只是存进一行数据,然后后面的就抛出异常。<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%
for(int i=1;i<=4;i++){
//接收客户端提交的数据
String list_number=codeToString(request.getParameter("list_number"));
int id=i;
String sort_number=codeToString(request.getParameter("sort_number_"+i+""));
if(sort_number==null)//无内容则设为空串
sort_number="";
String e_name=codeToString(request.getParameter("e_name_"+i+""));
if(e_name==null)//无内容则设为空串
e_name="";
String e_size=codeToString(request.getParameter("e_size_"+i+""));
if(e_size==null)//无内容则设为空串
e_size="";
String unit=codeToString(request.getParameter("unit_"+i+""));
if(unit==null)//无内容则设为空串
unit="";
String amount=codeToString(request.getParameter("amount_"+i+""));
if(amount==null)//无内容则设为空串
amount="";
String location=codeToString(request.getParameter("location_"+i+""));
if(location==null)//无内容则设为空串
location="";
String facturer=codeToString(request.getParameter("facturer_"+i+""));
if(facturer==null)//无内容则设为空串
facturer="";
String s_date=codeToString(request.getParameter("data"));
if(s_date==null)//无内容则设为空串
s_date="";
//out.print(e_size);
//构造追加记录SQL语句
String sqlString="insert into shouTable(list_number,id,sort_number,e_name,e_size,unit,amount,location,facturer,s_date)"+
" values('"+list_number+"','"+id+"','"+sort_number+"','"+e_name+"','"+e_size+"','"+unit+"','"+amount+"','"+location+"','"+facturer+"','"+s_date+"')";
//执行SQL语句
try
{ Connection con;
Statement sql;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shangdianDB","sa","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql.executeUpdate(sqlString);
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
}
%>运行后的结果 SQL异常! SQL异常!SQL异常!i=1时的数据被存进了数据库了,后面的抛出异常。
刚学不久,请大家帮帮忙,谢谢了
<%@ page import="java.sql.*" %>
<%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%
for(int i=1;i<=4;i++){
//接收客户端提交的数据
String list_number=codeToString(request.getParameter("list_number"));
int id=i;
String sort_number=codeToString(request.getParameter("sort_number_"+i+""));
if(sort_number==null)//无内容则设为空串
sort_number="";
String e_name=codeToString(request.getParameter("e_name_"+i+""));
if(e_name==null)//无内容则设为空串
e_name="";
String e_size=codeToString(request.getParameter("e_size_"+i+""));
if(e_size==null)//无内容则设为空串
e_size="";
String unit=codeToString(request.getParameter("unit_"+i+""));
if(unit==null)//无内容则设为空串
unit="";
String amount=codeToString(request.getParameter("amount_"+i+""));
if(amount==null)//无内容则设为空串
amount="";
String location=codeToString(request.getParameter("location_"+i+""));
if(location==null)//无内容则设为空串
location="";
String facturer=codeToString(request.getParameter("facturer_"+i+""));
if(facturer==null)//无内容则设为空串
facturer="";
String s_date=codeToString(request.getParameter("data"));
if(s_date==null)//无内容则设为空串
s_date="";
//out.print(e_size);
//构造追加记录SQL语句
String sqlString="insert into shouTable(list_number,id,sort_number,e_name,e_size,unit,amount,location,facturer,s_date)"+
" values('"+list_number+"','"+id+"','"+sort_number+"','"+e_name+"','"+e_size+"','"+unit+"','"+amount+"','"+location+"','"+facturer+"','"+s_date+"')";
//执行SQL语句
try
{ Connection con;
Statement sql;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shangdianDB","sa","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql.executeUpdate(sqlString);
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
}
%>运行后的结果 SQL异常! SQL异常!SQL异常!i=1时的数据被存进了数据库了,后面的抛出异常。
刚学不久,请大家帮帮忙,谢谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货