程序不会读取我的if语句

时间:2013-05-15 03:56:11

标签: mysql jsp

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

HTML CODE

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

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

1 个答案:

答案 0 :(得分:0)

我想if语句中的条件应该是这样的

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

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

而不是

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

你可以在这个动作变量中捕捉选项

String action = request.getParameter("option")