HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() throws HibernateException{
return sessionFactory;
}}LoginAction.java
public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException { // TODO Auto-generated method stub
System.out.println("Action do1");
HttpSession session = request.getSession();
System.out.println("Action do2");
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("Action do3");
LoginForm loginForm = (LoginForm) form; String sort = loginForm.getSort();
String username = loginForm.getUsername();
String password = loginForm.getPassword();
int loginSort = Integer.parseInt(sort); String[] userlist = new String[2];
userlist[0] = username;
userlist[1] = password; try {
Transaction tx=s.beginTransaction();
String str = new String();
switch (loginSort) {
case 1:
str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword";
Query query = s.createQuery(str);
System.out.println(username + " " + password);
query.setString("stuName", username);
query.setString("stuPassword", password);
if (query.list().size() > 0) {
s.close();
return mapping.findForward("studentLoginsuccess");
} else
break;
case 2:
str = " from Teacher tea where tea.name = '" + username
+ "' and tea.password ='" + password + "'";
if (s.createQuery(str).list().size() > 0) {
s.close();
return mapping.findForward("teacherLoginsuccess");
} else
break;
case 3:
str = " from Admin admin where admin.name = '" + username
+ "' and admin.password ='" + password + "'";
System.out.println(username + " " + password);
if (s.createQuery(str).list().size() > 0) {
session.setAttribute("id", ((Admin) s.createQuery(str)
.list().get(0)).getId());
s.close();
return mapping.findForward("adminLoginsuccess");
} else
break;
default:
break;
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
s.close();
} ActionMessages errors = new ActionMessages();
errors.add("login error", new ActionMessage("login.error"));
saveErrors(request, errors);
return mapping.getInputForward(); }
}
上面是我的Action和HibernateUtil文件
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() throws HibernateException{
return sessionFactory;
}}LoginAction.java
public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException { // TODO Auto-generated method stub
System.out.println("Action do1");
HttpSession session = request.getSession();
System.out.println("Action do2");
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("Action do3");
LoginForm loginForm = (LoginForm) form; String sort = loginForm.getSort();
String username = loginForm.getUsername();
String password = loginForm.getPassword();
int loginSort = Integer.parseInt(sort); String[] userlist = new String[2];
userlist[0] = username;
userlist[1] = password; try {
Transaction tx=s.beginTransaction();
String str = new String();
switch (loginSort) {
case 1:
str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword";
Query query = s.createQuery(str);
System.out.println(username + " " + password);
query.setString("stuName", username);
query.setString("stuPassword", password);
if (query.list().size() > 0) {
s.close();
return mapping.findForward("studentLoginsuccess");
} else
break;
case 2:
str = " from Teacher tea where tea.name = '" + username
+ "' and tea.password ='" + password + "'";
if (s.createQuery(str).list().size() > 0) {
s.close();
return mapping.findForward("teacherLoginsuccess");
} else
break;
case 3:
str = " from Admin admin where admin.name = '" + username
+ "' and admin.password ='" + password + "'";
System.out.println(username + " " + password);
if (s.createQuery(str).list().size() > 0) {
session.setAttribute("id", ((Admin) s.createQuery(str)
.list().get(0)).getId());
s.close();
return mapping.findForward("adminLoginsuccess");
} else
break;
default:
break;
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
s.close();
} ActionMessages errors = new ActionMessages();
errors.add("login error", new ActionMessage("login.error"));
saveErrors(request, errors);
return mapping.getInputForward(); }
}
上面是我的Action和HibernateUtil文件
解决方案 »
- struts2 中${}的用法
- 类型转换抛异常~!!!
- java保存出现错误
- eclipse 运行不起来咋办捏
- 请教webservice配置问题
- hibernate 对象到数据? 还是 数据到对象?
- 考验struts2高手------<s:checkbox>生成的选择框,如何使点击的文字时,失去选择或不选择效果?
- 请问句柄是什么意思?
- Java 主要用于什么开发,J2EE,J2ME,J2SE各用于什么开发?Java和.NET谁更有优势
- [学生求教]java程序员必须要精通ajax和jquery吗?
- 报错:The provider BC may not be signed by a trusted party 如何解决知道的请帮忙下
- ajax 报异常 java.lang.IllegalStateException: getOutputStream() has already been called for this response
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.stuman.domain.Admin" table="admin">
<id name="id" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="string">
<column name="name" length="32" />
</property>
<property name="password" type="string">
<column name="password" length="32" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://192.168.0.248:1433;database=test;
</property>
<property name="connection.username">sa</property>
<property name="connection.password">dzhdba</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <mapping resource="com/stuman/domain/Admin.hbm.xml" />
</session-factory></hibernate-configuration>以上是我的hibernate配置文件和管理员对象的映射文件
是不是说transaction需要commit()下
好像是Session s = HibernateUtil.getSessionFactory().getCurrentSession()中getCurrentSession()方法的问题,改成openSession()以后就可以运行了!