`
shappy1978
  • 浏览: 709344 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

MySQL不能自动创建表格原因小结

    博客分类:
  • J2EE
阅读更多

    1.错误原因:hbm文件中定义的元素含有sql的关键字,例如: 
    Java代码   收藏代码
    1. <property name="name"></property>  
    2. <property name="birthday"></property>  
    3. <property name="from"></property>  

      代码中含有"from"关键字,创建表时抛错如下代码: 
    Java代码   收藏代码
    1. Caused by: 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 'from) values ('name', '2009-04-02 14:23:59', null)' at line 1  
    2.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)  
    3.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)  
    4.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)  
    5.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)  
    6.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)  
    7.     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)  
    8.     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)  
    9.     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)  
    10.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)  
    11.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)  
    12.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)  
    13.     at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)  
    14.     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)  


    2.错误原因:配置文件中使用了create-drop属性,数据库表其实已经创建,只是又被删除了 
    Java代码   收藏代码
    1. <property name="hbm2ddl.auto">create-drop</property>  

    只要把create-drop替换为create即可 

    3.错误原因:默认的数据库类型错误。 
    把mysql中my.ini文件中配置的 MyISAM 变成 INNODB,代码如下, 
    Java代码   收藏代码
    1. # The default storage engine that will be used when create new tables when  
    2. default-storage-engine=INNODB  


    4.错误原因;hbm文件中未写错,但是在myeclipse中开发的 
    myeclipse不能自动识别如下语句,需要在Configuration中手动重新配置一下即可运行; 
    Java代码   收藏代码
    1. <property name="hbm2ddl.auto">create</property>  

    也可能hbm2dll的相对位置在配置文件中过低,导致无法识别(参考其他人提出的原因) 

    错误代码 
    Java代码   收藏代码
    1. Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'test.hzhu_user' doesn't exist  
    2.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)  
    3.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)  
    4.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)  
    5.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)  
    6.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)  
    7.     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)  
    8.     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)  
    9.     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)  
    10.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)  
    11.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)  
    12.     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)  
    13.     at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)  
    14.     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)  
    //***************************************************
    可惜上面的原因都不适合我,找了很久,包括更新驱动程序,检查hbm文件等等,最终发现是数据库方言设置的问题,原来是这样:
    hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
    后来修改为
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    就可以了,俺的基础薄弱,原因待查.
    hibernate 设置 hibernate.hbm2ddl.auto 为 create 时 会自动导入 import.sql 文件,import.sql 文件必须放在classpath根目录下,由于测试数据需要自动导入不同的数据,能否在程序或配置文件中指定 import.sql 文件的位置?
    //***************************************************

    数据库

    hibernate方言

    DB2org.hibernate.dialect.DB2DialectDB2 AS/400org.hibernate.dialect.DB2400DialectDB2 OS390org.hibernate.dialect.DB2390DialectPostgreSQLorg.hibernate.dialect.PostgreSQLDialectMySQLorg.hibernate.dialect.MySQLDialectMySQL with InnoDBorg.hibernate.dialect.MySQLInnoDBDialectMySQL with MyISAMorg.hibernate.dialect.MySQLMyISAMDialectOracle (any version)org.hibernate.dialect.OracleDialectOracle 9i/10gorg.hibernate.dialect.Oracle9DialectSybaseorg.hibernate.dialect.SybaseDialectSybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialectMicrosoft SQL Serverorg.hibernate.dialect.SQLServerDialectSAP DBorg.hibernate.dialect.SAPDBDialectInformixorg.hibernate.dialect.InformixDialectHypersonicSQLorg.hibernate.dialect.HSQLDialectIngresorg.hibernate.dialect.IngresDialectProgressorg.hibernate.dialect.ProgressDialectMckoi SQLorg.hibernate.dialect.MckoiDialectInterbaseorg.hibernate.dialect.InterbaseDialectPointbaseorg.hibernate.dialect.PointbaseDialectFrontBaseorg.hibernate.dialect.FrontbaseDialectFirebirdorg.hibernate.dialect.FirebirdDialect
    //***************************************************

    hibernate.properties   

      

    ######################   

    ### Query Language ###   

    ######################   

      

    ## define query language constants / function names   

      

    hibernate.query.substitutions yes 'Y', no 'N'  

      

      

    ## select the classic query parser   

      

    #hibernate.query.factory_class org.hibernate.hql.classic.ClassicQueryTranslatorFactory   

      

      

    #################   

    ### Platforms ###   

    #################   

      

    ## JNDI Datasource   

      

    #hibernate.connection.datasource jdbc/test   

    #hibernate.connection.username db2   

    #hibernate.connection.password db2   

      

      

    ## HypersonicSQL   

      

    hibernate.dialect org.hibernate.dialect.HSQLDialect   

    hibernate.connection.driver_class org.hsqldb.jdbcDriver   

    hibernate.connection.username sa   

    hibernate.connection.password   

    hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate   

    #hibernate.connection.url jdbc:hsqldb:hsql://localhost   

    #hibernate.connection.url jdbc:hsqldb:test   

      

    ## H2 (www.h2database.com)   

    #hibernate.dialect org.hibernate.dialect.H2Dialect   

    #hibernate.connection.driver_class org.h2.Driver   

    #hibernate.connection.username sa   

    #hibernate.connection.password   

    #hibernate.connection.url jdbc:h2:mem:./build/db/h2/hibernate   

    #hibernate.connection.url jdbc:h2:testdb/h2test   

    #hibernate.connection.url jdbc:h2:mem:imdb1   

    #hibernate.connection.url jdbc:h2:tcp://dbserv:8084/sample;    

    #hibernate.connection.url jdbc:h2:ssl://secureserv:8085/sample;    

    #hibernate.connection.url jdbc:h2:ssl://secureserv/testdb;cipher=AES   

      

    ## MySQL   

      

    #hibernate.dialect org.hibernate.dialect.MySQLDialect   

    #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect   

    #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect   

    #hibernate.connection.driver_class com.mysql.jdbc.Driver   

    #hibernate.connection.url jdbc:mysql:///test   

    #hibernate.connection.username gavin   

    #hibernate.connection.password   

      

      

    ## Oracle   

      

    #hibernate.dialect org.hibernate.dialect.OracleDialect   

    #hibernate.dialect org.hibernate.dialect.Oracle9Dialect   

    #hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver   

    #hibernate.connection.username ora   

    #hibernate.connection.password ora   

    #hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl   

    #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE   

      

      

    ## PostgreSQL   

      

    #hibernate.dialect org.hibernate.dialect.PostgreSQLDialect   

    #hibernate.connection.driver_class org.postgresql.Driver   

    #hibernate.connection.url jdbc:postgresql:template1   

    #hibernate.connection.username pg   

    #hibernate.connection.password   

      

      

    ## DB2   

      

    #hibernate.dialect org.hibernate.dialect.DB2Dialect   

    #hibernate.connection.driver_class com.ibm.db2.jcc.DB2Driver   

    #hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver   

    #hibernate.connection.url jdbc:db2://localhost:50000/somename   

    #hibernate.connection.url jdbc:db2:somename   

    #hibernate.connection.username db2   

    #hibernate.connection.password db2   

      

    ## TimesTen   

      

    #hibernate.dialect org.hibernate.dialect.TimesTenDialect   

    #hibernate.connection.driver_class com.timesten.jdbc.TimesTenDriver   

    #hibernate.connection.url jdbc:timesten:direct:test   

    #hibernate.connection.username   

    #hibernate.connection.password   

      

    ## DB2/400  

      

    #hibernate.dialect org.hibernate.dialect.DB2400Dialect   

    #hibernate.connection.username user   

    #hibernate.connection.password password   

      

    ## Native driver   

    #hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver   

    #hibernate.connection.url jdbc:db2://systemname   

      

    ## Toolbox driver   

    #hibernate.connection.driver_class com.ibm.as400.access.AS400JDBCDriver   

    #hibernate.connection.url jdbc:as400://systemname   

      

      

    ## Derby (not supported!)   

      

    #hibernate.dialect org.hibernate.dialect.DerbyDialect   

    #hibernate.connection.driver_class org.apache.derby.jdbc.EmbeddedDriver   

    #hibernate.connection.username   

    #hibernate.connection.password   

    #hibernate.connection.url jdbc:derby:build/db/derby/hibernate;create=true  

      

      

    ## Sybase   

      

    #hibernate.dialect org.hibernate.dialect.SybaseDialect   

    #hibernate.connection.driver_class com.sybase.jdbc2.jdbc.SybDriver   

    #hibernate.connection.username sa   

    #hibernate.connection.password sasasa   

    #hibernate.connection.url jdbc:sybase:Tds:co3061835-a:5000/tempdb   

      

      

    ## Mckoi SQL   

      

    #hibernate.dialect org.hibernate.dialect.MckoiDialect   

    #hibernate.connection.driver_class com.mckoi.JDBCDriver   

    #hibernate.connection.url jdbc:mckoi:///   

    #hibernate.connection.url jdbc:mckoi:local://C:/mckoi1.0.3/db.conf   

    #hibernate.connection.username admin   

    #hibernate.connection.password nimda   

      

      

    ## SAP DB   

      

    #hibernate.dialect org.hibernate.dialect.SAPDBDialect   

    #hibernate.connection.driver_class com.sap.dbtech.jdbc.DriverSapDB   

    #hibernate.connection.url jdbc:sapdb://localhost/TST   

    #hibernate.connection.username TEST   

    #hibernate.connection.password TEST   

    #hibernate.query.substitutions yes 'Y', no 'N'  

      

      

    ## MS SQL Server   

      

分享到:
评论

相关推荐

    MySQL 建表的优化策略 小结

    然而,对于不常使用的列、数据量小的列、text、image和bit类型列以及在修改性能至关重要的场景下,不应创建索引。联合索引可以提升特定查询的效率,但要注意,若查询条件不完全匹配索引前列,将无法充分利用联合索引...

    powerdesigner连接mysql和导出表到word文档.docx

    #### 四、小结 通过上述步骤,用户不仅可以使用PowerDesigner连接MySQL数据库,还能轻松地将数据库表结构导出至Word文档中。这一过程不仅简化了数据库设计工作,还提高了工作效率。对于那些需要经常处理数据库设计...

    《MyEclipse 6 Java 开发中文教程》前10章

    7.4.1创建表格 112 7.4.2创建 HibernateDemo Java Project 113 7.4.3添加 Hibernate Capabilities 到现有项目 114 7.4.4 使用Hibernate配置文件编辑器修改文件 118 7.4.5 使用反向工程快速生成Java POJO类,映射文件...

    Mysql学习-生成数据库和表及应用数据库.pdf

    创建好数据库后,接下来就是设计并创建表格来存储数据。以动物园为例,我们可能需要创建一个名为`animals`的表来记录每只宠物的信息,如名字、种类、年龄等。 ```sql CREATE TABLE animals ( id INT AUTO_...

    hibernate小结

    Hibernate是一个基于Java的持久层ORM(对象关系映射)框架,它致力于解决Java应用程序与关系数据库之间的“阻抗不匹配”问题。在面向对象编程的世界里,我们使用对象、属性和关联来表示数据,而在关系型数据库中,...

    javaSE代码实例

    1.5 小结 11 第2章 基本数据类型——构建Java 大厦的基础 12 2.1 源代码注释 12 2.1.1 单行注释 12 2.1.2 区域注释 12 2.1.3 文档注释 13 2.2 基本数据类型 14 2.2.1 整型 15 2.2.2 浮点型 17 ...

    Eclipse_Swt_Jface_核心应用_部分19

    9.9.11 表格小结 192 9.10 树(Tree) 192 9.10.1 不同样式的树 193 9.10.2 为树添加图标 193 9.10.3 可编辑的树 196 9.10.4 表格树 197 9.10.5 树小结 198 9.11 格式化文本(StyleText) 198 ...

    疯狂JAVA讲义

    1.9 本章小结 22 本章练习 22 第2章 理解面向对象 23 2.1 面向对象 24 2.1.1 结构化程序设计简介 24 2.1.2 程序的三种基本结构 25 2.1.3 面向对象程序设计简介 27 2.1.4 面向对象的基本特征 28 2.2 UML...

    用POWER DESIGNER 生成MSYQL数据库反向工程.doc

    #### 五、小结 通过上述步骤,我们可以高效地利用PowerDesigner进行MySQL数据库的反向工程,从而获得清晰的物理数据模型。这不仅有助于我们更好地理解现有数据库的结构,还能够在此基础上进行优化和扩展。 #### 六...

    第23章 新闻发布系统数据库设计PPT

    22.4 小结 本章的重点在于数据库设计,特别强调了在MySQL数据库中的应用。在设计过程中,不仅考虑了表结构和字段定义,还引入了索引、视图和触发器等高级特性,以提升系统的整体性能。在性能优化方面,通过冗余字段...

    原创 wicket开发手记(二)

    小结 本篇文章通过一个具体的Wicket应用案例,详细介绍了如何利用Wicket框架构建具有查询、列表展示、分页等功能的Java Web应用。通过实际编码演示了项目结构设计、数据库连接配置、页面逻辑编写等关键步骤,为...

    perl实例精解第4版下

    ### 小结 以上章节介绍了如何使用Perl结合DBI模块操作MySQL数据库,以及如何使用Perl与操作系统进行交互。通过这些知识点的学习,读者可以更加熟练地使用Perl进行实际开发工作,特别是在处理数据库和文件系统方面。...

Global site tag (gtag.js) - Google Analytics