0
我正在做這個項目,我差不多完成了。我花了一段時間才真正讓代碼真正運行,但我終於明白了。我的任務是製作HTML表單,使用JSP將其連接到MySQL,並將值插入數據庫或從數據庫中刪除值。它也是密碼保護的。就像我說過的,我已經得到它來編譯沒有錯誤,但是我已經實現的If語句沒有被拾取。我想知道你們是否都可以幫我弄清楚爲什麼會發生這種情況。我要粘貼我的HTML代碼,然後我的JSP。程序不會讀我的if語句
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(pass2 ==「apples4」)..','if(request.getParameter(「action」)。equals(「delete」))....',或者if(request.getParameter(「action 「).equals(」add「)) {' –
可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-在-JAVA) – EJP