通过jsp将值插入数据库

时间:2018-06-12 14:58:44

标签: javascript mysql database jsp

我创建了几个输入框和一个提交按钮,用于将值插入数据库。 这是我的全部代码: -

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

现在的问题是,在下面的部分数据没有被分配给值..因此它总是显示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");

%>

这里dob,供应商......等没有得到参数。该怎么办??

0 个答案:

没有答案