test.jsp的
try {
Integer id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
// Class.forName("jdbc.odbc.JdbcOdbcDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection cn;
Statement st;
ResultSet rs;
//cn=DriverManager.getConnection("jdbc:odbc:JAY");
cn=DriverManager.getConnection("jdbc:ucanaccess://D://J//DB//database.accdb");
st=cn.createStatement();
String s="Select * from emp where EMP_ID="+id+" or EMP_NAME='"+name+"'";
rs=st.executeQuery(s);
if((id.equals("") || id.equals("null")) && (name.equals("") || name.equals("null"))) {
out.print("Enter the username or password");
}
// if(id.equals(""))
if("".equals(id)) {
out.print("Enter the username or password");
}
else if(id.equals("null")) {
out.print("Enter the username or password");
}
else if(name.equals("")) {
out.print("Enter the username or password");
}
else if(name.equals("null")) {
out.print("Enter the username or password");
}
else {
if(rs.next()) {
Integer w=rs.getInt("EMP_ID");
String z=rs.getString("EMP_NAME");
HttpSession sess = request.getSession(true);
sess.setAttribute("user", name);
if((id.equals(w)) && (name.equals(z))) {
out.println("<center><h1>ID is: " + id + "</h1>");
out.println("<center><h1>Welcome: " + name + "</h1>");
response.sendRedirect("jsp1.jsp");
}
else {
out.println("You enter wrong username or password");
}
}
else {
out.println("You enter wrong username or password");
}
}
}
catch(Exception e) {
out.println(e);
}
当我填写正确的详细信息以登录并获取会话ID 当我没有填写id文本框和名称填写bt我得到此错误
java.lang.NumberFormatException: For input string: ""
但我已经检查了空状态 为什么它不执行该代码
如果你传递id文本框中字符串中的值,那么和相同的情况 那个时候出现了这个错误
java.lang.NumberFormatException:对于输入字符串:“jay”
如何处理字符串值传递?
答案 0 :(得分:0)
当您尝试将空String解析为int时,会得到java.lang.NumberFormatException。你的空/空字符串检查在你的parseInt()方法之后。
在尝试parseInt()之前,应该进行空检查。
以下是您当前代码的建议。请注意,有更好的方法来实现您的用例。我建议您首先在语言基础知识,调试等方面进行自我提升(在进入Web开发之前)
try {
String idParam=request.getParameter("id");
String name=request.getParameter("name");
int id = 0;
if(idParam == null || idParam.trim() =="" || name == null || name.trim() == ""){
out.print('Enter username or ...');
}else {
try{
id = Integer.parseInt(idParam);
} catch(NumberFormatException ex) {
out.print("Please enter number for id");
}
// Class.forName("jdbc.odbc.JdbcOdbcDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection cn;
Statement st;
ResultSet rs;
//cn=DriverManager.getConnection("jdbc:odbc:JAY");
cn=DriverManager.getConnection("jdbc:ucanaccess://D://J//DB//database.accdb");
st=cn.createStatement();
String s="Select * from emp where EMP_ID="+id+" or EMP_NAME='"+name+"'";
rs=st.executeQuery(s);
if(rs.next()) {
Integer w=rs.getInt("EMP_ID");
String z=rs.getString("EMP_NAME");
HttpSession sess = request.getSession(true);
sess.setAttribute("user", name);
if((id.equals(w)) && (name.equals(z))) {
out.println("<center><h1>ID is: " + id + "</h1>");
out.println("<center><h1>Welcome: " + name + "</h1>");
response.sendRedirect("jsp1.jsp");
}
else {
out.println("You enter wrong username or password");
}
}
}
catch(Exception e) {
out.println(e);
}
答案 1 :(得分:0)