通过jsp将数据保存到访问文件中

时间:2014-02-03 18:08:53

标签: java html jsp jdbc

错误是...... 空指针异常 它没有保存数据和错误重复两次

     <%@page import="java.sql.*" %>
     <%@page import="java.sql.DriverManager"%>
     <%@page contentType="text/html" pageEncoding="UTF-8"%>
     <!DOCTYPE html>
     <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>addProduct</title>


   <body>
      <table width="750" height="800" border="3" align="center" bordercolor="#55A0FF">
   <tr>
    <td width="147" height="151" bgcolor="#000000"><img src="PCMS.png" width="145"
    height="133" hspace="0" vspace="0" align="baseline" /></td>
  <td width="584" height="140" bgcolor="#8DC7C7" class="style1"><div    
   align="center">Product Catalogue Management System(PCMS) </div></td>
   </tr>
   <tr>
   <td height="567" valign="top" bgcolor="#B4CC8E"><p><a       
   href="manageProduct.jsp">Home</a></p>
  <p><a href="viewProduct.jsp">View Products </a></p>
  <p><a href="addProduct.jsp">Add Product </a></p>
   <p><a href="deleteProduct.jsp">delete Product</a></p></td>
   <td valign="top" bgcolor="#D6D6D6"><p align="center"><br />
  <strong>Add New Product:  </strong></p>
  <p><strong></strong></p>
  <form id="addform" name="addform" method="post" action="saveDisplay.jsp">

    <table width="394" height="392" border="1" align="center" cellspacing="0"  
     bordercolor="#999999">
    <tr>
      <td width="107" align="left">Product ID:</td>
      <td width="223" align="left"><input name="txtPID" type="text" id="txtPID" /></td>
    </tr>
<tr>
      <td width="107" align="left">Product Name:</td>
      <td width="223" align="left"><input name="txtPName" type="text" id="txtPName" 
      /></td>
     </tr>
    <tr>
      <td align="left">Product Category: </td>
      <td align="left"><select name="select">
        <option selected="selected">Select Category</option>
        <option>Course materials</option>
        <option>T- Shirts</option>
        <option>USB</option>
                          </select></td>
    </tr>
    <tr>
      <td align="left">Price</td>
      <td align="left"><input name="txtPPrice" type="text" id="txtPPrice" /></td>
    </tr>
    <tr>
      <td align="left">Currently available::</td>
      <td align="left"><label>
        <input name="radiobutton" type="radio" value="Yes" />
        In stock 
        <input name="radiobutton" type="radio" value="No" />
        Not in Stock 
      </label></td>
    </tr>
    <tr>
      <td align="left">Quantity</td>
      <td align="left"><input name="txtPQty" type="text" id="txtPQty" /></td>
    </tr>
    <tr>
      <td align="left">Description</td>
      <td align="left"><textarea name="txtPDescription" id="txtPDescription">
       </textarea></td>
    </tr>
    <%   
    try
    {
        //Step 2: Load driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        //Step 3: Define the connection URL
        String url = "jdbc:odbc:productDSN";

        //Step 4: Establish the connection
        Connection con = null;
        con = DriverManager.getConnection(url, "", "");

        //Step 5: Create the statement
        Statement st = con.createStatement();

        //Step 6: Execute the query
        String sql = "SELECT * FROM product";
        PreparedStatement pStmt = con.prepareStatement(sql, 
        ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        ResultSet rs = pStmt.executeQuery();





    String pName = request.getParameter("txtPName");
    String pCat = request.getParameter("select");
        String pPrice = request.getParameter("txtPPrice");
        String inStock = request.getParameter("radiobutton");
        String pQty = request.getParameter("txtPQty");
        String pDesc = request.getParameter("txtPDescription");


       if(inStock.equals("Yes"))
        {
    inStock="Yes";  


        }
        else
        {

    inStock="No";

        }




        rs.last();
        rs.moveToInsertRow();


        rs.updateString(2, pName);
        rs.updateString(3, pCat);
        rs.updateString(4, pPrice);
        rs.updateString(5, inStock);            
        rs.updateString(6, pQty);
    rs.updateString(7, pDesc);



    rs.insertRow();

        con.close();
        out.close();

    }catch(Exception ex)
    {
        System.out.println(ex);
    }


        %>
    <tr>
      <td align="left">&nbsp;</td>
      <td align="left"><label>
        <input type="submit" name="Submit" value="Submit" />
        <input type="Reset" name="Reset" value="Reset" />
      </label></td>
    </tr>
   </table>
    </form>
    </td>
   </tr>



   </table>
    </body>
    </html>

所有完整的页面代码..其中包含数据并将其保存到访问文件中..其中ID字段为自动编号,并且在访问文件中以文本格式进行重新映射。当我点击提交时,它会运行两次。请帮忙

0 个答案:

没有答案