Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the current schema for DB2 using Hibernate/JDBC?

Tags:

schema

jdbc

db2

I used to use currentSchema=MYSCHEMA; in my JDBC URL connection, but the version of DB2 we're using no longer supports that, showing the error 'The "currentSchema" property is not allowed on the target server'. I've tried using hibernate.default_schema, but it's not automatically adding the schema to my table names. I don't want to set the schema on every @Table annotation since I'll need to change it between test and production. Is there another way to set on the connection or via Hibernate?

Update: it must have been a driver version issue. I upgraded to later drivers and currentSchema worked.

like image 520
Brian Deterling Avatar asked Jun 01 '10 21:06

Brian Deterling


People also ask

How do I create a schema in JDBC connection?

there is added support for specifying the current schema using URL. With @ClassRule, we create an instance of PostgreSQL database container. Next, in the setup method, create a connection to that database and create the required objects. To change the default schema, we need to specify the currentSchema parameter.

What is schema in IBM Db2?

A schema is a collection of named objects. The first part of a schema name is the qualifier. A schema provides a logical classification of objects in the database. The objects that a schema can contain include tables, indexes, table spaces, distinct types, functions, stored procedures, and triggers.

Which environment are used for developing the Java application for use with Db2 database?

JDBC. JDBC is an application programming interface (API) that Java applications use to access relational databases. IBM data server support for JDBC lets you write Java applications that access local Db2 or IBM Informix data or remote relational data on a server that supports DRDA.


2 Answers

With DB2 JDBC type 4 driver (com.ibm.db2.jcc.DB2Driver), I'm using this URL to connect :

jdbc:db2://<HOST>:<PORT>/<DATABASE>:currentSchema=<SCHEMA>;

Source: http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/rjvdsprp.htm

like image 180
Stéphane B. Avatar answered Sep 21 '22 16:09

Stéphane B.


All the properties for the 9.7 (Latest) db are here...

https://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=/com.ibm.db2.luw.apdv.java.doc/doc/r0052607.html

use:

currentSchema

Specifies the default schema name that is used to qualify unqualified database objects in dynamically prepared SQL statements. The value of this property sets the value in the CURRENT SCHEMA special register on the database server. The schema name is case-sensitive, and must be specified in uppercase characters.

like image 26
Romain Hippeau Avatar answered Sep 21 '22 16:09

Romain Hippeau