I created local database in DB2 called \"TestDB
\" then I created table called \"TestTable
\".
I found that the table is put under schema name is
The error SQLERROR -204, SQLSTATE 42704 is a missing/unknown objectname and it is most likely caused by a missing space in:
rs = stmt.executeQuery("select *from TestTable");
Which should be:
rs = stmt.executeQuery("select * from TestTable");
As @Mark Rotteveel said, the -204
error is from a missing object, but it's missing for a reason other than what he said.
It's not found because you did not prefix it with the schema name. You said above that it's in schema yasmin
, but you're connecting with db2admin
, so it's trying to look for db2admin.TestTable
.
SELECT * FROM yasmin.TestTable
should be what you're looking for.
By default, the search path for schemas is the name of the currently connecting user. You can see what it is using
SELECT CURRENT SCHEMA FROM SYSIBM.SYSDUMMY1
If you want to change it, you can use the SET SCHEMA command to change the search path, but usually it's easier just to include the schema name in your query.
The problem was The table I created in db2 has schema name "yasmin"
I used username and password "db2admin/db2admin"
In JDBC connection
so it search for the table in this schema "db2admin"
so I need to set the schema to be "yasmin"
so I added the following lines of code after creating
the connection
String schemaName="yasmin";
if (schemaName != null && schemaName.length() != 0) {
try {
statement = connection.createStatement();
statement.executeUpdate("set current sqlid = " + schemaName);
System.out.println("The schema is set successfully.");
} catch (SQLException exception) {
exception.printStackTrace();
}
and grant db2admin all privilages on this table in db2
1-Right click on the table choose privilages
2-Click on "Add user" button
3-then write the username "db2admin" and click "apply" button
4-Then select the user you just add and click "gtant all" button
Now he can see the table.
Many Thanks @Mark Rotteveel and @bhamby for your help.
Other way is just create an ALIAS, likes this:
CREATE ALIAS USER1.YOURTABLE FOR TABLE USER2.YOURTABLE;
If you are connecting in Spring using Hibernate use following in the property line
<property name="url" value="jdbc:db2://<ip>:<port>/<database>:currentSchema=<currentSchema>;" />