0

我正在做这个项目,我几乎完成了。我花了一段时间才真正让代码真正运行,但我终于明白了。我的任务是制作一个 HTML 表单,使用 JSP 将其连接到 MySQL,然后将值插入数据库或从数据库中删除值。它也受密码保护。就像我说的那样,我已经让它编译没有错误,但是我实现的 If 语句没有被拾取。我想知道你们是否能帮我弄清楚为什么会这样。我要粘贴我的 HTML 代码,然后粘贴我的 JSP。

代码

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title> Assignment 3 </title>
</head>
<body>
<h3> Song Name Form </h3>
<form action = "hgooding.jsp" method = "post">

    <table border = "1">
        <tr>
            <td>Song Title: </td>
            <td> <input type = "text" name = "song"/> </td>
        </tr>
        <tr>
            <td>Artist </td>
            <td> <input type = "text" name = "artist"/> </td>
        </tr>
        <tr>
            <td>Password: </td>            
            <td> <input type = "password" name = "pass1"
                size = "20"/> </td>
        </tr>
        </table>
        <br>
          <input type = "radio" name = "option" value = "add"/> Add
           <input type = "radio" name = "option" value = "delete"/> Delete

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

</form>
</body>
</html>

JSP 代码

<%@ page import = "java.sql.*" %>

<html>
<head> <title> Database jsp </title></head>
<body>
<%

String connectionURL = "jdbc:mysql://sql.njit.edu:3306/hg33";
    Connection connection = null;
    Statement stm = null;
    ResultSet rst=null;

Class.forName("com.mysql.jdbc.Driver").newInstance();
     connection = DriverManager.getConnection(connectionURL, "hg33", "grapes34");
     String song = request.getParameter("song");
     String artist = request.getParameter("artist");
     String action = request.getParameter("option");
     String pass2 = request.getParameter("pass1");
     Statement stminsert = null;
     stminsert = connection.createStatement();
if (pass2 == "apples4")
   {     
     if (request.getParameter("action").equals("add"))
    {
      String sqlupdate1=("INSERT INTO Songs VALUES('"+song+"', '"+artist+"')");
    stm.executeUpdate(sqlupdate1);
        out.println("Hi!");
}
if (request.getParameter("action").equals("delete")) 
{
    String sqlupdate2=("DELETE FROM Music WHERE Song =('"+song+"')");
    stm.executeUpdate(sqlupdate2);
}       

rst = stm.executeQuery("SELECT * from Music");
   }
else
   {
    out.println( "Password is not correct!!!" );
   }        
     out.println("insert attempted");
%>
</body>
</html>
4

1 回答 1

0

我猜你在 if 语句中的条件应该是这样的

if (action.equals("add")){.........................}

或者

if (action == "add"){.........................}

代替

if (request.getParameter("action").equals("add")){....................} 

因为你像这样在动作变量中捕获选项

String action = request.getParameter("option") 
于 2013-05-15T04:08:12.130 回答