javascript—通过jsp将值插入数据库

yb3bgrhw  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(225)

我创建了几个输入框和一个submit按钮来将值插入数据库。这是我的全部code:-

<body   bgcolor=#D3D3D3>
    <form action ="index.jsp" method = "post"> 
    <%
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection connection = 
     DriverManager.getConnection
        ("jdbc:mysql://127.0.0.1:3306/userinfo?user=root&password=admin");

   Statement statement = connection.createStatement() ;

   resultset = statement.executeQuery("select * from item") ;
 %>
<center><h1><bold><font size="12">Form</font></bold></h1></center>
    <h5><font size="4">Item Supplier
    <select id="item3" name="item3" onchange = "display_selection()">
    <%  while(resultset.next()){ %>
        <option><%= resultset.getString(2)%>  <%= resultset.getString(3)%> 
 </option>
    <% } %>

     <script> 
    function display_selection ()
    {

        var e = document.getElementById("item3").value;
        var val= document.getElementById("item");

        val.value = e;

    }
    </script>
    </select></font>
    <div class="item2"><input type="text" name="item" id="item" value="" size="165" disabled="disabled" /></div>

    </h5>
<h6>
    <div class="item1"><font size="3">Supplier Name <input type="text" name="supplier" id="supplier" value="" size="14" /></div> 
    <div class="item2"><font size="3">Challan Rate <input type="text" name="challan" id="challan" value="" size="14" /></div> 
</h6>

<h7>
     <font size="3"><center>Receipt Number <input id="random-number"  value="" size="13" disabled="disabled" />

<br>
</h7>
<hr>
<link rel="stylesheet" type=text/css media=all href="css/styles.css">
<div class="container">
<div class="item1">Form Date 

    <input type="text" name="dob" id="datepicker"></div>
<div class="item2"> Challan Quantity <input type="text" id="quantity" name="quantity" value="" size="13" /></div>
</div>
<h12><center>

        Transaction Type <input type="text" id="type" name="<div>type</div>" value="" size="13" />
    </center></h12>

<hr>

<center>
<input type="submit" style="width:100px; height:40px" name="receive" value="RECEIVE"  onclick="random()"/> </form>

<script>
    var seq=1021;
    function random() 
    {

    seq=seq+1;
        document.getElementById("random-number").value = seq;
     }

</script>
   <%
   PreparedStatement pst = null;

    if(request.getParameter("receive")!=null)
    {

    String dob=request.getParameter("dob");
    String supplier=request.getParameter("supplier");
    String type=request.getParameter("type");
    String quantity =request.getParameter("quantity");

    if(dob==null || supplier==null || type==null || quantity==null)
       {
        %>
      <script language="javascript">
            alert("Enter all Data:");
      </script>
      <%
       }
       else
       {
        String queryString = "INSERT INTO stock_quantity(Date_Received,Supplier_Challan,Transaction_Type,Quantity) values(?,?,?,?)";
        pst  = connection.prepareStatement(queryString);
        pst.setString(1,dob);      
        pst.setString(2,supplier);
        pst.setString(3,type);
        pst.setString(4,quantity);
        int i = pst.executeUpdate();
   //  connection.commit();
        if(i!=0){
        %>
      <script language="javascript">
            alert("Send sucess");
      </script>
      <%    
      }
     else{
      %>
      <script language="javascript">
            alert("Error");
      </script>
      <%    
   }
 }
}
%>
</center>
</body>
</html>

现在的问题是,在下面的零件中,数据没有分配给值。。因此,它总是显示空。。我试着直接把值放在插入行中,然后取值。。。。

if(request.getParameter("receive")!=null)
{

String dob=request.getParameter("dob");
String supplier=request.getParameter("supplier");
String type=request.getParameter("type");
String quantity =request.getParameter("quantity");

%>

这里dob,supplier…etc没有得到参数。怎么办??

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题