gpt4 book ai didi

java - 空值被插入到数据库中

转载 作者:行者123 更新时间:2023-12-01 09:45:40 25 4
gpt4 key购买 nike

我正在检查提交的数据是否不为空,但在简单提交(无数据)时,它仍然插入数据库中,而且我已将我的列设置为不为空,但数据仍在插入。我不知道为什么?

在我的 Servlet 中

try{
String department=request.getParameter("department");
String company=request.getParameter("company");
String place=request.getParameter("place");
boolean checkd=checknull.value(department);
boolean checkc=checknull.value(company);
boolean checkp=checknull.value(place);
if(checkd==true&&checkc==true&&checkp==true) {
Connection con=ConnectionProvider.getCon();
String sql="insert into department(departmentname,company,place) values (?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);

pstmt.setString(1,department);
pstmt.setString(2,company);
pstmt.setString(3,place);
int rs=pstmt.executeUpdate();
if(rs>0){status=true;}

if(status){
PrintWriter out= response.getWriter();
out.print("values have been inserted,"+admin);
response.sendRedirect("inserted.jsp");
}
else
{
PrintWriter out= response.getWriter();
out.print("failed to insert");
response.sendRedirect("notinsered.jsp");
}
}
else{response.sendRedirect("entry.jsp");}
}catch(SQLException e){}

这是正在检查的java类

public class checknull {  
static boolean ch;
public static boolean value(String check)
{
ch = check != null;
return ch;
}

最佳答案

将您的 checknull 类修改为:

public class checknull
{
// static boolean ch; (You don't need this static variable)
public static boolean value(String check)
{
return check != null && check.length() > 0;
}
}

此外,Java 中的惯例是使用 UpperCase 命名类名。

示例:

public class CheckNull

编辑:正如 Erwin Bolwidt 建议的,这里使用的数据库很可能是 Oracle,因为 Oracle 将空字符串和 null 等同起来。

因此,这个答案仅适用于将空字符串视为空值的数据库(例如 Oracle)。

参见:null-vs-empty-string-in-oracle

关于java - 空值被插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067587/

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