maybe 'index' is a keyword .be careful in sql programming , some words look likes keywords ,you should avoid them
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
Domanda
Hi i am trying to make simple jsp registration form using hibernate where i am getting following Exception
type Exception report
messageInternal Server Error
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
root cause
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
root cause
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index) from user_registration' at line 1
Here is my
Userregistration.hbm.xml
<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 9, 2011 6:53:58 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.hibernateClass.UserRegistration" table="user_registration">
<id name="index">
<generator class="increment"></generator>
</id>
<property name="userName"></property>
<property name="password"></property>
<property name="email"></property>
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/employee</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="com/hibernateClass/UserRegistration.hbm.xml"/>
</session-factory>
</hibernate-configuration>
UserRegistration.java
public class UserRegistration implements java.io.Serializable {
int index;
String userName;
String password;
String email;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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;
}
}
How can i handle this Exception and get my desired output
Thanks in advance
Soluzione
Altri suggerimenti
Yes we cant use keyword of SQL in code. Otherwise it will show this type of error.
Note: You can check for SQL keyword by write in SQL Editor (Ex. SQLYOG) that will be capital letter ..here is screen Shot.enter image description here
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow