gpt4 book ai didi

java - 为什么我的方法没有收到我的 boolean 值?

转载 作者:行者123 更新时间:2023-12-01 18:13:15 24 4
gpt4 key购买 nike

我的 servlet 中有一个登录位。

else if (userPath.equals("/Login")) {
String LuName = request.getParameter("LuserName");
String Lpass = request.getParameter("Lpassword");
boolean log = Boolean.parseBoolean(request.getParameter("loggedIn"));
JavaSqlDb objDb2 = new JavaSqlDb("LineEquation");
objDb2.SelectDataReturn(LuName, Lpass, log);
String myUrl = "Welcome" + ".jsp";
try {
// Redirect back to the new URL with new data. Sending request from servlet to JSP.
if (log == true) {
request.getRequestDispatcher(myUrl).forward(request, response);
}
else {
System.out.println("Ya goofed.");
}
} catch (Exception ex) {
ex.printStackTrace();
}

}

一种查找所获取值的方法。

public boolean SelectDataReturn(String LuserName, String Lpassword, boolean loggedIn) {
// Public boolean or void?
Statement s = null;
ResultSet rs = null;
String dbQuery = "SELECT * FROM Users WHERE userName = '" + LuserName + "' AND password = '" + Lpassword + "' ";
try {
s = this.dbConn.createStatement();
rs = s.executeQuery(dbQuery);
if (rs.next()) {
LuserName = rs.getString(1);
Lpassword = rs.getString(2);
//loggedIn = rs.getBoolean(3);
loggedIn = true;
System.out.println(dbQuery + "found.");
} else {
//LuserName = rs.getString(1);
//Lpassword = rs.getString(2);
loggedIn = false;
System.out.println(dbQuery + "not found.");
}
String connectionURL = "jdbc:mysql://localhost:3306/" + this.dbName;
this.dbConn = DriverManager.getConnection(connectionURL, "root", "mysql1");
Statement st = this.dbConn.createStatement();
st.executeQuery(dbQuery);
dbConn.close();
} catch (SQLException se) {
System.out.println("SQL Error: Not able to find data.");
se.printStackTrace(System.err);
System.exit(0);
}

return loggedIn;
}

还有一个主要方法。

public static void main(String[] args) {
String tableName = "Users";
ArrayList<ArrayList<String>> myData;
String dbName = "LineEquation";
String[] tableHeader = {"userName", "password"};
JavaSqlDb objDb2 = new JavaSqlDb(dbName);
myData = objDb2.getData(tableName, tableHeader);
for (int row = 0; row < myData.size(); row++) {
System.out.println(myData.get(row));
}
objDb2.insertData("b", "2");
boolean test = objDb2.SelectDataReturn("Dhruv", "pass", false);
}

我是 servlet 和数据库的相对初学者,所以你可以理解我为什么不明白为什么,出于某种原因,当我在文本字段中输入值并提交它们时,我收到 404 错误和/或空白屏幕。我的 servlet 是否没有接收到我的 boolean 值或类似的值,我是否会进入一个不存在的页面?

最佳答案

boolean 值是不可变的。如果您想在代码示例中按照您想要的方式使用 boolean 值,则需要使用可变 boolean 值。

这篇文章中有一些选项。 Mutable boolean field in Java

关于java - 为什么我的方法没有收到我的 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60423358/

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