2016-12-22 4 views
0

私はhibernateとJSPを使用してデータベースに接続しようとしましたが、以下は私のコードです。HibertaneとJSPを使用してデータベースに接続

私はとにかくそれを内部でサーブレットに変換されますのみとJSPを使用して休止状態に接続したいError message

を得ました。

私は自分でそれを学んでいますので、愚かな間違いがあるかどうか教えてください。

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.cfg.xmlのために使用することができるhibernate.hbm.xml

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

このDTDに誤りがあります。 例:https://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm

そして、非常に古い休止状態のバージョンhttp://hibernate.sourceforge.netにはDTDを使用しないでください。

あなたはUserを保存しません。

+0

セッションオブジェクトを保存しました。 ses.save()と私はまだ同じエラーメッセージが表示されます。 – krrish

+0

@krrish問題はセーブではなく、無効なDTDである。そして 'ses.save()'はまるで偽のように見えます。 'Session'は' User'を知るためのテレパスのようなものだと思いますか? –

+0

私は初めて冬眠を学習しているので、自分で間違いがあるかもしれません。 マッピングとcfgファイルでDTDを変更する必要があるのか​​、1つだけ変更する必要があるのか​​を教えてください。 また、ses.saveが適切でない場合は、データベースにデータベースにデータを保存する必要があります。 – krrish

関連する問題