2
votes

I get "Hibernate: insert into USER (USER_NAME, PASSWORD, EMAIL, PHONE, CITY, ID) values (?, ?, ?, ?, ?, ?) Could not execute JDBC batch update error" this error.Please Help Me

I had Written Code like this

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
Add your code, and full stack trace, please. - v.ladynev
Welcome to Stack Overflow! Please read about how to ask good questions and try to edit your question. With high quality questions you will receive better answers faster. Thanks! - Tobi Nary
And hibernate SQL output and bindings log - Alessandro Da Rugna

1 Answers

0
votes

Thats probably because USER is keyword and you need to put them inside the backticks like

insert into `USER` (USER_NAME, PASSWORD, EMAIL, PHONE, CITY, ID) values (?, ?, ?, ?, ?, ?)

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.