2013-08-22 3 views
0

я посвежее зимовать рамки, я пытаюсь запустить мое первое приложение спящего режима я получил следующее исключение,не удалось выполнить JDBC пакетное обновление

Exception

Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) 
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235) 
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) 
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) 
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) 
at com.java4s.insert.InsertMain.main(InsertMain.java:26) Caused by: java.sql.BatchUpdateException: 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 'insert (user, pass, sno) values ('krish', 'password', 1)' at line 1 at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2045) 
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1468) 
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) 
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)... 8 more Caused by: com.mysql.jdbc.exceptions.jdbc4.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 'insert (user, pass, sno) values ('krish', 'password', 1)' at line 1 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
at java.lang.reflect.Constructor.newInstance(Unknown Source) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) 

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"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="connection.driver-class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/test</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">root</property> 
     <property name="show_sql">true</property> 
     <property name="dilect">org.hibernate.dialect.MySQL.Dialect</property> 
     <property name="hbm2dll.auto">update</property> 
     <mapping resource="Insert.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

Insert.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"> 
<hibernate-mapping> 
    <class name="com.java4s.insert.Insert" table="insert"> 
     <id name="sno" column="sno"/> 
     <property name="username" column="user"/> 
     <property name="password" column="pass"/> 
    </class> 
</hibernate-mapping> 

Insert.java (POJO класс)

public class Insert { 
    private int sno; 
    private String username; 
    private String password;  
    //setter and getter method; 
} 

InsertMain (класс конфигурации)

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

public class InsertMain { 

    public static void main(String[] args) { 

     Configuration cfg=new Configuration(); 
     cfg.configure("hibernate.cfg.xml"); 
     SessionFactory factory=cfg.buildSessionFactory(); 
     Session session=factory.openSession(); 
     Transaction tx=session.beginTransaction(); 
     Insert i=new Insert(); 
     i.setSno(1); 
     i.setUsername("krish"); 
     i.setPassword("password"); 
     session.save(i); 
     System.out.println("Object saved successfully.....!!"); 
     tx.commit(); 
     session.close(); 
     factory.close(); 
    } 
} 

ответ

3

insert зарезервированное SQL ключевое слово. Не используйте его как имя таблицы. Не используйте user как имя столбца.

+0

Теперь пакет обновления JDBC Update Exception очищен, но я получил test.product не смог найти исключение, если бы я вручную создал таблицу, мой код отлично работает, что ошибка в моем коде, почему таблица автоматически не создана. – Krish

+1

Поскольку свойство hibernate является 'hbm2ddl.auto', а не' hbm2dll.auto'. DLL = динамическая библиотека ссылок. DDL = Язык определения данных. –

+0

о! @JB Nizet благодарит меня, мой код работает нормально – Krish

Смежные вопросы