在我的jsp页面中,我试图将值插入数据库,但我没有看到任何错误,页面刷新并且未插入数据

时间:2016-01-16 22:59:25

标签: java html jsp

Here is my code.
<%-- 
    Document   : index
    Created on : Jan 16, 2016, 2:49:24 PM
    Author     : Manoj
--%>
<%@page import="java.sql.*"%>
<% Class.forName("org.apache.derby.jdbc.ClientDriver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Inserting Data</title>
    </head>
    <body>
        <h1>Inserting Data</h1>
        <%!
            public class Contact {
                String URL = "jdbc:derby://localhost:1527/contact";
                String USERNAME = "nbuser";
                String PASSWORD = "nbuser";

                Connection conn = null;
                PreparedStatement pst = null;
                Statement stm = null;
                ResultSet rst = null;

            public Contact(){
                try{
                    conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                    pst = conn.prepareStatement(
                    "INSERT INTO contactinfo (name, address, mobile)"+" VALUES (?, ?, ?)");
                    }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            }
            public int setContact(String name, String address, String mobile)
            {
                 int result =0;
                 try{
                    pst.setString(1, name);
                    pst.setString(2, address);
                    pst.setString(3, mobile);

                    result = pst.executeUpdate();
                 }
                 catch(SQLException e)
                 {
                    e.printStackTrace();
                 }
                 return result;
            }
            }
            %>
            <%
               int result = 0;
               if(request.getParameter("submit") != null){
               String name = new String();
               String address = new String();
               String mobile = new String();

               if (request.getParameter("name")!= null)
               {
                   name = request.getParameter("name");
               }
                 if (request.getParameter("address")!= null)
               {
                   address = request.getParameter("address");
               }
                   if (request.getParameter("mobile")!= null)
               {
                   mobile = request.getParameter("mobile");
               }

               Contact contact = new Contact();
               result = contact.setContact(name, address, mobile); 
               }
            %>
        <form name="myform" action="index.jsp" method="POST">
            <table border="0">
               <tbody>
                    <tr>
                        <td>Name</td>
                        <td><input type="text" name="name" value="" size="30" /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><input type="text" name="address" value="" size="50" /></td>
                    </tr>   
                     <tr>
                        <td>Mobile</td>
                        <td><input type="text" name="mobile" value="" size="10" /></td>
                    </tr>
                </tbody>
            </table>
            <input type="reset" value="Clear" name="clear" />
            <input type="submit" value="Submit" name="submit" />
        </form>
    </body>
</html>

请帮助我。这是我第一次编写JSP代码。 我使用的数据库是Neatbeans 8.1中的Apache derby。我尝试了所有替代方案,但找不到解决方案。当我手动将一行插入数据库时​​,它将插入值。

1 个答案:

答案 0 :(得分:0)

您可以尝试调试代码并检查if(request.getParameter("submit") != null)是否正在返回TRUEFALSE

我怀疑因为你使用了

<input type="submit" value="Submit" name="submit" />

其中value为大写S