我在hibernate web应用程序中有一个错误。我认为我的servlet类中存在一个错误

时间:2017-10-12 12:30:36

标签: java eclipse hibernate jsp servlets

我有hibernate代码的bug。我在eclipse IDE中工作,然后把所有的罐子放进去。

当我运行程序时,发生了异常。 请帮助我发生错误。

以下是我的代码:

<nav class="navbar navbar-inverse">
<div class="navbar-header">
    <a class="navbar-brand " href="#">Welcome</a>

</div>
<ul class="nav navbar-nav">
    <li><a href="login.jsp">Sign in</a></li>

</ul>

</nav>

<div class="container">
    <div class="jumbotron">
        <h1>Sign up</h1>
        <p>Fill the following details</p>

        <form class="form-group" action="registered" method="post">

            <div class="form-row">
                <div class="form-group col-md-6">
                    <label for="name">Name:</label> <input type="text"
                        class="form-control" name="name" required placeholder="Full name"><br>
                </div>
                <div class="form-group col-md-6">
                    <label for="pass">Password:</label> <input type="password"
                        class="form-control" name="password" required
                        placeholder="Password"><br>
                </div>
            </div>

            <label for="email">Email:</label> <input type="email"
                class="form-control" name="email" required placeholder="Email"><br>



            <label for="roll">Roll no.:</label> <input type="text"
                class="form-control" name="rollno" required placeholder="Roll no."><br>


            <label for="course">Course:</label> <select class="form-control"
                id="exampleFormControlSelect1" name="course">
                <option>B.tech</option>
                <option>B.Pharma</option>
                <option>MBA</option>
            </select><br> <label for="branch">Branch:</label> <select
                class="form-control" id="exampleFormControlSelect1" name="branch">
                <option>CSE</option>
                <option>IT</option>
                <option>ME</option>
                <option>CS</option>
                <option>EC</option>
                <option>EE</option>
            </select> <small id="emailHelp" class="form-text text-muted"> This
                field is optional</small><br> <br> <label for="gender">Gender:</label>
            <select class="form-control" id="exampleFormControlSelect1"
                name="gender">
                <option>Male</option>
                <option>Female</option>
            </select><br> <label for="year">Year:</label> <select
                class="form-control" id="exampleFormControlSelect1" name="year">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
            </select><br> <label for="percent">Percentage in previous
                semester:</label> <input type="text" class="form-control" name="percent"
                required placeholder="Percentage e.g. 85.2"><br>




            <button type="submit" class="btn btn-primary" name="button1">Submit</button>



            <button type="reset" class="btn btn-success" name="button2">Reset</button>


        </form>

    </div>
</div>

这里是dto代码

package dto;
private String name;
private String password;
private String email;
private int rollno;
private String course;
private String branch;
private String gender;
private int year;
private float percent;

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;
}

public int getRollno() {
    return rollno;
}

public void setRollno(int rollno) {
    this.rollno = rollno;
}

public String getCourse() {
    return course;
}

public void setCourse(String course) {
    this.course = course;
}

public String getBranch() {
    return branch;
}

public void setBranch(String branch) {
    this.branch = branch;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public float getPercent() {
    return percent;
}

public void setPercent(float percent) {
    this.percent = percent;
}

@Override
public String toString() {
    return "student [name=" + name + ", password=" + password + ", email=" + email + ", rollno=" + rollno
            + ", course=" + course + ", branch=" + branch + ", gender=" + gender + ", year=" + year + ", percent="
            + percent + "]";
}

}

**控制器类**

package controller;






@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean flag = false;
    RequestDispatcher reqDispatcher = null;
    try {
        test sd = new test();
        sd.setName(request.getParameter("name"));
        sd.setPassword(request.getParameter("password"));
        sd.setEmail(request.getParameter("email"));
        sd.setRollno(Integer.parseInt(request.getParameter("rollno")));
        sd.setCourse(request.getParameter("course"));
        sd.setBranch(request.getParameter("branch"));
        sd.setGender(request.getParameter("gender"));
        sd.setYear(Integer.parseInt(request.getParameter("year")));
        sd.setPercent(Float.parseFloat(request.getParameter("percent")));

        Configuration con = new Configuration().configure().addAnnotatedClass(test.class);

        SessionFactory sf = con.buildSessionFactory();

        Session session = sf.openSession();

        Transaction tx = session.beginTransaction();
        session.save(sd);
        tx.commit();
        flag = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (flag) {
        reqDispatcher = request.getRequestDispatcher("registered.jsp");
        reqDispatcher.forward(request, response);

    } else {
        reqDispatcher = request.getRequestDispatcher("failure.jsp");
        reqDispatcher.forward(request, response);

    }
}

}

hibernate.cfg.xml中

<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
    <property name="hibernate.connection.username">root</property>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>

    <mapping class="dto.test" />

</session-factory>

我得错误

  HTTP Status 500 - Servlet execution threw an exception

我需要帮助,为什么我的程序无效..谢谢

1 个答案:

答案 0 :(得分:0)

我之前没有使用过很多hibernate,但是我无法在xml文件中看到你在哪里做过“对象关系映射”,或者在实体中看到了注释。

https://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm

https://www.tutorialspoint.com/hibernate/hibernate_annotations.htm