一个简单的保存用户名和密码到数据库里,我在UserBean里持久化了类Login后
然后在struts中的Action中调用UserBean里的增加方法addlogin.
但是最后运行的时候产生异常 如下:
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
com.yourcompany.struts.action.LoginAction.execute(LoginAction.java:50)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
这个是我UserBean的代码:
package com.login.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import com.login.Hibernate.Login;
import net.sf.hibernate.cfg.Configuration;
public class UserBean {
public static SessionFactory sf=null;
private static UserBean bean=null;
private UserBean(){
}
public static synchronized UserBean getBean(){
if(bean==null){
bean=new UserBean();
}
return bean;
}
static {
Configuration config=new Configuration();
try {
config.addClass(Login.class);
sf=(SessionFactory) config.buildSessionFactory();
} catch (MappingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void addLogin(Login login){
Session session=null;
Transaction tx=null;
try {
session=SessionFactory.currentSession();
tx=session.beginTransaction();
session.save(login);
tx.commit();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
try {
tx.rollback();
} catch (HibernateException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
e.printStackTrace();
}finally{
if(session!=null){
try {
session.close();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
}
这个是我loginAction的代码:
Loginform loginForm=(LoginForm)actionForm;
Login login=new Login();//持久化的类
login.setUsername(loginForm.getUsername());
login.setUserpwd(loginForm.getUserpwd());
UserBean bean=new UserBean();
bean.addLogin(login); //这就是第50行 或UserBean.getInstance().addLogin(login);一样有异常