-----------------------------
java.lang.NullPointerException
at classmate.DBUser.checkUser(DBUser.java:25)
at classmate.LoginAction.execute(LoginAction.java:29)
……
空指针异常
-----------------------------
以上是控制台返回信息
-----------------------------
package classmate;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
public class DBUser {
DataSource dataSource=null;
public DBUser(DataSource dataSource){
this.dataSource=dataSource;
}
public boolean checkUser(String name,String psw)
throws Exception{
Connection connect=null;
String strSql;
ResultSet rs;
boolean result=false;
strSql="select * from classuser where username=';"+name
+"'; and password=';"+psw+"';";
try{
connect=dataSource.getConnection();
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(strSql);
if(rs.next()){
result=true;
}
}catch(SQLException ex){
ex.printStackTrace();
}catch(NullPointerException ee){
ee.printStackTrace();
System.out.println("空指针异常");
}
finally{
if(connect!=null)
connect.close();
}
return result;
}
}
-----------------------------
以上是DBUser类(作为Bean)
-----------------------------
package classmate;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
public final class LoginAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
UserForm userform=(UserForm)form;
String name=userform.getName();
String psw=userform.getPsw();
ServletContext context=servlet.getServletContext();
DataSource dataSource=(DataSource)context.getAttribute(
"mysqlDB1");
DBUser dbuser=new DBUser(dataSource);
HttpSession session=request.getSession();
if(!dbuser.checkUser(name,psw)){
ActionMessages errors=new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.deny"));
if(!errors.isEmpty()){
saveErrors(request, errors);
}
return mapping.findForward("failed");
}else{
return mapping.findForward("successed");
}
}
}
-----------------------------
以上是LoginAction类(作为Action使用)
-----------------------------
--------------------------------
struts-config.xml文件
已经设置了数据库,为什么dataSource还是null?
整个项目的代码
和tomcat的admin工具包有没有关系???或者JDBC链接包??? |