如何在netbeans中编写更新,删除和搜索代码

时间:2017-03-17 16:43:30

标签: html mysql jsp

我是netbeans的初学者。在我的代码中,insert命令正在运行。但更新,删除和搜索命令无效。

这是我的html表单。

Patients.html

    <html>
<head>

    <title>Patients</title>
</head>
<body>
    <h1><font color="1B407F">Patient Details</font></h1>
    <form method="GET" action="Patients.jsp">
        ID Number&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="pid"><br><br>
        Name&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="pname"><br><br>
        Mobile Number&nbsp<input type="text" name="pmobile"><br><br>
        Disease&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="text" name="pdisease"><br><br>

        <input type="submit" name="btn" value="ADD">&nbsp&nbsp&nbsp
        <input type="submit" name="btn" value="SEARCH">&nbsp&nbsp&nbsp
        <input type="submit" name="btn" value="UPDATE">&nbsp&nbsp&nbsp
        <input type="submit" name="btn" value="DELETE">

    </form>
</body>

这是jsp文件。

Patients.jsp

    <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Patients</title>
</head>
<body>
   <%@ page import="java.sql.*" %>
   <%
       try
       {  
           Connection c;
           Statement s;
           ResultSet rs;

           String id=request.getParameter("pid");
           String name=request.getParameter("pname");
           String mobile=request.getParameter("pmobile");
           String disease=request.getParameter("pdisease");
           String btnvalue=request.getParameter("btn");

           Class.forName("com.mysql.jdbc.Driver");  
           String path="jdbc:mysql://localhost/hospital";
           c=DriverManager.getConnection(path,"root",""); 
           s=c.createStatement();  

           if(btnvalue.equals("ADD")) 
           {
            s.executeUpdate("insert into patients(P_NIC,P_Name,P_Mobile,Disease) values('"+id+"','"+name+"','"+mobile+"','"+disease+"')");
           }

           else if(btnvalue.equals("SEARCH"))
           {
             rs=s.executeQuery("select * from patients where P_NIC='"+id+"'" );
             while(rs.next())
             {
                 String i=rs.getString(1);
                 String n=rs.getString(2);
                 int m=rs.getInt(3);
                 String d=rs.getString(4);

                 System.out.print(i+n+m+d);

             }
           }
             else if(btnvalue.equals("UPDATE"))
                     {
                     s.executeUpdate("update patients set P_Mobile='"+mobile+"' where P_NIC="+id+"");
                     }
             else if(btnvalue.equals("DELETE"))
             {
                 s.executeUpdate("Delete from patients where P_NIC="+id+"");
           }
       }
       catch(ClassNotFoundException e)
       { 
           System.out.println(e.getMessage());
       } 
       catch(SQLException r)
       {
           { System.out.println(r.getMessage());}
       }


      %>
</body>

   The MySQL table patients contain 4 varchar fields such as P_NIC, P_Name, P_Mobile and Disease.

请帮我弄清问题。谢谢。

1 个答案:

答案 0 :(得分:0)

包含您收到的错误。这将有助于其他人找出真正的问题。

最好的方法是使用Servlet。选中此tutorial

 Logger.getLogger(SERVLETNAME.class.getName()).log(Level.SEVERE, null, ex);

使用此代码捕获并记录您的错误。

  

SERVLETNAME是您的Servletclass的名称,ex是Exception的变量名称

声明不安全,不是一个好的做法使用PreparedStament 有关JDBC

的更多信息