日期:2014-05-20  浏览次数:21164 次

JSP 批量插入数据库问题,新手求助
2个jsp代码如下:
******************************************************************
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 


<script type="text/javascript"> 

var i=1; 
function addnewLine(){ 
var table=document.getElementById("tbody"); 

var tr=document.createElement("tr"); 

var td1=document.createElement("td"); 
var td2=document.createElement("td"); 
 
var text1=document.createElement("input"); 
text1.name="1"+i; 
var text2=document.createElement("input"); 
text2.name="2"+i; 


//将文本框对象添加到td 
td1.appendChild(text1); 
td2.appendChild(text2); 


//将td对象添加到tr 
tr.appendChild(td1); 
tr.appendChild(td2); 



//将 tr对象添加到tbody 
table.appendChild(tr); 
i++; 
document.getElementById("count").value=i; 


</script> 
</HEAD> 

<BODY> 
<table id="mytable"> 
<form action="1.jsp" method="post"/> 
<tbody id="tbody"> 
<tr> 
<input type="hidden" name="count" value="1"/> 
<td> <input type="text" name="10"/> </td> 
<td> <input type="text" name="20"/> </td> 


</tr> 
</tbody> 
</table> 
<p> <input type="button" value="增加新行" onClick="addnewLine()"/>
  <input name="submit" type="submit" value="提交" />
</p> 
</form> 
</BODY> 
</HTML> 
**************************************************************************
然后这个是1.jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%
request.setCharacterEncoding("gb2312");

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Library"; 
String user="sa"; 
String password="sa"; 
Connection conn= DriverManager.getConnection(url,user,password); //连数据库

String count = request.getParameter("count").trim(); 
int i = Integer.parseInt(count); 

String sql = "insert into jobs(job_id,job_desc) values(?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);

for (int j = 0; j < i; j++)
{
String job_id = request.getParameter("job_id"+j); 
String job_desc = request.getParameter("job_desc"+j); 

stmt.setString(1,job_id);  
stmt.setString(2,job_desc);

stmt.executeUpdate();
}

%>
<body>