无法执行JDBC批量更新错误

时间:2016-03-01 13:41:14

标签: hibernate jsp servlets

我得到“Hibernate:插入USER(USER_NAME,PASSWORD,EMAIL,PHONE,CITY,ID)值(?,?,?,?,?,?) 无法执行JDBC批量更新 错误” 这个错误。请帮帮我

我有像这样的书面代码

Registration.Jsp

<body>
<h1>Registration Form</h1>
<form action="./UserControllerServlet" method="post">
    <table cellpadding="3pt">
        <tr>
            <td>User Name :</td>
            <td><input type="text" name="userName" size="30" /></td>
        </tr>
        <tr>
            <td>Password :</td>
            <td><input type="password" name="password1" size="30" /></td>
        </tr>

        <tr>
            <td>Confirm Password :</td>
            <td><input type="password" name="password2" size="30" /></td>
        </tr>
        <tr>
            <td>email :</td>
            <td><input type="text" name="email" size="30" /></td>
        </tr>
        <tr>
            <td>Phone :</td>
            <td><input type="text" name="phone" size="30" /></td>
        </tr>
        <tr>
            <td>City :</td>
            <td><input type="text" name="city" size="30" /></td>
        </tr>
    </table>
    <p />
    <input type="submit" value="Register" />
</form>

UserControllerServlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String userName=request.getParameter("userName");
    String password=request.getParameter("password1");
    String email=request.getParameter("email");
    String phone=request.getParameter("phone");
    String city=request.getParameter("city");

    HttpSession session=request.getSession(true);

    try{
        UserDAO userDAO=new UserDAO();
        userDAO.addUserDetails(userName, password, email, phone, city);
        response.sendRedirect("Success");

    }
    catch(Exception e){
        e.printStackTrace();

    }

User.java

public class User {

private int id;
private String userName;
private String password1;
private String email;
private String phone;
private String city;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getPassword1() {
    return password1;
}
public void setPassword1(String password1) {
    this.password1 = password1;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}

UserDAO的

public class UserDAO {

public void addUserDetails(String userName,String password,String email,String phone,String city){

    try{
        Configuration config=new Configuration();

        SessionFactory sessionFactory=config.configure().buildSessionFactory();

        Session session=sessionFactory.openSession();

        Transaction transaction=session.beginTransaction();

        User user=new User();

        user.setUserName(userName);
        user.setPassword1(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setCity(city);

        session.save(user);
        transaction.commit();

        System.out.println("\n\n Detais Added \n\n");


    }
    catch(HibernateException e){
        System.out.println(e.getMessage());
        System.out.println("error");
    }

的hibernate.cfg.xml

<hibernate-configuration>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@chetan:1521:XE</property>
    <property name="connection.username">system</property> 
    <property name="connection.password">manager</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>             

    <mapping resource="com/jwt/hibernate/bean/user.hbm.xml" /> 
</session-factory>

user.hbm.xml

<hibernate-mapping>
<class name="com.jwt.hibernate.bean.User" table="USER">
    <id column="ID" name="id" type="java.lang.Integer" />
    <property column="USER_NAME" name="userName" type="java.lang.String" />
    <property column="PASSWORD" name="password1" type="string" />
    <property column="EMAIL" name="email" type="java.lang.String" />
    <property column="PHONE" name="phone" type="java.lang.String" />
    <property column="CITY" name="city" type="java.lang.String" />
</class>

1 个答案:

答案 0 :(得分:0)

这可能是因为USER是关键字,你需要把它们放在像

这样的反引号中
insert into `USER` (USER_NAME, PASSWORD, EMAIL, PHONE, CITY, ID) values (?, ?, ?, ?, ?, ?)
  

您可以强制Hibernate在生成的SQL中引用标识符   在映射中的反引号中包含表或列名称   文献。 Hibernate将为SQL使用正确的引用样式   方言。这通常是双引号,但SQL Server使用   括号和MySQL使用反引号。