且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring 数据源和数据库模式

更新时间:2022-10-20 17:25:18

问题是没有标准的模式设置方法,每个数据库都有不同的机制.

解决方法是将架构设置为 db url 的一部分...

对于 db2,url 看起来像:

jdbc:db2://SERVER_NAME:PORT/DATABASE:currentSchema=SCHEMA_NAME;

希望对您有所帮助...

特别注意:一定要加分号;在 URL 的末尾,否则你会得到错误,说 URL 无效.最后还要确保什么都没有;存在(甚至没有空格).

I am trying to declare a Spring datasource pointing to a DB2 database. Presently I am using a org.springframework.jdbc.datasource.DriverManagerDataSource to setup the connection but am not finding any way to specify the database schema in the database in the datasource bean. Could anyone help me on this?

Problem is there is no standard way to set the schema, each database has a different mechanism.

A work around is to set the schema as part of the db url...

For db2 the url will look something like:

jdbc:db2://SERVER_NAME:PORT/DATABASE:currentSchema=SCHEMA_NAME;

hope that helps...

Special note: make sure you add the semicolon ; at the end of the URL, otherwise you will get errors saying URL is invalid. Also make sure nothing after last ; exists (not even spaces).