AdduserAction.javapublic class AdduserAction extends Action { public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws HibernateException { // TODO Auto-generated method stub
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
Admin ulist=new Admin();
ulist.setId("001");
ulist.setName("sonic");
ulist.setPassword("oksonic");
session.save(ulist);
tx.commit();
HibernateUtil.closeSession();
return mapping.findForward("/index.jsp");
}
}
HibernateUtil.java //用来调用hibernate
public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}}hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory> <!-- mapping files -->
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<mapping resource="Add valid path" /><mapping resource="com/test/Hibernate/Admin.hbm.xml" /></session-factory></hibernate-configuration>
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws HibernateException { // TODO Auto-generated method stub
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
Admin ulist=new Admin();
ulist.setId("001");
ulist.setName("sonic");
ulist.setPassword("oksonic");
session.save(ulist);
tx.commit();
HibernateUtil.closeSession();
return mapping.findForward("/index.jsp");
}
}
HibernateUtil.java //用来调用hibernate
public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}}hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory> <!-- mapping files -->
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<mapping resource="Add valid path" /><mapping resource="com/test/Hibernate/Admin.hbm.xml" /></session-factory></hibernate-configuration>
root cause java.lang.ExceptionInInitializerError
all.HibernateUtil.<clinit>(HibernateUtil.java:20)
com.test.struts.action.AdduserAction.execute(AdduserAction.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.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.发生了以上错误,我已经用MyEclipse Database Explorer成功打开数据库。把那三个jar包都放好了。但是总是不能添加数据到数据库。
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: could not insert: [com.test.Hibernate.Admin#402880d8088b06df01088b06ebb70001]
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause net.sf.hibernate.exception.GenericJDBCException: could not insert: [com.test.Hibernate.Admin#402880d8088b06df01088b06ebb70001]
net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)
net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)
net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:472)
net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)
net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)
net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
com.test.struts.action.AdduserAction.execute(AdduserAction.java:64)
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.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Jan 02 19:54:11 CST 2006 -->
<hibernate-mapping package="com.test.Hibernate"> <class name="com.test.Hibernate.Admin" table="admin">
<id name="id" column="id" type="java.lang.String">
<generator class="uuid.string"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<property name="password" column="password" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
主要是这个原因吧!
sessionFactory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
<id name="id" column="id" type="java.lang.String">
<generator class="assigned"/>
</id>
<hibernate-mapping package="com.test.Hibernate">
的package属性去掉看看,
如果package已配置的话,
下面class的name属性就没必要加包名了,
如果还不行的话,你改下面的代码
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
把进一步详细的异常信息贴出来