2016-12-22 49 views
0

我试图用hibernate和JSP连接数据库,下面是我的代码。使用Hibertane和JSP连接数据库

Error message

我想连接使用JSP只反正它会被转换为内部Servlet来冬眠。

我正在自己学习,所以请让我知道是否有一些愚蠢的错误。

index.html 

<html> 
<head> 
    <title>TODO supply a title</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 
<body> 

    <form action="action.jsp" method="post"> 
    Name <input type="text" name = "name"> <br> 
    Password <input type="password" name="password"> <br> 
    <input type="submit" value="submit"> 
    </form> 
</body> 
</html> 


action.jsp 

<%@page import="org.hibernate.Transaction"%> 
<%@page import="p1.User"%> 
<%@page import="org.hibernate.Session"%> 
<%@page import="org.hibernate.SessionFactory"%> 
<%@page import="org.hibernate.cfg.Configuration"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 

    <% 
     Configuration cfg = new Configuration(); 
     cfg.configure("hibernate.cfg.xml"); 
//  out.println("Configuration object created"); 
     SessionFactory sf = cfg.buildSessionFactory(); 
     Session ses = sf.openSession(); 
     Transaction t = ses.beginTransaction(); 

     String n = request.getParameter("name"); 
     String p = request.getParameter("password"); 

     // out.println("Welcome " + n); 
     User u1 = new User(n, p); 
     ses.save(); 
     t.commit(); 
     ses.close(); 
     out.println("Data inserted successfully"); 

    %> 
</body> 
</html> 

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> 
    <property         name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/form?zeroDateTimeBehavior=convertToNull</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">tiger</property> 
    <mapping resource="p1/hibernate.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 


hibernate.hbm.xml 

    <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    <hibernate-mapping> 
    <class name="p1.User" table="user"> 
     <property name="name" column="uname"></property> 
     <property name="password" column="password"></property> 
    </class> 
</hibernate-mapping> 

回答

0

有一个在hibernate.hbm.xml

http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd 

DTD此误差可用于仅hibernate.cfg.xml。 请参考这个例子:https://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm

而且,请不要使用DTD从http://hibernate.sourceforge.net非常老的休眠版本。

而且您不保存您的User

+0

我已经保存了会话对象,例如: ses.save()和我仍然收到相同的错误信息。 – krrish

+0

@krrish问题不在于保存,而是使用无效的DTD。 'ses.save()'看起来像废话。你认为'会话'就像是一个心灵感应器,可以全面了解'用户'吗? –

+0

我正在学习第一次使用hibernate,因此可能会出现错误。 请让我知道是否需要在映射和cfg文件中更改DTD,或只更改它应该更改的DTD。 另外建议如何将数据保存到数据库到数据库如果ses.save不是一个好主意。 – krrish