![](/img/trans.png)
[英]sql jdbc getgeneratedkeys returns column “id” not found, column type unknown
[英]"Unknown JDBC type code .. at Column.setTypeCode" (DllUtils)
我正在使用 DllUtils 并希望使用它将数据库模式生成到 XML 文件中。 有谁知道如何使用它? 我正在使用 SQL 服务器进行测试。 不知道我如何才能从中获得 DataSource object。 I am reading http://db.apache.org/ddlutils/api-usage.html and https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html#datasource_connection I think there is something wrong用我的“模型”名称,但我不知道它到底应该是什么才能获得数据库 object。
public class Main {
public static void main(String[] args) {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setDescription("Felix testing program");
ds.setServerName("localhost");
ds.setPortNumber(1433);
ds.setUser("test");
ds.setPassword("1234");
ds.setDatabaseName("database");
try {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES,
"org.apache.naming");
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
pdEnv.put(Context.PROVIDER_URL, "jdbc:sqlserver://localhost:1433");
Context ctx = new InitialContext(pdEnv);
ctx.bind("jdbc:sqlserver", ds);
DataSource ds2 = (DataSource)ctx.lookup("jdbc:sqlserver");
Database db = readDatabase(ds2);
writeDatabaseToXML(db, "C:/Users/fcao/Documents/sample.xml");
readDatabaseFromXML("C:/Users/fcao/Documents/sample.xml");
} catch (NamingException e) {
e.printStackTrace();
}
}
public static Database readDatabaseFromXML(String fileName)
{
return new DatabaseIO().read(fileName);
}
public static void writeDatabaseToXML(Database db, String fileName)
{
new DatabaseIO().write(db, fileName);
}
public static Database readDatabase(DataSource dataSource)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
return platform.readModelFromDatabase("model");
}
public static void changeDatabase(DataSource dataSource,
Database targetModel,
boolean alterDb)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
if (alterDb)
{
platform.alterTables(targetModel, false);
}
else
{
platform.createTables(targetModel, true, false);
}
}
我收到以下错误:
Exception in thread "main" org.apache.ddlutils.model.ModelException: Unknown JDBC type code -9 at org.apache.ddlutils.model.Column.setTypeCode(Column.java:215) at org.apache.ddlutils.platform. JdbcModelReader.readColumn(JdbcModelReader.java:781) at org.apache.ddlutils.platform.mssql.MSSqlModelReader.readColumn(MSSqlModelReader.java:177) at org.apache.ddlutils.platform.JdbcModelReader.readColumns(JdbcModelReader.java:755) at org.apache.ddlutils.platform.JdbcModelReader.readTable(JdbcModelReader.java:565) at org.apache.ddlutils.platform.mssql.MSSqlModelReader.rea dTable(MSSqlModelReader.java:100) at org.apache.ddlutils.platform.JdbcModelReader.readTables(JdbcModelReader.java:516) at org.apache.ddlutils.platform.JdbcModelReader.getDatabase(JdbcModelReader.java:472) at org.apache .ddlutils.platform.JdbcModelReader.getDatabase(JdbcModelReader.java:432) at org.apache.ddlutils.platform.PlatformImplBase.readModelFromDatabase(PlatformImplBase.java:1884) at org.apache.ddlutils.platform.PlatformImplBase.readModelFromDatabase(PlatformImplBase.java :1869) at com.trillium.io.test.Main.readDatabase(Main.java:...) at com.tri llium.io.test.Main.main(Main.java:...)
对于测试程序,我已经包含了 jars:commons-betwixt-0.8 commons-collections-3.2.2 commons-lang-2.6 commons-logging-1.2 DdlUtils-1.0 sqljdbc4 jakarta-oro-2.0.1naming-common-4.1.36公共消化器3-3.2
我试图改变 readDatabase 方法:
public static Database readDatabase(DataSource dataSource)
{
Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
Database db = null;
try {
db = platform.getModelReader().getDatabase(dataSource.getConnection(), "database");
} catch (Exception e) {
e.printStackTrace();
}
return db;
}
From https://db.apache.org/ddlutils/api/org/apache/ddlutils/Platform.html#readModelFromDatabase(java.sql.Connection , java.lang.String) documentation. 如果我的数据库名称是“数据库”,那么名称应该是什么...
获取数据库
public Database getDatabase(Connection connection, String name) throws SQLException 从给定的连接读取数据库 model。 参数: connection - 连接名称 - 结果数据库的名称; null 当需要默认名称(目录)时,可能是 null 本身,但返回:数据库 model 抛出:SQLException
从数据库中读取模型
Database readModelFromDatabase(Connection connection, String name) throws DatabaseOperationException 从给定连接指向的实时数据库中读取数据库 model。 参数: connection - 到数据库的连接 name - 结果数据库的名称; null when the default name (the catalog) is desired which might be null itself though Returns: The database model Throws: DatabaseOperationException - If an error occurred during reading the model
原因是 ddlutils 不包含 nvarchar 的类型映射。
我采取了以下方法解决了这个问题:首先,使用 platform.readModelFromDatabase(name, databaseName, "dbo", new String[]{"TABLE"}); Then, rewrite org.apache.ddlutils.model.TypeMap as follow: create class org.apache.ddlutils.model.TypeMap in project, copy all code into TypeMap, add following code into the static block, registerJdbcType(-9, "VARCHAR ", JdbcTypeCategoryEnum.TEXTUAL); 然后它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.