I am using DllUtils and wish to use it to generate the database schema into XML file. Does anyone know how to use it? I am using SQL Server for testing. Not sure how exactly I can get the DataSource object out of it. 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 with my "model" name, but I don't know what exactly it is supposed to be in order to get the Database 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);
}
}
I am getting the following errors:
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.readTable(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.trillium.io.test.Main.main(Main.java:...)
For the testing program, I have included 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.1 naming-common-4.1.36 commons-digester3-3.2
I tried to change the readDatabase method:
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. What exactly is the name supposed to be if my database name is "database"...
getDatabase
public Database getDatabase(Connection connection, String name) throws SQLException Reads the database model from the given connection. Parameters: connection - The connection name - The name of the resulting database; null when the default name (the catalog) is desired which might be null itself though Returns: The database model Throws: SQLException
readModelFromDatabase
Database readModelFromDatabase(Connection connection, String name) throws DatabaseOperationException Reads the database model from the live database to which the given connection is pointing. Parameters: connection - The connection to the database name - The name of the resulting database; 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
1条答案
按热度按时间9gm1akwq1#
原因是ddlutils不包含nvarchar的类型Map。
我采取了如下办法解决了这个问题:首先,使用platform.readModelFromDatabase(名称,数据库名称,“dbo”,new String[]{“TABLE”});然后,将org.apache. dddlutils.model.TypeMap重写为如下所示:在项目中创建类org.apache.ddlutils.model.TypeMap,将所有代码复制到TypeMap中,在静态块中添加以下代码:registerJdbcType(-9,“VARCHAR”,JdbcTypeCategoryEnum.TEXTUAL);那就成功了。