我正在使用Spring Hibernate来克隆一个新的MySQL数据库(通过利用现有的数据库的结构),但是我的代码如下所示抛出异常。任何想法为什么会发生这种情况?
String existingDb = "SHOW TABLES IN crm_prod";
String dbName = "NewDb";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE DATABASE " + dbName);
SqlRowSet result = jdbcTemplate.queryForRowSet(existingDb);
while(result.next()) {
String tableName = result.getString(result.getMetaData().getColumnName(1));
jdbcTemplate.execute("CREATE TABLE "+dbName+"." + tableName +
" LIKE crm_prod." + tableName);
}
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SHOW TABLES IN crm_prod]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'TABLE_NAMES' in information_schema
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:91)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:419)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:474)
at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:515)
at Tester.main(Tester.java:43)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'TABLE_NAMES' in information_schema
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1062)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2783)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1569)
at com.mysql.jdbc.Field.getCollation(Field.java:491)
at com.mysql.jdbc.ResultSetMetaData.isCaseSensitive(ResultSetMetaData.java:569)
at com.sun.rowset.CachedRowSetImpl.initMetaData(CachedRowSetImpl.java:722)
at com.sun.rowset.CachedRowSetImpl.populate(CachedRowSetImpl.java:639)
at org.springframework.jdbc.core.SqlRowSetResultSetExtractor.createSqlRowSet(SqlRowSetResultSetExtractor.java:82)
at org.springframework.jdbc.core.SqlRowSetResultSetExtractor.extractData(SqlRowSetResultSetExtractor.java:65)
at org.springframework.jdbc.core.SqlRowSetResultSetExtractor.extractData(SqlRowSetResultSetExtractor.java:46)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:463)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:408)
... 3 more
答案 0 :(得分:1)
我通过以下代码实现了目标。
SqlRowSet createTable = jdbcTemplate.queryForRowSet(
"SELECT * FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`='" + cloneDBName + "'");
while (createTable.next()) {
String tableName = createTable.getString(createTable.getMetaData().getColumnName(3));
jdbcTemplate
.execute("CREATE TABLE " + dbName + "." + tableName + " LIKE " + cloneDBName + "." + tableName);
}
答案 1 :(得分:0)
可以添加适当的数据进行排序: 从DatabaseMetadata中,您可以提取表格和表格等详细信息。列。
String catalog = null;
String schemaPattern = null;
String tableNamePattern = null;
String[] types = null;
String columnNamePattern = null;
DatabaseMetaData databaseMetaData = con.getMetaData();
result = databaseMetaData.getTables(catalog, schemaPattern, tableNamePattern, types );
while(result.next()) {
String tableName =result.getString(3);
result1= databaseMetaData.getColumns(catalog, schemaPattern, tableName, columnNamePattern);
while(result1.next()) {
String columnName = result1.getString(4);
String columnType = result1.getString(6);
}
}
For More info : http://tutorials.jenkov.com/jdbc/databasemetadata.html