gpt4 book ai didi

java - 将 boolean 值设置为 Activity 或非 Activity 存在于包含用户列表的 View 页面的下拉菜单中

转载 作者:行者123 更新时间:2023-11-29 11:15:50 25 4
gpt4 key购买 nike

我是编程初学者,这是我的 DAO 文件,用于一个带有更新和删除方法的简单登录应用程序,更新和删除方法都有单独的 servlet 文件,我可以毫无问题地执行相同的操作,有人问我向表中添加一个 boolean 列,其中 true 值指示用户 处于 Activity 状态,反之亦然。

我知道引用 boolean 功能的代码是错误的。我希望我能得到一些帮助。 boolean 值的访问器方法写入另一个文件中。我在整个互联网甚至这个网站上进行了搜索,但两天来还是无法弄清楚。

import java.util.*;
import java.sql.*;

public class UserDao {

//connection to jdbc mysql server//

public static Connection getConnection(){
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="mylogin";
String driver="com.mysql.jdbc.Driver";
String userName="root";
String dbPassword="admin";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url + dbName,userName,dbPassword);
}catch(Exception e){
System.out.println(e);
}
return conn;
}

//* block to update the user details present inside a table in DB *//

public static int update(User e){
int status=0;
try{
Connection conn=UserDao.getConnection();
PreparedStatement ps=conn.prepareStatement("UPDATE UDATA SET username=?,password=?,address=?,phone=?,email=? where id=?");
System.out.println("UPDATE UDATA SET username="+e.getName()+",password="+e.getPassword()+",address="+e.getAddress()+",phone="+e.getPhone()+",email="+e.getEmail()+" where id="+e.getId());

ps.setString(1,e.getName());
ps.setString(2,e.getPassword());
ps.setString(3,e.getAddress());
ps.setString(4,e.getPhone());
ps.setString(5,e.getEmail());
ps.setInt(6,e.getId());

status=ps.executeUpdate();

conn.close();
}catch(Exception ex){ex.printStackTrace();}

return status;
}

// code for performing deletion process//

public static int delete(int id){
int status=0;
try{
Connection conn=UserDao.getConnection();
PreparedStatement ps=conn.prepareStatement("DELETE FROM udata where id=?");
ps.setInt(1,id);
status=ps.executeUpdate();

conn.close();
}catch(Exception e){e.printStackTrace();}

return status;
}

//code to perform the boolean operation //

public static boolean userStatus(){
int id=0;
int status=0;
boolean active=true;
try{
Connection conn=UserDao.getConnection();
PreparedStatement ps=conn.prepareStatement("SELECT CASE WHEN status IS NULL THEN 'false' ELSE 'true' END FROM udata;");
PreparedStatement ps1=conn.prepareStatement("UPDATE udata where id=? and status=?");
ps.setBoolean(1, active);
ps.setInt(2, id);
status=ps.executeUpdate();

}

return active;
}

Here is the setter and getter methods of the parameters. There is a similar file for Users This one only involves Admin credentials

     public class Admin {

private int id;
private String adName,adPassword;
private static boolean userStatus;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return adName;
}
public void setName(String name) {
this.adName = name;
}
public String getPassword() {
return adPassword;
}
public void setPassword(String password) {
this.adPassword = password;
}
public static boolean isUserStatus(){
return userStatus;
}
public void setUserStatus(boolean active){
Admin.userStatus=true;
}
}

最佳答案

查看代码,您在 userStatus() 方法中似乎在错误的准备好的语句中设置了参数

改变

ps.setBoolean(1, active);
ps.setInt(2, id);

ps1.setBoolean(1, active);
ps1.setInt(2, id);

但我仍然很困惑为什么要为选择查询设置参数?另外为什么你不执行它来获取结果集?

关于java - 将 boolean 值设置为 Activity 或非 Activity 存在于包含用户列表的 View 页面的下拉菜单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789203/

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