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

2023-11-23 03:58

本文主要是介绍MySQL不能自动创建表格原因小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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方言

DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS390 org.hibernate.dialect.DB2390Dialect PostgreSQL org.hibernate.dialect.PostgreSQLDialect MySQL org.hibernate.dialect.MySQLDialect MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect Oracle (any version) org.hibernate.dialect.OracleDialect Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect Sybase org.hibernate.dialect.SybaseDialect Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect Microsoft SQL Server org.hibernate.dialect.SQLServerDialect SAP DB org.hibernate.dialect.SAPDBDialect Informix org.hibernate.dialect.InformixDialect HypersonicSQL org.hibernate.dialect.HSQLDialect Ingres org.hibernate.dialect.IngresDialect Progress org.hibernate.dialect.ProgressDialect Mckoi SQL org.hibernate.dialect.MckoiDialect Interbase org.hibernate.dialect.InterbaseDialect Pointbase org.hibernate.dialect.PointbaseDialect FrontBase org.hibernate.dialect.FrontbaseDialect Firebird org.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不能自动创建表格原因小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/415169

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

MySQL中的InnoDB单表访问过程

《MySQL中的InnoDB单表访问过程》:本文主要介绍MySQL中的InnoDB单表访问过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、访问类型【1】const【2】ref【3】ref_or_null【4】range【5】index【6】

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分