2016-03-01 57 views
2

我得到「Hibernate:插入到USER(USER_NAME,PASSWORD,EMAIL,PHONE,CITY,ID)值(?,?,?,?,?,?) 無法執行JDBC批量更新 錯誤」 這error.Please幫我無法執行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> 

+0

請添加您的代碼和完整的堆棧跟蹤。 –

+0

歡迎來到堆棧溢出!請閱讀[如何問好問題](// stackoverflow.com/help/how-to-ask)並嘗試編輯您的問題。有了高質量的問題,您將會收到更快的答案。謝謝! – SmokeDispenser

+0

和休眠SQL輸出和綁定日誌 –

回答

0

那可能是因爲用戶是關鍵字,你需要將它們放在反引號裏面像

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

您可以強制休眠通過 封閉引用的標識符生成的SQL在映射 文件中的反引號中的表或列名稱。 Hibernate將使用SQL 方言的正確引號樣式。這通常是雙引號,但SQL Server使用 括號,MySQL使用反引號。

+0

非常感謝您的suggession.I只是將用戶類更改爲UserInfo類,它的工作原理。謝謝YOu –

+0

@MeghanBidwaik: - 不客氣。 –

相關問題