我檢查提交的數據是不是空的,但只是提交(沒有數據)它仍然插入數據庫中,也使我的列不空,但數據仍然插入。我不知道爲什麼?空值插入數據庫
在我的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){}
這是正在檢查
public class checknull {
static boolean ch;
public static boolean value(String check)
{
ch = check != null;
return ch;
}
表單中的值可能爲空字符串。 –
我應該如何檢查空字符串? –