使用Hibertane和JSP连接到数据库

时间:2016-12-22 18:17:29

标签: hibernate jsp

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

我得到了Error message

我想只使用JSP连接到hibernate,因为它会在内部转换为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>

1 个答案:

答案 0 :(得分: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的旧版hibernate版本。

并且您不能保存User

相关问题