Sql server 2008 如何在jsp中使用while循环在数据库中插入值

Sql server 2008 如何在jsp中使用while循环在数据库中插入值,sql-server-2008,jsp,Sql Server 2008,Jsp,现在我从select表中获得3条记录,现在我必须将值放入文本框并存储到数据库中。我没有获得将数据插入数据库的正确方法。因此,请提供帮助。提前感谢。我从不建议您在JSP中使用Scriplet,而是使用更易于使用且不易出错的方法 您可以使用 使用它是通过Scriptlet嵌入Java for、while或do-while循环的一个很好的替代方法 样本代码 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE

现在我从select表中获得3条记录,现在我必须将值放入文本框并存储到数据库中。我没有获得将数据插入数据库的正确方法。因此,请提供帮助。提前感谢。

我从不建议您在JSP中使用Scriplet,而是使用更易于使用且不易出错的方法

您可以使用

使用它是通过Scriptlet嵌入Java for、while或do-while循环的一个很好的替代方法

样本代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <%@page import="java.sql.Statement"%>
    <%@page import="java.util.Date"%>
    <%@page import="java.text.DateFormat"%>
    <%@page import="java.text.SimpleDateFormat"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="webinq.DB"%>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

    <link rel="stylesheet" href="/resources/demos/style.css">
    <link rel="stylesheet" href="dir.css" />
    <script>
     $(function() {
     $("#datepicker").datepicker({ dateFormat: "yy-mm-dd",maxDate:new Date()}).val();
     });
     </script>
</head>
<body>
    <h1>Insert</h1>
    <jsp:include page="navbar.jsp" />
     <table width ="100%" align ="left">
        <tr>
            <td>
                <form name ="cir1"  method="post">
                    <table>
                         <tr>
                            <td>Report</td>
                            <td>
                         <p>Date: <input name="ReserveDate" type="text"  id="datepicker" value="YYYY-MM-DD"/></p>
                            </td>
                         </tr>
                    </table>
   <tr>
   <% out.println("<td><font color=\"red\">Please insert numeric values only.</font></td>"); %> 
   </tr>
   <tr>
  <% out.println("<td><font color=\"red\">Please insert values in rupees only.</font></td>"); %>
   </tr>
    <table border="2" width="100%">
        <td align="center" height="40" width="10%"><b>REGION</b></td>
        <td align="center" height="40" width="10%"><b>CIRCLE</b></td>
        <td align="center" height="40" width="10%"><b>CASH COLLECTION EMI</b></td>
        <td align="center" height="40" width="10%"><b>REPO NOS</b></td>
        <td align="center" height="40" width="10%"><b>REPO ARREAR</b></td>
        <td align="center" height="40" width="10%"><b>TOTAL RESOLUTION</b></td>
        <td align="center" height="40" width="10%"><b>PI COLLECTION</b></td>

   </table> 
    <%
    Connection con=null;
      ResultSet rs=null;
      Statement stmt,stmt1=null;
      try{

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con =  DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DCRA;user=sa;password=sam123;");    
    String ptr=("SELECT [utype],[circle] from [DCRA].[DBO].[Login] where [uid]='"+DB.userid+"'");
    stmt =con.createStatement();
    rs=stmt.executeQuery(ptr);
    while(rs.next())
    {
        String utype=rs.getString(1);
        String circle=rs.getString(2);  
     %>
     <table border="2" width="100%">
        <td align="center" height="40" width="10%"><b><%=utype%></b></td>
        <td align="center" height="40" width="10%"><%=circle%></b></td>
        <td align="center" height="40" width="10%"><input type="text" name ="e1" size="3" value="" id="emi1" onkeyup="total1()"/></td>
        <td align="center" height="40" width="10%"><input type="text" name ="repo1" size="3" value=""/></td>
        <td align="center" height="40" width="10%"><input type="text" name ="a1" size="3" value="" id="arr1" onkeyup="total1()"/></td>
        <td align="center" height="40" width="10%"><input type="text" name ="r1" size="3" id="reso1" value="" readonly/></td>
        <td align="center" height="40" width="10%"><input type="text" name ="pi1" size="3" value=""/></td>
   </table>
     <%   
    }
    }
    catch(Exception e)
    {   
    }
    %>
</body>
</html>

在这里找到完整的示例代码。

你能用更好的方式解释一下吗?另外,我不能用你的insert QUERY查询我有不同的用户,他们属于那里的utype。我正在从select语句中提取该值,并显示状态和那里的文本框。我将用这段代码做这件事,现在我必须插入3个不同于使用文本框的值,比如德里、古吉拉特邦、拉贾斯坦邦的文本框,那么如何插入这些值呢。
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/TEST"
     user="root"  password="pass123"/>

<sql:update dataSource="${snapshot}" var="count">
   INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
</sql:update>