gpt4 book ai didi

mysql - 程序不会读取我的 if 语句

转载 作者:行者123 更新时间:2023-11-30 01:38:10 25 4
gpt4 key购买 nike

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

HTML 代码

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

最佳答案

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

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

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

而不是

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

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

String action = request.getParameter("option") 

关于mysql - 程序不会读取我的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com