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

时间:2014-05-26 04:35:19

标签: sql-server-2008 jsp

<%@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>

现在我从select表中得到3条记录,现在我必须将值放入TextBox并存储到数据库中。我没有得到正确的方法将数据插入到数据库中。所以请帮助。提前谢谢。< / p>

1 个答案:

答案 0 :(得分:0)

我从不建议您在JSP中使用 Scriplet ,而是使用更易于使用且不易出错JavaServer Pages Standard Tag Library

您可以使用JSTL SQL Tag Library来实现它。

使用JSTL Core c:forEach, c:forTokens Tag作为通过Scriptlet嵌入Java forwhiledo-while loop的良好替代方案。


示例代码;

<%@ 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>

JSTL SQL Tag上找到完整的示例代码。