<form action="register.jsp" method="post">
id:<input type="text" name="id"/><br><br/>
Name:<input type="text" name="name"/><br><br/>
Password:<input type="password" name="password"/><br><br/>
Email ID:<input type="text" name="email"/><br><br/>
<input type="submit" value="register"/>
</form>
上面的文件是index.jsp
<%@page import="com.javatpoint.mypack.UserDao"%>
<jsp:useBean id="obj" class="com.javatpoint.mypack.User"></jsp:useBean>
<jsp:setProperty property="*" name="obj"/>
<%
obj.setName(request.getParameter("name"));
obj.setPassword(request.getParameter("password"));
obj.setEmail(request.getParameter("email"));
try
{
System.out.println("--------->"+obj.getName());
int i=UserDao.register(obj);
if(i>0)
{
out.print("You are successfully registered");
}
else
{
out.print("registration failed");
}
}
catch(Exception e)
{
out.print("You are not registered");
}
%>
上面的文件是register.jsp
package com.javatpoint.mypack;
public class User {
public int id;
public String name,password,email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
以上文件是user.java
package com.javatpoint.mypack;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
public class UserDao {
public static int register(User u){
int i=0;
try{
Session session=new Configuration().configure().buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
t.begin();
i=(Integer)session.save(u);
t.commit();
session.close();
}catch (Exception e) {
e.printStackTrace();
}
return i;
}
}
上述程序是UserDao.java 在执行应用程序时,它找不到hibernate.properties错误 请回复我为什么会发生
答案 0 :(得分:0)
启动时默认情况下,Hibernate会查找hibernate.properties和hibernate.cfg.xml,并在未找到调试消息的情况下记录调试消息。因此,禁用hibernate的debug / info,您将看不到message.see http://forum.springsource.org/showthread.php?127317-hibernate-properties-with-JavaConfig
答案 1 :(得分:0)
确保.properties文件位于类路径的根目录中,这意味着如果您有类似
的内容--src //source folder which all files in it will be compiled/copied to deployment
--user //those are folders which has classes in it
--dao //those are folders which has classes in it
hibernate.properties //should be here
如果它高于结构,那么new Configuration().configure()
应该自动找到它,或者如果你不想把它放在那里那么你可以做类似的事情:
new Configuration().configure(path)
或者看看answer